超過130 個你需要了解的 vim 命令

2021-01-12 開源中國

從 1970 年開始,vi 和 vim 就成為了程式設計師最喜愛的文本編輯器之一。5年前,我寫了一個問自己名為 「每個程式設計師都應該知道的 100 個 vim 命令」 這次算是之前那篇文章的改進版,希望你會喜歡。

基礎:e filenameOpen filename for edition:wSave file:qExit Vim:q!Quit without saving:xWrite file (if changes has been made) and exit:sav filenameSaves file as filename.Repeats the last change made in normal mode5.Repeats 5 times the last change made in normal mode在文件中移動k or Up Arrowmove the cursor up one linej or Down Arrowmove the cursor down one lineemove the cursor to the end of the wordbmove the cursor to the begining of the word0move the cursor to the begining of the lineGmove the cursor to the end of the lineggmove the cursor to the begining of the fileLmove the cursor to the end of the file:59move cursor to line 59. Replace 59 by the desired line number.20|move cursor to column 20.%Move cursor to matching parenthesis[[Jump to function start[{Jump to block start剪切、複製和粘貼yCopy the selected text to clipboardpPaste clipboard contentsddCut current lineyyCopy current liney$Copy to end of lineDCut to end of line搜索/wordSearch word from top to bottom?wordSearch word from bottom to top*Search the word under cursor/\cstringSearch STRING or string, case insensitive/jo[ha]nSearch john or joan/\< theSearch the, theatre or then/the\>Search the or breathe/\< the\>Search the/\< ¦.\>Search all words of 4 letters/\/Search fred but not alfred or frederick/fred\|joeSearch fred or joe/\<\d\d\d\d\>Search exactly 4 digits/^\n\{3}Find 3 empty lines:bufdo /searchstr/Search in all open filesbufdo %s/something/somethingelse/gSearch something in all the open buffers and replace it with somethingelse替換:%s/old/new/gReplace all occurences of old by new in file:%s/onward/forward/giReplace onward by forward, case unsensitive:%s/old/new/gcReplace all occurences with confirmation:2,35s/old/new/gReplace all occurences between lines 2 and 35:5,$s/old/new/gReplace all occurences from line 5 to EOF:%s/^/hello/gReplace the begining of each line by hello:%s/$/Harry/gReplace the end of each line by Harry:%s/onward/forward/giReplace onward by forward, case unsensitive:%s/ *$//gDelete all white spaces:g/string/dDelete all lines containing string:v/string/dDelete all lines containing which didn’t contain string:s/Bill/Steve/Replace the first occurence of Bill by Steve in current line:s/Bill/Steve/gReplace Bill by Steve in current line:%s/Bill/Steve/gReplace Bill by Steve in all the file:%s/^M//gDelete DOS carriage returns (^M):%s/\r/\r/gTransform DOS carriage returns in returns:%s#<[^>]\+>##gDelete HTML tags but keeps text:%s/^\(.*\)\n\1$/\1/Delete lines which appears twiceCtrl+aIncrement number under the cursorCtrl+xDecrement number under cursorggVGg?Change text to Rot13大小寫VuLowercase lineVUUppercase lineg~~Invert casevEUSwitch word to uppercasevE~Modify word caseggguGSet all text to lowercasegggUGSet all text to uppercase:set ignorecaseIgnore case in searches:set smartcaseIgnore case in searches excepted if an uppercase letter is used:%s/\<./\u&/gSets first letter of each word to uppercase:%s/\<./\l&/gSets first letter of each word to lowercase:%s/.*/\u&Sets first letter of each line to uppercase:%s/.*/\l&Sets first letter of each line to lowercase讀寫文件:1,10 w outfileSaves lines 1 to 10 in outfile:1,10 w >> outfileAppends lines 1 to 10 to outfile:r infileInsert the content of infile:23r infileInsert the content of infile under line 23文件瀏覽器:e .Open integrated file explorer:SexSplit window and open integrated file explorer:Sex!Same as :Sex but split window vertically:browse eGraphical file explorer:lsList buffers:cd ..Move to parent directory:argsList files:args *.phpOpen file list:grep expression *.phpReturns a list of .php files contening expressiongfOpen file name under cursor和 Unix 系統交互:!pwdExecute the pwd unix command, then returns to Vi!!pwdExecute the pwd unix command and insert output in file:shTemporary returns to Unix$exitRetourns to Vi對齊:%!fmtAlign all lines!}fmtAlign all lines at the current position5!!fmtAlign the next 5 linesTabs/Windows:tabnewCreates a new tabgtShow next tab:tabfirstShow first tab:tablastShow last tab:tabm n(position)Rearrange tabs:tabdo %s/foo/bar/gExecute a command in all tabs:tab ballPuts all open files in tabs:new abc.txtEdit abc.txt in new window分屏顯示:e filenameEdit filename in current window:split filenameSplit the window and open filenamectrl-w up arrowPuts cursor in top windowctrl-w ctrl-wPuts cursor in next windowctrl-w_Maximize current window verticallyctrl-w|Maximize current window horizontallyctrl-w=Gives the same size to all windows10 ctrl-w+Add 10 lines to current window:vsplit fileSplit window vertically:sview fileSame as :split in readonly mode:hideClose current window:­nlyClose all windows, excepted current:b 2Open #2 in this window自動完成Ctrl+n Ctrl+p (in insert mode)Complete wordCtrl+x Ctrl+lComplete line:set dictionary=dictDefine dict as a dictionnaryCtrl+x Ctrl+kComplete with dictionnaryMarksm {a-z}Marks current position as {a-z}' {a-z}Move to position {a-z}''Move to previous position

縮寫:ab mail mail@provider.orgDefine mail as abbreviation of mail@provider.org文本縮進:set autoindentTurn on auto-indent:set smartindentTurn on intelligent auto-indent:set shiftwidth=4Defines 4 spaces as indent sizectrl-t, ctrl-dIndent/un-indent in insert mode>>Indent<<Un-indent=%Indent the code between parenthesis1GVG=Indent the whole file語法高亮:syntax onTurn on syntax highlighting:syntax offTurn off syntax highlighting:set syntax=perlForce syntax highlighting

 

via catswhocode

相關焦點

  • vim命令使用總結
    vim 共有三種模式:命令模式:用vim打開文件就進入了命令模式;輸入模式:在命令模式輸入i/I/a/A/o/O就進入輸入模式;在輸入模式下按鍵esc,則退出輸入模式進入命令模式底線模式:在命令模式按下英文冒號:就進入了底線模式;二. vim命令使用總結2.1 光標移動(1)光標上下左右移動(鍵盤的上下左右四個鍵):
  • linux-vi或vim命令
    稍微解釋一下,當在normal模式下輸入:qx後,你對文本的所有編輯動作將會被記錄下來,再次輸入q即退出了記錄模式,然後輸入@x對剛才記錄下來的命令進行重複,此命令後可跟數字,表示要重複多少次,比如@x20,可以重複20次。
  • linux中的vim命令
    如何查看命令的幫助文檔,三種方式:內置命令:help ls外部命令:ls --help,簡單的幫助文檔手冊man ls, 通過使用man查看詳細的幫助內容vim和vi命令本質上就是一個命令,這個命令是在linux命令中專門用來編輯文本文件的
  • Linux 下 非常強大的Vim命令合集詳解
    啟動vim 在命令行窗口中輸入以下命令即可 vim 直接啟動vim vim filename 打開vim並創建名為filename的文件 文件命令 打開單個文件 vim file 同時打開多個文件 vim file1 file2 file3 ...
  • vim快捷鍵收藏版
    vim和vscode 到底誰更好用,大家爭得不可開交,然後我就在vscode裡面裝了一個vim插件,完美得解決了這個問題,用完之後覺得真香,所以我就整理一份終端命令的快捷鍵,所以大家趕緊收藏起來吧。作者:良知猶存轉載授權以及圍觀:歡迎添加微信公眾號:羽林君一:初識VI/VIM剛開始用 VIM 打開文件的時候,需要從宏觀的去了解一下 VIM 這個編輯器。
  • 學會這21條,你離 Vim 大神就不遠了!
    打開文件# 打開單個文件vim file    # 同時打開多個文件vim file1 file2..  # 在vim窗口中打開一個新文件:open [file]       【舉個例子】# 當前打開1.txt,做了一些編輯沒保存:open!
  • @程式設計師,不容錯過的 Vim 實用技巧請查收!
    然而,在我看來,不斷發現新事物也許是失敗之處,因為你很難知道Vim還有哪些功能。雖然人們經常談論模態編輯或文本對象的美感,但我認為這並不是Vim的本質。Vim是一個拼湊而成的子系統,每一部分都塞滿了各種特殊用途的工具。僅在普通模式下就有超過一百種不同的鍵盤輸入命令。密集的功能成就了Vim的實用性。
  • 編輯神器Vim新教程出爐,GitHub3400星,複雜命令輕鬆搞定
    提起文本編輯器,你一定會想到編輯器之神 Vim。作為一個快 30 歲的「老牌」編輯器,直到現在還很受歡迎。Vim 編輯文件非常高效,可支持多個作業系統,如 Unix/Linux、Windows、macOS,甚至 iOS 和 Android 上都能找到 Vim 的移植版本。Vim 雖然非常強大,但是需要記住很多操作命令。
  • 開發者的實用 Vim 插件(一) - OSCHINA - 中文開源技術交流社區
    在你的Home目錄下創建 .vim 目錄,並在其中創建子目錄 autoload 和 bundle。然後,在 autoload 放置 pathogen.vim 文件,這個文件可以從此處 下載。最後,在你的Home目錄創建 .vimrc 文件,並添加以下內容。call pathogen#infect()
  • Linux-vi編輯器之簡單常用命令
    接著下來,我要將 50 到 100 行之間的 man 改為MAN ,並且一個一個挑選是否需要修改;命令:在轉義模式下輸入:50,100s/man/Man/c8.修改完之後,突然反悔了,要全部復原;命令:在命令模式下輸入u9.
  • linux常用命令總結
    (查看配置文件時,不需要配置文件的絕對路徑,只需要文件名即可)1:調用的是more命令來瀏覽幫助文檔,按空格翻下一頁,按回車翻下一行,2:按q退出。只有命令,沒有菜單。《大碗》編輯器版周圍的同事不是用VI就是Emacs,你要是用UltraEdit都不好意思跟人家打招呼...什麼插件呀、語法高亮呀、拼寫檢查呀,能給它開的都給它開著,就是一個字:酷!你說這麼牛X的一東西,怎麼著學會也得小半年吧。半年!?入門都遠著呢!
  • 【評測】Vimtag小雪人智能攝像機評測
    無論是家庭防護防盜還是看顧老人小孩,智能攝像機都能幫你解決這些問題。前不久vimtag(微目騰科)推出智能雲攝像機CP1,據介紹,該款智能雲攝像機受到廣大家庭的喜愛。拿到機子後,迫不及待就打開了,現場就試用起來,我們一起來看下這款小雪人攝像頭的魅力吧。一、開箱篇
  • 好玩的Linux命令,將禮品包在盒子中
    不管你是慶祝春節,還是另一個節日。我收集到的幾個Linux命令行工具,供您欣賞並與朋友共享。讓我們一起來找點樂子,給這個寒冷的冬天增添點快樂吧。很有可能,你以前聽說過一些。但是,希望我們都有機會學習新東西。今天我們將了解如何使用boxes實用程序在Linux終端繪製帶有字符的形狀,並使您的單詞脫穎而出。
  • 自從用上命令行神器「thefuck」, 再也不用擔心我敲錯命令了
    你是不是經常在終端敲錯命令?敲錯命令,刪掉重敲,很煩有沒有?
  • Linux 命令行下的好東西
    就這個角度而言絕大多數 Linux 命令行的推廣簡直是爛透了。繁多 Linux 有用極了的命令行工具就靜靜的躺在你發行版默認安裝的包裹裡,很多時候只有當你碰到什麼問題的時候網上搜一圈才能知道。更蛋疼的是這裡面很多東西你一旦知道了就會想我靠原來沒這個到底是怎麼過的下去。所以這裡我會列出一些我用過的一些,大部分發行版默認就有,或者能用包裹管理工具輕鬆獲取的東西。
  • 這些常用命令你確定都用過嗎?
    這個命令也是極具危險性的,因為不但會清除工作區中未提交的改動,也會清除暫存區中未提交的改動。# 添加當前目錄下所有文件到暫存區$ touch README.md$ git add README.mdgit status查看在你上次提交之後是否有對文件進行再次修改。
  • 粉姬勇士登場 VIM 自動販賣機開啟 2.0 首秀!
    在勇士系列中,她是最有共情心的一個,雖然在所有勇士中粉姬擁有最強大的力量,但她卻選擇用自己的力量去幫助那些需要幫助的人。揮舞著擁有療愈功能的水晶球,粉姬勇士帶著她治癒一切疾病的力量,加入了 VIMworld 宇宙,與智能收藏品生態中的其他寶寶們一同開啟了她的新生活...
  • Linux shell命令總結大全
    本文轉載自【微信公眾號:java進階架構師,ID:java_jiagoushi】經微信公眾號授權轉載,如需轉載與原文作者聯繫前言Linux shell命令應該算是非常入門的東西,權當給大家做個記錄。但是實際上在使用的時候,會遇到各種各樣的問題,前幾天我在我們的項目上需要做一個功能,根據進程名字殺死這個進程,下面是過程1、我們正常需要的操作是$ps |grep xxx $kill -9 xx2、kill命令是常用的,但是用killall
  • 計算機Ping命令的7種玩法,教你秒變大神!
    命令的7個基礎用法,掌握了秒變大神! time值:響應時間,這個時間越小,說明你連接這個地址速度越快。 TTL值:Time To Live,表示DNS記錄在DNS伺服器上存在的時間,它是IP協議包的一個值,告訴路由器該數據包何時需要被丟棄。