vi編輯器中的匹配搜尋操作
玩轉VIM編輯器-強力搜尋
搜尋導航
導航鍵 |
描述 |
/ |
向前搜尋,前向 |
? |
向後搜尋,反向 |
n |
跳轉到下一個搜尋詞所在位置 |
N |
跳轉到前一個搜尋詞所在位置 |
//或?? |
重複前面的搜尋或反向搜尋 |
跳轉到下一個/上一個當前游標所在的字元
使用該技巧可以不用使用/pattern可以直接跳轉到游標所在的字元。
導航鍵 |
描述 |
* |
跳轉到游標所在字元的下一個匹配處 |
# |
跳轉到游標所在字元的上一個匹配處 |
上面的搜尋方式是精確匹配的,比如游標在字元hell上,就不會搜尋到hello上,但是我們可以使用下面描述的可以使得游標在hell上時,也能搜尋到hello或者shell等包含hell的單次。
導航鍵 |
描述 |
g* |
跳轉到下一個區域性匹配的單詞處 |
g# |
跳轉到上一個區域性匹配的單詞處 |
PS:可以使用[I來列出所有符號游標處單次的行。
在一行中搜索字元
導航鍵 |
描述 |
fX |
在一行中向前搜尋字元X並跳轉 |
FX |
在一行中向後搜尋字元X並跳轉 |
tX |
在一行中向前搜尋字元X並跳轉到X前面一個字元 |
TX |
*****沒有測試出來 |
; |
向前重複上述操作 |
, |
向後重複上述操作 |
12個實用的強力搜尋和替換例項
Vim編輯器中文字替換命令的語法為:
:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
其中有3個可能的flags,分別為:
•[c]每次替換都確定一下
•[g]替換當前行中所有符合的
•[i] 忽略模式匹配的大小寫
1替換整個檔案中所有符合模式的文字
其中%s指定了所有的行,而其中%的含義為替換整個檔案;
其中g的含義為指定行中所有的匹配項,如果沒有g將只是替換行中發現的第一個。
2在一行中替換文字
:s/old-text/new-text/gi
上述命令中沒有指定range範圍,將預設為當前行。
3在指定行中替換文字
:1,10s/I/We/g
上述命令將把從第1行到第10行的I替換為We。
4在指定塊中替換文字
在使用CTRL+V時,可以替換選定的文字,在輸入:時,就會自動變為:’<,’>,這是就可以輸入命令了:
:'<,'>s/helo/hello/g
該命令將把指定塊中的helo替換為hello。
5在接下來的N行中替換文字
:s/helo/hello/g N
該命令將替換從當前行開始的共N行。
6只替換整個單詞而不是部分匹配
標準替換:
原始文字: This is his idea
:s/his/her/g
替換文字:Ther is her idea
完整單詞替換:
原始文字: This is his idea
:s/\<his\>/her/
替換文字: This is her idea
所以,如果需要完整替換,需要在關鍵詞前後新增<>,對於一些新手而言,可能會希望使用空格來代替,殊不知,這樣可能會導致開始和結束的字元不符合條件。
7使用正則表示式來替換多個文字
原始文字: Linux is good. Life is nice.
:%s/\(good\|nice\)/awesome/g
替換文字: Linux is awesome. Life is awesome.
8互動式地查詢替換
可以在命令列中使用c-flag來互動式地查詢替換,比如
:%s/awesome/wonderful/gc
將會提示如下資訊:
replace with wonderful (y/n/a/q/l/^E/^Y)?
•y:替換當前高亮顯示的單詞,並高亮顯示下一個匹配的詞;
•n:不替換當前高亮顯示的單詞,然後高亮顯示下一個匹配的詞;
•a:將不再出現提示而替換掉所有剩下匹配的詞;
•l:替換當前高亮顯示的詞,並且中斷操作。
9將行號寫到檔案中
:%s/^/\=line(".") . ". "/g
該命令將會把顯示的行號寫到檔案中去。這個與:set number不同,set number只是顯示行數而不會對檔案內容有所影響。
10使用等價值來替換一個特殊字元
原始文字: Current file path is ~/test/
:%s!\~!\= expand($HOME)!g
替換文字: Current file path is /home/leo/test/
11在插入一個新的條目時更改所有其他的條目
比如我們有下面的圖書列表:
Article 1: Vi and Vim Editor: 3 Steps To Enable Thesaurus Option
Article 2: Vim Autocommand: 3 Steps to Add Custom Header To Your File
Article 3: 5 Awesome Examples For Automatic Word Completion Using CTRL-X
Article 4: Vi and Vim Macro Tutorial: How To Record and Play
Article 5: Tutorial: Make Vim as Your C/C++ IDE Using c.vim Plugin
Article 6: How To Add Bookmarks Inside Vim Editor
Article 7: Make Vim as Your Bash-IDE Using bash-support Plugin
Article 8: 3 Powerful Musketeers Of Vim Editor ? Macro, Mark and Map
Article 9: 8 Essential Vim Editor Navigation Fundamentals
Article 10: Vim Editor: How to Correct Spelling Mistakes Automatically
Article 11: Transfer the Power of Vim Editor to Thunderbird for Email
Article 12: Convert Vim Editor to Beautiful Source Code Browser
忘記了3rd Article “Make Vim as Your Perl IDE Using perl-support.vim Plugin” ,現在我們要做的就是插入3rd Article “Make Vim as Your Perl IDE Using perl-support.vim Plugin”並且需要把後面的數目需要逐次增加1。
那麼我們可以使用命令:
:4,$s/\d\+/\=submatch(0) + 1/
其中:
範圍:4,$為從第4行到最後;
\d\表示一個數字的字串;
\=submatch(0) + 1 取到數字並且加1;
由於這裡沒有flag,所以預設只會替換第一個吻合的單詞。
更換後變為:
Article 1: Vi and Vim Editor: 3 Steps To Enable Thesaurus Option
Article 2: Vim Autocommand: 3 Steps to Add Custom Header To Your File
Article 3: Make Vim as Your Perl IDE Using perl-support.vim Plugin
Article 4: 5 Awesome Examples For Automatic Word Completion Using CTRL-X
Article 5: Vi and Vim Macro Tutorial: How To Record and Play
Article 6: Tutorial: Make Vim as Your C/C++ IDE Using c.vim Plugin
Article 7: How To Add Bookmarks Inside Vim Editor
Article 8: Make Vim as Your Bash-IDE Using bash-support Plugin
Article 9: 3 Powerful Musketeers Of Vim Editor ? Macro, Mark and Map
Article 10: 8 Essential Vim Editor Navigation Fundamentals
Article 11: Vim Editor: How to Correct Spelling Mistakes Automatically
Article 12: Transfer the Power of Vim Editor to Thunderbird for Email
Article 13: Convert Vim Editor to Beautiful Source Code Browser
12將每個句子的首字母大寫
:%s/\.\s*\w/\=toupper(submatch(0))/g
該命令將把每個句子的首字母大寫。
\.\s*\w --搜尋模式,在句點後跟零個或多個空格,然後一個單詞;
toupper – 將給定的文字改為大寫字母;
submatch(0) –返回匹配的模式
使用vimgrep在多個檔案中搜索
:vimgrep leo *.txt --可以使用該命令在當前所有txt檔案中搜索leo,如果有多個匹配項,可以使用:cn來跳轉到下一個。
命令 |
描述 |
:vimgrep pattern * |
在多個檔案中搜索pattern |
:cn |
跳轉到下一個vimgrep搜尋到的項 |
:cN |
跳轉到上一個vimgrep搜尋到的項 |
:clist |
列出所有vimgrep搜尋到的項 |
:cc number |
直接跳轉到number對應的項 |
高亮顯示搜尋結果
:set hlsearch 該選項將高亮顯示搜尋的結果
:set nohlsearch 該選項將不高亮顯示搜尋的結果
:nohlsearch 清除高亮顯示的結果
Vim增量搜尋
:set incsearch該增量搜尋模式將在你輸入字元的時候就立即定位符合你輸入的pattern,同樣禁用該選項為:set noincsearch
給力的:match
:match ErrorMsg /Error/
將檔案中所有的Error用ErrorMsg配色方案來顯示;
比如::match ErrorMsg /printf/將顯示如下:
其中,配色方案主要有:
•ErrorMsg
•WarningMsg
•ModeMsg
•MoreMsg
關於配色方案,我們也可以自己設定。