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

来源:https://www.cnblogs.com/zfc2201/archive/2018/01/01/8169223.html
-Advertisement-
Play Games

從 1970 年開始,vi 和 vim 就成為了程式員最喜愛的文本編輯器之一。5年前,我寫了一個問自己名為 “每個程式員都應該知道的 100 個 vim 命令” 這次算是之前那篇文章的改進版,希望你會喜歡。 基礎 在文件中移動 剪切、複製和粘貼 搜索 替換 大小寫 讀寫文件 文件瀏覽器 和 Unix ...


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

基礎

:e filename Open filename for edition
:w Save file
:q Exit Vim
:q! Quit without saving
:x Write file (if changes has been made) and exit
:sav filename Saves file as filename
. Repeats the last change made in normal mode
5. Repeats 5 times the last change made in normal mode

在文件中移動

k or Up Arrow move the cursor up one line
j or Down Arrow move the cursor down one line
e move the cursor to the end of the word
b move the cursor to the begining of the word
0 move the cursor to the begining of the line
G move the cursor to the end of the line
gg move the cursor to the begining of the file
L move the cursor to the end of the file
:59 move 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

剪切、複製和粘貼

y Copy the selected text to clipboard
p Paste clipboard contents
dd Cut current line
yy Copy current line
y$ Copy to end of line
D Cut to end of line

搜索

/word Search word from top to bottom
?word Search word from bottom to top
* Search the word under cursor
/\cstring Search STRING or string, case insensitive
/jo[ha]n Search john or joan
/\< the Search 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\|joe Search fred or joe
/\<\d\d\d\d\> Search exactly 4 digits
/^\n\{3} Find 3 empty lines
:bufdo /searchstr/ Search in all open files
bufdo %s/something/somethingelse/g Search something in all the open buffers and replace it with somethingelse

替換

:%s/old/new/g Replace all occurences of old by new in file
:%s/onward/forward/gi Replace onward by forward, case unsensitive
:%s/old/new/gc Replace all occurences with confirmation
:2,35s/old/new/g Replace all occurences between lines 2 and 35
:5,$s/old/new/g Replace all occurences from line 5 to EOF
:%s/^/hello/g Replace the begining of each line by hello
:%s/$/Harry/g Replace the end of each line by Harry
:%s/onward/forward/gi Replace onward by forward, case unsensitive
:%s/ *$//g Delete all white spaces
:g/string/d Delete all lines containing string
:v/string/d Delete 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/g Replace Bill by Steve in current line
:%s/Bill/Steve/g Replace Bill by Steve in all the file
:%s/^M//g Delete DOS carriage returns (^M)
:%s/\r/\r/g Transform DOS carriage returns in returns
:%s#<[^>]\+>##g Delete HTML tags but keeps text
:%s/^\(.*\)\n\1$/\1/ Delete lines which appears twice
Ctrl+a Increment number under the cursor
Ctrl+x Decrement number under cursor
ggVGg? Change text to Rot13

大小寫

Vu Lowercase line
VU Uppercase line
g~~ Invert case
vEU Switch word to uppercase
vE~ Modify word case
ggguG Set all text to lowercase
gggUG Set all text to uppercase
:set ignorecase Ignore case in searches
:set smartcase Ignore case in searches excepted if an uppercase letter is used
:%s/\<./\u&/g Sets first letter of each word to uppercase
:%s/\<./\l&/g Sets 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 outfile Saves lines 1 to 10 in outfile
:1,10 w >> outfile Appends lines 1 to 10 to outfile
:r infile Insert the content of infile
:23r infile Insert the content of infile under line 23

文件瀏覽器

:e . Open integrated file explorer
:Sex Split window and open integrated file explorer
:Sex! Same as :Sex but split window vertically
:browse e Graphical file explorer
:ls List buffers
:cd .. Move to parent directory
:args List files
:args *.php Open file list
:grep expression *.php Returns a list of .php files contening expression
gf Open file name under cursor

和 Unix 系統交互

:!pwd Execute the pwd unix command, then returns to Vi
!!pwd Execute the pwd unix command and insert output in file
:sh Temporary returns to Unix
$exit Retourns to Vi

對齊

:%!fmt Align all lines
!}fmt Align all lines at the current position
5!!fmt Align the next 5 lines

Tabs/Windows

:tabnew Creates a new tab
gt Show next tab
:tabfirst Show first tab
:tablast Show last tab
:tabm n(position) Rearrange tabs
:tabdo %s/foo/bar/g Execute a command in all tabs
:tab ball Puts all open files in tabs
:new abc.txt Edit abc.txt in new window

分屏顯示

:e filename Edit filename in current window
:split filename Split the window and open filename
ctrl-w up arrow Puts cursor in top window
ctrl-w ctrl-w Puts cursor in next window
ctrl-w_ Maximize current window vertically
ctrl-w| Maximize current window horizontally
ctrl-w= Gives the same size to all windows
10 ctrl-w+ Add 10 lines to current window
:vsplit file Split window vertically
:sview file Same as :split in readonly mode
:hide Close current window
:­nly Close all windows, excepted current
:b 2 Open #2 in this window

自動完成

Ctrl+n Ctrl+p (in insert mode) Complete word
Ctrl+x Ctrl+l Complete line
:set dictionary=dict Define dict as a dictionnary
Ctrl+x Ctrl+k Complete with dictionnary

Marks

m {a-z} Marks current position as {a-z}
' {a-z} Move to position {a-z}
'' Move to previous position

縮寫

:ab mail [email protected] Define mail as abbreviation of [email protected]

文本縮進

:set autoindent Turn on auto-indent
:set smartindent Turn on intelligent auto-indent
:set shiftwidth=4 Defines 4 spaces as indent size
ctrl-t, ctrl-d Indent/un-indent in insert mode
>> Indent
<< Un-indent
=% Indent the code between parenthesis
1GVG= Indent the whole file

語法高亮

:syntax on Turn on syntax highlighting
:syntax off Turn off syntax highlighting
:set syntax=perl Force syntax highlighting

原文鏈接:http://www.catswhocode.com/blog/130-essential-vim-commands

譯文鏈接:http://www.oschina.net/news/43167/130-essential-vim-commands


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 待續 ...
  • 作為Linux的系統管理者,有必要對系統的安全負責任,每個人對系統的操作都要有所監控,才能保證系統安全的運行,下麵就說一下單機如何添加日誌審計,有效獲取其他人對系統的操作行為: 1,首先要查看有沒有安裝sudo 和rsyslog服務 2,如果沒查詢到服務,可用yum安裝相關服務 3 ,/etc/rs ...
  • 簡介 linux系統有個讓人蛋疼的通病,軟體包依賴,好在當前主流的兩大發行版本都自帶瞭解決方案,Red hat有yum,Ubuntu有apt-get Homebrew簡稱brew,是Mac OSX上的軟體包管理工具,能在Mac中方便的安裝軟體或者卸載軟體,可以說Homebrew就是mac下的apt- ...
  • 一、通過yum命令線上安裝jdk 1.查看雲端目前支持安裝的jdk版本 2.選擇版本後,安裝(執行以下命令會自動安裝jdk相關依賴) 3.安裝完成,驗證是否安裝成功 4.通過搜索java文件,查找jdk預設安裝目錄 提示:通過yum命令線上安裝jdk簡單、快捷、無需配置環境變數即可使用java相關服 ...
  • 1.為何選擇Debian 作為一般用戶,系統是拿來用的,普及、穩定是首選,流行版本Ubuntu基於Debian,Mint基於Ubuntu,國產Deepin基於Debian。 2.獲取和安裝 Debian最新的穩定版是 9.3,代號stretch。最近一次更新於2017年12月09日。 訪問https ...
  • 解決辦法 在Windows下運行最新版的Apache和php7都需要Visual C++Redistributable 2015,而之前的版本不需要那麼高的,這個組件是運行Visual Studio 2015所建立的C++應用的必要組件,安裝一次即可解決環境問題,那麼去微軟官網下載安裝程式,安裝一遍 ...
  • 1.關機重啟命令 (1)shutdown命令:shutdown [選項] 時間 選項:-c 取消前一個關機命令 -h 關機 -r 重啟 例:shutdown -h now #立刻關機,root用戶 +10& #10分鐘後關機 20:30& #定義絕對重啟時間 (2)其他關機命令(不一定正確保存):h ...
  • Mac OS X一直能夠讀取NTFS驅動器,但隱藏在Mac OS X中是隱藏的選項,以支持格式化為NTFS的驅動器(NTFS代表新技術文件系統,是Microsoft Windows專有的文件系統格式)。在Mac上啟用NTFS寫入支持是有相當技術性的,並沒有得到蘋果公司的正式支持,因此它是一個實驗性的 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...