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

2021-01-09 開源中國

從 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是一個類似vi的文本編輯器,不過在vi的基礎上增加了很多新特性,vim被公認為類vi編輯器中最好用的一個。為什麼要學習VIM,vi不夠?vim在vi的基礎之上增加了很多的小功能。可以有效的幫助程式設計師快速的排查問題。
  • vim常用命令有哪些?Linux入門學習
    vim是一個文本編輯程序,沒有菜單,只有命令,且命令繁多。在運維培訓學習中,vim編輯器都有哪些知識點?有哪些命令需要掌握? 命令行模式下相關命令移動光標h:←左移l:→右移j:↓下移k:↑上移使用y複製選中內容 替換操作r:替換當前字符文本行移動>>:文本行右移<<:文本行左移文本模式下相關命令
  • Linux 系統操作之 vim 命令總結
    在該模式下,用戶可以輸入各種合法的Vi命令,用於管理自己的文檔。此時從鍵盤上輸入的任何字符都被當做編輯命令來解釋,若輸入的字符是合法的Vi命令,則Vi在接受用戶命令之後完成相應的動作。 但需注意的是,所輸入的命令並不在屏幕上顯示出來。若輸入的字符不是Vi的合法命令,Vi會響鈴報警。
  • Vim 與 Git 如何交互 - 技術翻譯 - OSCHINA 社區
    如果不依賴環境變量,core.editor的git 配置可以按如下方式設置:[core]    editor = "vim"當在Vim工具中編輯一條評論信息時,你想要終止,則可以使用:cq命令以non-zero狀態退出。這樣操作後Git會取消評論信息,及時你已經開始寫了一套信息。
  • vim+py文件構造linux後門
    那現在我們就可以使用vim的python擴展去啟動一個python後門,命令如下:vim -E -c "pyfile test.py"其中 -E是啟動一個改進的Ex模式(也就是命令模式),-c是去加載一個文件,並去執行。
  • 編輯神器Vim新教程出爐,GitHub3400星,複雜命令輕鬆搞定
    Vim 雖然非常強大,但是需要記住很多操作命令。如果沒有有效的學習方法,操作起來很不方便,只有做到非常熟練才能感受到它帶來的快捷。下圖展示了 Vim 中關於刪除、複製與粘貼的部分命令:其實 Vim 的操作命令還遠不如此,如果沒有很好的學習方法,可能很難使用這種編輯器。
  • 多語言編程必備的十大Vim插件
    你可以使用一個簡單的命令 volt profile set myprofile 啟用新的配置。這樣我可以做到這樣的事情,如為 Python 配置單獨啟用 indentpython 插件。Volt 還提供了一種針對每個插件配置的簡單方法,這些配置會在「profile」之間共享,因此只需要安裝一次插件,就可以在多個「profile」之間使用。
  • 10個你值得擁有的Vim配色方案
    這裡我們向你推薦10個值得擁有的 vim 配色方案:1. GRB256GRB256 基於 ir_black,感覺特別適合 Ruby on Rails 的應用開發。網址: GitHub作者: Gary Bernhardt2.
  • Linux-vi編輯器之簡單常用命令
    接著下來,我要將 50 到 100 行之間的 man 改為MAN ,並且一個一個挑選是否需要修改;命令:在轉義模式下輸入:50,100s/man/Man/c8.修改完之後,突然反悔了,要全部復原;命令:在命令模式下輸入u
  • 了解你的 VIM(二)!「地獄還是決鬥」系列 VIM 角色介紹在這裡
    上期我們一起了解了原創系列 VIM 的小故事,讓我們再來看看「地獄還是決鬥」系列的 VIM 又有著什麼不一樣的傳說吧~「地獄還是決鬥(Hell or High Noon)」由眾多遊戲和玩具行業公司的特約插畫師、知名藝術家 Inkarat Mittongtare
  • 10條Linux命令錦囊,防你刪庫牢底坐穿
    這些動作裡面,並不總是存在主觀的惡意,而是這些命令太危險了。線上操作時,一定要保持清醒的頭腦,切記馬虎大意。你說你誤操作的,誰信呢?4. cat命令cat命令也能出錯?是的,而且還很嚴重,因為你掌握了高級技能:重定向符。如果你想要向文件中追加 內容,會使用 cat >> file的方式,如果你不小心少輸入了一個>,那麼不好意思,你的文件內容就丟失了。
  • 8個nc命令的實用案例
    使用nc命令掃描埠netcat可用於埠掃描:了解目標伺服器上哪些埠是打開的並正在運行服務。它可以掃描一個或多個或一系列打開的埠。vim index.html 在文件中添加以下HTML行:<html>
  • linux常用命令總結
    (查看配置文件時,不需要配置文件的絕對路徑,只需要文件名即可)1:調用的是more命令來瀏覽幫助文檔,按空格翻下一頁,按回車翻下一行,2:按q退出。只有命令,沒有菜單。《大碗》編輯器版周圍的同事不是用VI就是Emacs,你要是用UltraEdit都不好意思跟人家打招呼...什麼插件呀、語法高亮呀、拼寫檢查呀,能給它開的都給它開著,就是一個字:酷!你說這麼牛X的一東西,怎麼著學會也得小半年吧。半年!?入門都遠著呢!
  • 推薦兩款非常實用的vim插件——快速定位、成對編輯
    今天給大家介紹兩款非常實用的vim插件,在頁面內快速移動以及成對編輯。該插件github地址是:https://github.com/easymotion/vim-easymotion同樣,我們使用vim-plug插件安裝好它。
  • 神操作:教你如何將Vim變成一個R語言IDE
    如果你在工作生活中遇到Vim / Emacsland,那麼本文可能會給你解惑不少。  為什麼不直接使用RStudio呢?我用的是vim - plug,你可以使用任何插件管理器輕鬆安裝它。  Plug 'jalvesaq/Nvim-R'  然後打開R文件並點擊 rf,你會看到terminal buffer 出現在與你當前會話綁定的R控制臺,點擊 rq。
  • 整理幾個超實用的前端提效shell命令
    curlcurl 是常用的命令行工具,用來請求 Web 伺服器。它的名字就是客戶端(client)的 URL 工具的意思。curl 功能非常強大,它的命令可以直接放到 postman 使用,postman 也是支持 curl 的請求方式。
  • 如何記憶Linux命令
    Linux是作業系統,作業系統提供進程、文件、網絡等管理功能特性決定了命令行的複雜性。Linux命令雖然多,但學習Linux命令可以換一種角度來學習。許多人記憶的過程是這樣的ls --> 列出目錄內容這樣的記憶就是簡單的映射,沒有構建起聯繫。