vim 經常使用 NERDTree 快捷鍵

ctrl + w + h 光標 focus 左側樹形目錄
ctrl + w + l 光標 focus 右側文件顯示窗口
ctrl + w + w 光標自動在左右側窗口切換
ctrl + w + r 移動當前窗口的佈局位置php

o 在已有窗口中打開文件、目錄或書籤,並跳到該窗口
go 在已有窗口 中打開文件、目錄或書籤,但不跳到該窗口
t 在新 Tab 中打開選中文件/書籤,並跳到新 Tab
T 在新 Tab 中打開選中文件/書籤,但不跳到新 Tab
i split 一個新窗口打開選中文件,並跳到該窗口
gi split 一個新窗口打開選中文件,但不跳到該窗口
s vsplit 一個新窗口打開選中文件,並跳到該窗口
gs vsplit 一個新 窗口打開選中文件,但不跳到該窗口
! 執行當前文件
O 遞歸打開選中 結點下的全部目錄
x 合攏選中結點的父目錄
X 遞歸 合攏選中結點下的全部目錄
e Edit the current dif
雙擊 至關於 NERDTree-o
中鍵 對文件至關於 NERDTree-i,對目錄至關於 NERDTree-e
D 刪除當前書籤
P 跳到根結點
p 跳到父結點
K 跳到當前目錄下同級的第一個結點
J 跳到當前目錄下同級的最後一個結點
k 跳到當前目錄下同級的前一個結點
j 跳到當前目錄下同級的後一個結點
C 將選中目錄或選中文件的父目錄設爲根結點
u 將當前根結點的父目錄設爲根目錄,並變成合攏原根結點
U 將當前根結點的父目錄設爲根目錄,但保持展開原根結點
r 遞歸刷新選中目錄
R 遞歸刷新根結點
m 顯示文件系統菜單
cd 將 CWD 設爲選中目錄
I 切換是否顯示隱藏文件
f 切換是否使用文件過濾器
F 切換是否顯示文件
B 切換是否顯示書籤
q 關閉 NerdTree 窗口
? 切換是否顯示 Quick Helphtml

切換標籤頁python

:tabnew [++opt選項] [+cmd] 文件 創建對指定文件新的tab
:tabc 關閉當前的 tab
:tabo 關閉全部其餘的 tab
:tabs 查看全部打開的 tab
:tabp 前一個 tab
:tabn 後一個 tab
標準模式下:
gT 前一個 tab
gt 後一個 tab
MacVim 還能夠藉助快捷鍵來完成 tab 的關閉、切換
cmd+w 關閉當前的 tab
cmd+{ 前一個 tab
cmd+} 後一個 tablinux

NerdTree 在 .vimrc 中的經常使用配置git

" 在 vim 啓動的時候默認開啓 NERDTree(autocmd 能夠縮寫爲 au)
autocmd VimEnter * NERDTree
" 按下 F2 調出/隱藏 NERDTree
map :silent! NERDTreeToggle
" 將 NERDTree 的窗口設置在 vim 窗口的右側(默認爲左側)
let NERDTreeWinPos="right"
" 當打開 NERDTree 窗口時,自動顯示 Bookmarks
let NERDTreeShowBookmarks=1shell


5、查找替換
/pattern     向後搜索字符串patternexpress

?pattern     向前搜索字符串patternvim

"\c" 忽略大小寫windows

 "\C" 大小寫敏感
n             下一個匹配(若是是/搜索,則是向下的下一個,?搜索則是向上的下一個)
N             上一個匹配(同上)
:%s/old/new/g     搜索整個文件,將全部的old替換爲new
:%s/old/new/gc     搜索整個文件,將全部的old替換爲new,每次都要你確認是否替換安全

6、退出編輯器
:w             將緩衝區寫入文件,即保存修改
:wq         保存修改並退出
:x             保存修改並退出
:q             退出,若是對緩衝區進行過修改,則會提示
:q!         強制退出,放棄修改

 

鍵盤移動 (Move)

一切都從鍵盤的移動
k -> 上 up
j -> 下 down
h -> 左 left
l -> 右 right
z -> 重畫屏幕,當前光標變成屏幕的第一行 (redraw current line at top of window)
CTRL-f -> 跳到下一頁 (page down)
CTRL-b -> 跳到上一頁 (page up)
跳躍指令 (jumps)

跳躍指令相似於遊覽器中的<前進><後退>按鈕
CTRL-] -> 跟着link/tag轉入 (follow link/tag)
CTRL-o -> 回到上一次的jump (go back)
CTRL-i -> 跳回下一個 (go forward)
:ju -> 顯示全部的能夠跳躍的地方 (print jump list)
重作/回覆

u -> undo
CTRL-r -> redo
vim的undo是樹結構的,你能夠回到這個結構中的任何地方
:undo 2 -> undo 到結構的2層 (undo to tree 2)
:undolist -> 顯示全部的undo列表 (show undo list)
:earlier 10s -> undo到10秒前的編輯 (undo to 10 seconds ago)
:earlier 10h -> undo到10小時前的編輯 (back to 10 hours ago)
:earlier 1m -> undo到1分鐘前 (back to 1 minutes ago)
下面是undo的tree結構的解釋
………..one
…………. |
……..change 1
…………. |
………one too
………. /……..\
…..change 2 ……. change 3
………… | ………………… |
…….one two ………. me too
……….. |
….. change 4
………..|
…… not two
視覺模式 (visual)

v -> 進入視覺模式
在視覺模式內能夠做block的編輯
CTRL-v -> visual block
打印 (print)

:hardcopy -> 打印vim中的內容 (print text)
混合視覺模式 (visual) 能夠選擇打印的區域
沒試過是否能夠直接給值打印(應該能夠)例如 :1,15hardcopy 打印前15行
將文件寫成網頁格式 (html)

:source $VIMRUNTIME/syntax/2html.vim -> change current open file to html
格式 (format)

dos/windows跟unix/linux對於文件的結束是不同的。vim能夠直接設定/更改格式
用紙令:set fileformats=unix,dos 能夠改變文件的格式 (change format)

:set ff=unix -> 設定文件成unix格式 (set file in unix format)
:set ff=dos -> 設定文件成dos格式 (set file in dos format)
:set ff? -> 檢查當前文件格式 (check the format of current file)
若是改變格式,直接:w存檔就會存成新的格式了。
加密 (encryption)

vim能夠給文件加密碼
vim -x 文件名 (filename) -> 輸入2次密碼,保存後文件每次都會要密碼才能進入 (encrypt the file with password)
vim 處理加密文件的時候,並不會做密碼驗證,也就是說,當你打開文件的時候,vim無論你輸入的密碼是否正確,直接用密碼對本文進行解密。若是密碼錯誤,你看 到的就會是亂碼,而不會提醒你密碼錯誤(這樣增長了安全性,沒有地方能夠得知密碼是否正確)固然了,若是用一個夠快的機器做窮舉破解,vim仍是能夠揭開 的
vim 語法顯示 (syntax)

:syntax enable -> 打開語法的顏色顯示 (turn on syntax color)
:syntax clear -> 關閉語法顏色 (remove syntax color)
:syntax off -> 徹底關閉所有語法功能 (turn off syntax)
:syntax manual -> 手動設定語法 (set the syntax manual, when need syntax use :set syntax=ON)
輸入特殊字符 (special character)

CTRL-v 編碼就能夠了
例如 CTRL-v 273 -> ÿ 獲得 ÿ
二進 制文件 (binary file)

vim能夠顯示,編輯2進位文件

vim -b datafile 
:set display=uhex -> 這樣會以uhex顯示。用來顯示一些沒法顯示的字符(控制字符之類)(display in uhex play non-display char)

:%!xxd -> 更改當前文件顯示爲2進位 (change display to binary)
:%!xxd -r -> 更改二進位爲text格式 (convert back to text)
自動完成 (auto-completion)

vim自己有自動完成功能(這裏不是說ctag,而是vim內建的)
CTRL-p -> 向後搜索自動完成 (search backward)
CTRL-n -> 向前搜索自動完成 (search forward)
CTRL-x+CTRL-o -> 代碼自動補全 (code completion)
自動備份 (backup)

vim能夠幫你自動備份文件(儲存的時候,以前的文件備份出來)
:set backup -> 開啓備份,內建設定備份文件的名字是 源文件名加一個 ‘~’ (enable backup default filename+~)
:set backupext=.bak -> 設定備份文件名爲源文件名.bak (change backup as filename.bak)

自動備份有個問題就是,若是你屢次儲存一個文件,那麼這個你的備份文件會被不斷覆蓋,你只能有最後一次存文件以前的那個備份。不要緊,vim還提 供了patchmode,這個會把你第一次的原始文件備份下來,不會改動
:set patchmode=.orig -> 保存原始文件爲 文件名.orig (keep orignal file as filename.orig)
開啓,保存與退出 (save & exit)

:w -> 保存文件 (write file)
:w! -> 強制保存 (force write)
:q -> 退出文件 (exit file without save)
:q! -> 強制退出 (force quite without save)
:e filename -> 打開一個文件名爲filename的文件 (open file to edit)
:e! filename -> 強制打開一個文件,全部未保存的東西會丟失 (force open, drop dirty buffer)
:saveas filename -> 另存爲 filename (save file as filename)
編輯指令 (edit)

a -> 在光表後插入 (append after cursor)
A -> 在一行的結尾插入 (append at end of the line)
i -> 在光標前插入 (insert before cursor)
I -> 在第一個非空白字符前插入 (insert before first non-blank)
o -> 光標下面插入一個新行 (open line below)
O -> 光標上面插入一個新行 (open line above)
x -> 刪除光標下(或者以後)的東西 (delete under and after cursor)
例如x就是刪除當前光標下,3x就是刪除光標下+光標後2位字符
X -> 刪除光標前的字符 (delete before cursor)
d -> 刪除 (delete)
能夠用dd刪除一行,或者3dw刪除3個詞等等
J -> 將下一行提到這行來 (join line)
r -> 替換個字符 (replace characters)
R -> 替換多個字符 (replace mode – continue replace)
gr -> 不影響格局佈置的替換 (replace without affecting layout)
c -> 跟d鍵同樣,可是刪除後進入輸入模式 (same as 「d」 but after delete, in insert mode)
S -> 刪除一行(好像dd同樣)可是刪除後進入輸入模式 (same as 「dd」 but after delete, in insert mode)
s -> 刪除字符,跟(d)同樣,可是刪除後進入輸入模式 (same as 「d」 but after delete, in insert mode)
s4s 會刪除4個字符,進入輸入模式 (delete 4 char and put in insert mode)
~ -> 更改大小寫,大變小,小變大 (change case upper-> lower or lower->upper)
gu -> 變成小寫 (change to lower case)
例如 guG 會把光標當前到文件結尾所有變成小寫 (change lower case all the way to the end)
gU -> 變成大寫 (change to upper case)
例如 gUG 會把光標當前到文件結尾所有變成大寫 (change upper case all the way to the end)
複製與粘貼 (copy & paste)

y -> 複製 (yank line)
yy -> 複製當前行 (yank current line)
「{a-zA-Z}y -> 把信息複製到某個寄存中 (yank the link into register {a-zA-Z})
例如我用 「ayy 那麼在寄存a,就複製了一行,而後我再用「byw複製一個詞在寄存b
粘貼的時候,我能夠就能夠選擇貼a裏面的東西仍是b裏面的,這個就好像是多個複製版同樣
「*y -> 這個是把信息複製進系統的複製版(能夠在其餘程序中貼出來)(yank to OS buffer)
p -> 當前光標下粘貼 (paste below)
P -> 當前光標上粘貼 (paste above)
「{a-zA-Z}p -> 將某個寄存的內容貼出來 (paste from register)
例如「ap那麼就在當前光標下貼出我以前在寄存a中 的內容。「bP就在當前光標上貼出我以前寄存b的內容
「*p -> 從系統的剪貼板中讀取信息貼入vim (paste from OS buffer to vim)
reg -> 顯示全部寄存中的內容 (list all registers)
書籤 (Mark)

書籤是vim中很是強大的一個功能,書籤分爲文件書籤跟全局書籤。文件書籤是你標記文件中的不一樣位置,而後能夠在文件內快速跳轉到你想要的位置。 而全局書籤是標記不一樣文件中的位置。也就是說你能夠在不一樣的文件中快速跳轉

m{a-zA-Z} -> 保存書籤,小寫的是文件書籤,能夠用(a-z)中的任何字母標記。大寫的是全局 書籤,用大寫的(A-Z)中任意字母標記。(mark position as bookmark. when lower, only stay in file. when upper, stay in global)
‘{a-zA-Z} -> 跳轉到某個書籤。若是是全局書籤,則會開啓被書籤標記的文件跳轉至標記的行 (go to mark. in file {a-z} or global {A-Z}. in global, it will open the file)
’0 -> 跳轉入如今編輯的文件中上次退出的位置 (go to last exit in file)
」 -> 跳轉如最後一次跳轉的位置 (go to last jump -> go back to last jump)
‘」 -> 跳轉至最後一次編輯的位置 (go to last edit)
g’{mark} -> 跳轉到書籤 (jump to {mark})
:delm{marks} -> 刪除一個書籤 (delete a mark) 例如:delma那麼就刪除了書籤a
:delm! -> 刪除所有書籤 (delete all marks)
:marks -> 顯示系統所有書籤 (show all bookmarks)
標誌 (tag)

:ta -> 跳轉入標誌 (jump to tag)
:ts -> 顯示匹配標誌,而且跳轉入某個標誌 (list matching tags and select one to jump)
:tags -> 顯示全部標誌 (print tag list)
運行外部命令 (using an external program)

:! -> 直接運行shell中的一個外部命令 (call any external program)
:!make -> 就直接在當前目錄下運行make指令了 (run make on current path)
:r !ls -> 讀取外部運行的命令的輸入,寫入固然vim中。這裏讀取ls的輸出 (read the output of ls and append the result to file)
:3r !date -u -> 將外部命令date -u的結果輸入在vim的第三行中 (read the date -u, and append result to 3rd line of file)

:w !wc -> 將vim的內容交給外部指令來處理。這裏讓wc來處理vim的內容 (send vim’s file to external command. this will send the current file to wc command)
vim對於經常使用指令有一些內建,例如wc (算字數)(vim has some buildin functions, such like wc)
g CTRL-G -> 計算當前編譯的文件的字數等信息 (word count on current buffer)
!!date -> 插入當前時間 (insert current date)
多個文件的編輯 (edit multifiles)

vim能夠編輯多個文件,例如
vim a.txt b.txt c.txt 就打開了3個文件

:next -> 編輯下一個文件 (next file in buffer)
:next! -> 強制編輯下個文件,這裏指若是更改了第一個文件 (force to next file in buffer if current buffer changed)
:wnext -> 保存文件,編輯下一個 (save the file and goto next)
:args -> 查找目前正在編輯的文件名 (find out which buffer is editing now)
:previous -> 編輯上個文件 (previous buffer)
:previous! -> 強制編輯上個文件,同 :next! (force to previous buffer, same as :next!)
:last -> 編輯最後一個文件 (last buffer)
:first -> 編輯最前面的文件 (first buffer)
:set autowrite -> 設定自動保存,當你編輯下一個文件的時候,目前正在編輯的文件若是改動,將會自動保存 (automatic write the buffer when you switch to next buffer)
:set noautowrite -> 關閉自動保存 (turn autowrite off)
:hide e abc.txt -> 隱藏當前文件,打開一個新文件 abc.txt進行編輯 (hide the current buffer and edit abc.txt)
:buffers -> 顯示全部vim中的文件 (display all buffers)
:buffer2 -> 編輯文件中的第二個 (edit buffer 2)

vim中不少東西能夠用簡稱來寫,就不用打字那麼麻煩了,例如 :edit=:e, :next=:n 這樣.
分屏 (split)

vim提供了分屏功能(跟screen裏面的split同樣)
:split -> 將屏幕分紅2個 (split screen)
:split abc.txt -> 將屏幕分紅兩個,第二個新的屏幕中顯示abc.txt的內容 (split the windows, on new window, display abc.txt)
:vsplit -> 豎着分屏 (split vertically)
:{d}split -> 設定分屏的行數,例如我要一個屏幕只有20行,就能夠下:20split (split the windows with {d} line. 20split: open new windows with 3 lines)
:new -> 分屏而且在新屏中創建一個空白文件 (split windows with a new blank file)
CTRL-w+j/k/h/l -> 利用CTRL加w加上j/k/h/l在不一樣的屏內切換 (switch, move between split screens)
CTRL-w+ -/+ -> 增減分屏的大小 (change split size)
CTRL-w+t -> 移動到最頂端的那個屏 (move to the top windows)
CTRL-w+b -> 移動到最下面的屏 (move to bottom window)
:close -> 關閉一個分出來的屏 (close splited screen)
: only -> 只顯示光標當前屏 ,其餘將會關閉(only display current active screen, close all others )
:qall -> 退出全部屏 (quite all windows)
:wall -> 保存全部屏 (write to all windows)
:wqall -> 保存並退出全部屏 (write and quite all windows)
:qall! -> 退出全部屏,不保存任何變更 (quite all windows without save)
開啓文件的時候,利用 -o選項,就能夠直接開啓多個文件在分屏中 (with -o option from command line, it will open files and display in split mode)
vim -o a.txt b.txt

今天有人說不會看diff,其實vim也能夠用來看diff,這個也是屬於分屏的部分,這裏也寫一下。

vimdiff a.txt b.txt 若是直接給 -d選項是同樣的 vim -d a.txt b.txt
:diffsplit abc.txt 若是你如今已經開啓了一個文件,想vim幫你區分你的文件跟abc.txt有什麼區別,能夠在vim中用diffsplit的方式打開第二個文件,這個時 候vim會用split的方式開啓第二個文件,而且經過顏色,fold來顯示兩個文件的區別
這樣vim就會用顏色幫你區分開2個文件的區別。若是文件比較大(源碼)重複的部分會幫你摺疊起來(摺疊後面會說)
如今來講patch
:diffpatch filename 經過:diffpatch 你的patch的文件名,就能夠以當前文件加上你的patch來顯示。vim會split一個新的屏,顯示patch後的信息而且用顏色標明區別。
若是不喜歡上下對比,喜歡左右(比較符合視覺)能夠在前面加vert,例如:
:vert diffsplit abc.txt
:vert diffpatch abc.txt
看完diff,用: only回到本來編輯的文件,覺 得diff的討厭顏色仍是在哪裏,只要用:diffoff關閉就行了。
還有個經常使用的diff中的就是 :diffu 這個是 :diffupdate 的簡寫,更新用
TAB

除了split以外, vim還能夠用 tab

:tab split filename -> 這個就用tab的方式來顯示多個文件 (use tab to display buffers)
gt -> 到下一個tab (go to next tab)
gT -> 到上一個tab (go to previous tab)
vim大多數東西都是可一給數字來執行的,tab也是同樣
0gt ->跳到第一個tab (switch to 1st tab)
5gt -> 跳到第五個tab (switch to 5th tab)
關閉全部的tab可使用qall的指令。另外讓vim在啓動的時候就自動用tabnew的方式來開啓多個文件,能夠用alias
linux: 添加 alias vim=’vim -p’ 到 ~/.bashrc
windows: 本身寫個vim.bat的文件,而後放在path中,文件內容:
@echo off
vim -p %*
當須要更改多個tab中的文件的時候,能夠用 :tabdo 這個指令 這個就至關於 loop 到你的全部的 tab 中而後運行指令。
例若有5個文件都在tab裏面,須要更改一個變量名稱:abc 到 def, 就能夠用 :tabdo %s/abc/def/g 這樣全部的5個tab裏面的abc就都變成def了
 
摺疊 (folding)

vim的摺疊功能。。。我記得應該是6版出來的時候才推出的吧。這個對於寫程序的人來講,很是有用。
zfap -> 按照段落摺疊 (fold by paragraph)
zo -> 打開一個摺疊 (open fold)
zc -> 關閉一個摺疊 (close fold)
zf -> 建立摺疊 (create fold) 這個能夠用v視覺模式,能夠直接給行數等等
zr -> 打開必定數量的摺疊,例如3rz (reduce the folding by number like 3zr)
zm -> 摺疊必定數量(以前你定義好的摺疊) (fold by number)
zR -> 打開全部的摺疊 (open all fold)
zM -> 關閉全部的摺疊 (close all fold)
zn -> 關閉摺疊功能 (disable fold)
zN -> 開啓摺疊功能 (enable fold)
zO -> 將光標下全部摺疊打開 (open all folds at the cursor line)
zC -> 將光標下全部摺疊關閉 (close all fold at cursor line)
zd -> 將光標下的摺疊刪除,這裏不是刪除內容,只是刪除摺疊標記 (delete fold at cursor line)
zD -> 將光標下全部摺疊刪除 (delete all folds at the cursor line)
按照tab來摺疊,python最好用的 (ford by indent, very useful for python)
:set foldmethod=indent -> 設定後用zm 跟 zr 就能夠的開關關閉了 (use zm zr)
保存 (save view)

對於vim來講,若是你設定了摺疊,可是退出文件,不論是否保持文件,摺疊部分會自動消失的。這樣來講很是不方便。因此vim給你方法去保存摺 疊,標籤,書籤等等記錄。最厲害的是,vim對於每一個文件能夠保存最多10個view,也就是說你能夠對同一個文件有10種不一樣的標記方法,根據你的需 要,這些東西都會保存下來。
:mkview -> 保存記錄 (save setting)
:loadview -> 讀取記錄 (load setting)
:mkview 2 -> 保存記錄在寄存2 (save view to register 2)
:loadview 3 -> 從寄存3中讀取記錄 (load view from register 3)
經常使用指令 (commands)

:set ic ->設定爲搜索時不區分大小 寫 (search case insensitive)
:set noic ->搜索時區分大小寫。 vim內定是這個(case sensitive )
& -> 重複上次的」:s」 (repeat previous 「:s」)
. -> 重複上次的指令 (repeat last command)
K -> 在man中搜索當前光標下的詞 (search man page under cursor)
{0-9}K -> 查找當前光標下man中的章節,例如5K就是同等於man 5 (search section of man. 5K search for man 5)
:history -> 查看命令歷史記錄 (see command line history)
q: -> 打開vim指令窗口 (open vim command windows)
:e -> 打開一個文件,vim能夠開啓http/ftp/scp的文件 (open file. also works with http/ftp/scp)
:e http://www.google.com/index.html -> 這裏就在vim中打開google的index.html (open google’s index.html)
:cd -> 更換vim中的目錄 (change current directory in vim)
:pwd -> 顯示vim當前目錄 (display pwd in vim)
gf -> 打開文件。例如你在vim中有一行寫了#include 那麼在abc.h上面按gf,vim就會把abc.h這個文件打開 (look for file. if you have a file with #include , then the cursor is on abc.h press gf, it will open the file abc.h in vim )
記錄指令 (record)

q{a-z} -> 在某個寄存中記錄指令 (record typed char into register)
q{A-Z} -> 將指令插入以前的寄存器 (append typed char into register{a-z})
q -> 結束記錄 (stop recording)
@{a-z} -> 執行寄存中的指令 (execute recording)
@@ -> 重複上次的指令 (repeat previours :@{a-z})
仍是給個例子來講明比較容易明白
我如今在一個文件中下qa指令,而後輸入itest而後ESC而後q
這裏qa就是說把個人指令記錄進a寄存,itest實際是分2步,i 是插入 (insert) 寫入的文字是 text 而後用ESC退回指令模式q結束記錄。這樣我就把itest記錄再一個寄存了。
下面我執行@a那麼就會自動插入test這個詞。@@就重複前一個動做,因此仍是等於@a
搜索 (search)

vim超級強大的一個功能就是搜索跟替換了。要是熟悉正表達(regular expressions)這個搜索跟後面的替換將會是無敵利器(支持RE的編輯器很少吧)

從簡單的提及
# -> 光標下反向搜索關鍵詞 (search the word under cursor backward)
* -> 光標下正向搜索關鍵詞 (search the word under cursor forward)
/ -> 向下搜索 (search forward)
? -> 向上搜索 (search back)
這裏能夠用 /abc 或 ?abc的方式向上,向下搜索abc
% -> 查找下一個結束,例如在」(「下查找下一個」)」,能夠找」()」, 「[]」 還有shell中經常使用的 if, else這些 (find next brace, bracket, comment or #if/#else/#endif)

下面直接用幾個例子說話
/a* -> 這個會搜到 a aa aaa
/\(ab\)* -> 這個會搜到 ab abab ababab
/ab\+ -> 這個會搜到 ab abb abbb
/folers\= -> 這個會搜到 folder folders
/ab\{3,5} -> 這個會搜到 abbb abbbb abbbbb
/ab\{-1,3} -> 這個會在abbb中搜到ab (will match ab in abbb)
/a.\{-}b -> 這個會在axbxb中搜到axb (match ‘axb’ in ‘axbxb’)
/a.*b -> 會搜索到任何a開頭後面有b的 (match a*b any)
/foo\|bar -> 搜索foo或者bar,就是同時搜索2個詞 (match ‘foo’ or ‘bar’)
/one\|two\|three -> 搜索3個詞 (match ‘one’, ‘two’ or ‘three’)
/\(foo\|bar\)\+ -> 搜索foo, foobar, foofoo, barfoobar等等 (match ‘foo’, ‘foobar’, ‘foofoo’, ‘barfoobar’ … )
/end\(if\|while\|for\) -> 搜索endif, endwhile endfor (match ‘endif’, ‘endwhile’, ‘endfor’)
/forever\&… -> 這個會在forever中搜索到」for」可是不會在fortuin中搜索到」for」 由於咱們這裏給了&…的限制 (match ‘for’ in ‘forever’ will not match ‘fortuin’)

特殊字符前面加^就能夠 (for special character, user 「^」 at the start of range)
/」[^"]*」
這裏解釋一下
」 雙引號先引發來 (double quote)
[^"] 任何不是雙引號的東西(any character that is not a double quote)
* 全部的其餘 (as many as possible)
」 結束最前面的引號 (double quote close)
上面那個會搜到「foo」 「3!x」這樣的包括引號 (match 「foo」 -> and 「3!x」 include double quote)

更多例子,例如搜索車牌規則,假設車牌是 「1MGU103」 也就是說,第一個是數字,3個大寫字幕,3個數字的格式。那麼咱們能夠直接搜索全部符合這個規則的字符
(A sample license plate number is 「1MGU103″. It has one digit, three upper case
letters and three digits. Directly putting this into a search pattern)
這個應該很好懂,咱們搜索
\數字\大寫字母\大寫字母\大寫字母\數字\數字\數字

/\d\u\u\u\d\d\d

另一個方法,是直接定義幾位數字(否則要是30位,難道打30個\u去?)
(specify there are three digits and letters with a count)

/\d\u\{3}\d\{3}

也能夠用範圍來搜索 (Using [] ranges)
/[0-9][A-Z]\{3}[0-9]\{3}

用到範圍搜索,列出一些範圍(range)
這個沒什麼好說了,看一下就都明白了,要所有記住。。。用的多了就記住了,用的少了就忘記了。每次看幫助,呵呵

/[a-z]
/[0123456789abcdef] = /[0-9a-f]
\e
\t
\r
\b
簡寫 (item matches equivalent)

\d digit [0-9]
\D non-digit [^0-9]
\x hex digit [0-9a-fA-F]
\X non-hex digit [^0-9a-fA-F]
\s white space [ ] ( and )
\S non-white characters [^ ] (not and )
\l lowercase alpha [a-z]
\L non-lowercase alpha [^a-z]
\u uppercase alpha [A-Z]
\U non-uppercase alpha [^A-Z]

:help /[] –> 特殊的定義的,能夠在vim中用用help來看 (everything about special)
:help /\s –> 普通的也能夠直接看一下 (everything about normal)
替換 (string substitute) – RX

替換其實跟搜索是同樣的。只不過替換是2個值,一個是你搜索的東西,一個是搜索到以後要替換的 string substitute (use rx)

%s/abc/def/ -> 替換abc到def (substitute abc to def)
%s/abc/def/c -> 替換abc到def,會每次都問你肯定(substitute on all text with confirmation (y,n,a,q,l))
1,5s/abc/def/g -> 只替換第一行到第15行之間的abc到def (substitute abc to def only between line 1 to 5)
54s/abc/def/ -> 只替換第54行的abc到def (only substitute abc to def on line 54)

結合上面的搜索正表達式,這個替換功能。。。就十分只強大。linux中不少地方都是用正表達來作事請的,因此學會了受益無窮。
全局 (global)

這個不知道怎麼翻譯,反正vim是叫作global,能夠對搜索到的東西執行一些vim的命令。我也是2-3個星期前由於讀log中一些特殊的東 西,才學會用的。 (find the match pater and execute a command)

global具體自行方法是 g/pattern/command
:g/abc/p -> 查找並顯示出只有abc的行 (only print line with 「abc」 )
:g/abc/d -> 刪除全部有abc的行 (delete all line with 「abc」)
:v/abc/d -> 這個會把凡是否是行裏沒有abc的都刪掉 (delete all line without 「abc」)
信息過濾 (filter)

vim又一強大功能

! -> 用!就是告訴vim,執行過濾流程 (tell vim to performing a filter operation)
!5G -> 從光標下向下5行執行過濾程序 (tell vim to start filter under cursor and go down 5 lines)

正式指令開始,這裏用sort來作例子:
!5Gsort -> 從光標下開始執行sort,一共執行5行,就是說我只要sort5行而已 (this will sort the text from cursor line down to 5 lines)
!Gsort -k3 -> 能夠直接代sort的參數,我要sort文字中的第三段 (sort to the end of file by column 3)
!! -> 值過濾當前的這行 (filter the current line)

若是以爲!這樣的方法5G這樣的方法用起來彆扭(我是這麼以爲),能夠用標準的命令模式來作!其實就是個:.,而已 (to type the command):.,start,end!sort 這裏定義:.,起始行,結束行!運行指令:.,$!sort -> 從當前這行一直執行至文件結束 (sort from current line to end):.0,$!sort -> 從文件的開始第一個行一直執行到文件結束 (sort from start of file to end):.10,15!sort -> 只在文件的第10行到第15行之間執行 (sort between line 10 to 15)

相關文章
相關標籤/搜索