跳轉到內容

Vim

25% developed
來自華夏公益教科書
  • $ vim xxx yyy zzz - 開啟一個或多個檔案
  • (-R: 作為只讀開啟; +n: 跳到第 n 行; +/xxx: 跳到字串 xxx;)

vim . - 瀏覽當前目錄

模式

esc - 轉到命令模式

i,I,a,A,o,O - 轉到插入模式

v,V,Ctrl+v - 轉到可視模式

冒號命令

w - 儲存

w myfile - 儲存為 myfile

q - 退出

r myfile - 在當前檔案的遊標處貼上 myfile

e myfile - 編輯 myfile,關閉當前檔案

f - 顯示檔案屬性

n - 在引數列表中開啟下一個 vi 檔案

!xxx - 在 shell 中執行 xxx

!!xxx - 將當前行替換為 shell 命令 xxx 的結果

!}xxx - 將當前段落替換為 shell 命令 xxx 的結果

map - 顯示鍵對映

map key cmd - 將 vi 命令對映到鍵

. - 顯示行號

set nonumber - 關閉行號

s/old/new/ - 搜尋和替換

%s/search/&/g - 搜尋並計算匹配項

1,$ s/old/new/g - 全域性替換

g/^$/d - 刪除所有空行

/xxx - 向下搜尋 xxx

?xxx - 向上搜尋 xxx

搜尋命令

/ - 正則表示式搜尋

n - 重複上次搜尋(查詢下一個)

/↑ - 編輯上次搜尋

控制命令

Ctrl-b - 上翻頁

Ctrl-f - 下翻頁

Ctrl-u - 半頁上翻

Ctrl-d - 半頁下翻

編輯

a - 在遊標後追加

A - 在當前行後追加

i - 在遊標前插入

r - 替換遊標處的字元

~ - 轉置遊標處文字的大小寫

R - 從遊標處替換到新文字的末尾

o - 在遊標後開啟新行

O - 在遊標前開啟新行

u - 撤銷上次操作

Ctrl+R - 重做上次操作

U - 撤銷當前行

yy - 複製當前行

nyy - 從當前行復制 n 行

y/string/ 從遊標位置複製到字串的位置,向前搜尋

y?string? 從字串的位置複製到遊標,向後搜尋

p - 貼上

ddp - 剪下並貼上當前行

dd - 刪除當前行

dw - 刪除當前單詞

d$ - 從遊標刪除到行尾

D - 從遊標刪除到行尾

cc - 更改當前行

cw - 更改當前單詞

c$ - 從遊標更改到行尾

C - 從遊標更改到行尾

s - 更改當前符號

S - 更改當前行

游標移動

h/j/k/l - 向左/向下/向上/向右移動

w - 按單詞向前移動

b - 按單詞向後移動

e - 移動到當前單詞的末尾

W - 與 w 相同,但僅使用空格作為分隔符

B - 與 b 相同,但僅使用空格作為分隔符

E - 與 e 相同,但僅使用空格作為分隔符

} - 向前移動一個段落

{ - 向後移動一個段落

) - 向前移動一行

( - 向後移動一行

^ - 第一個非空白字元

0 - 當前行的開頭

$ - 當前行的末尾

gg - 檔案的頂部

G - 檔案的底部

ngg - 轉到第 n 行

nG - 與 ngg 相同

% - 查詢遊標中當前括號的匹配項


儲存會話

   :mksession

在啟動 vim 時載入會話

   $ vim -S Session.vim

在 vim 中載入會話

   :so Session.vim

從命令列重新制表一個或多個檔案

   $ vim -c retab -c wq <filename(s)>


Wscript 在 ~/.vimrc 中包含以下幾行,避免將檔案型別設定為 python

   1 autocmd BufRead,BufNewFile wscript set filetype = python

清除突出顯示的搜尋詞

   :let @/=""

將縮排的程式碼從另一個應用程式貼上到 vim 中

   :set paste
   <Terminal paste command>
   :set nopaste

將終端輸出重定向到 Vim

   repoquery  -lq *gstreamer* |vim -

寫入並退出檔案

   Shift + zz

從其他檔案讀取文字塊

   :r! sed -n 12,40p filename

這將從當前檔案中的第 12 行到第 40 行貼上行內容

儲存需要 sudo 許可權的檔案,無需退出檔案

   :w !sudo tee %
     You can add following line to your .vimrc to make this trick easy-to-use: just type :w!!.
   cmap w!! %!sudo tee > /dev/null %

自動換行

   set wrap

自動縮排

   Select the block(visual mode) you want to auto indent and just press "=" button.

將檔案複製到當前位置

   eg: 44t.

將一行移動到另一個位置

   eg: 4m5

縮排一行

   shift + >>
   shift + <<

刪除一個塊

   eg: 1,10 d

設定/取消設定行號

   :set nu
   :set nonu

檢視/隱藏 vim 中的特殊字元

   :set list
   :set nolist




必須使用 Vim 外掛

NERDTree


更有效地開啟和編輯您的檔案。Nerd Commenter

   Comment or un-comment multiple lines with keys.This plugins detects the file type and apply's the correct comment syntax.It even supports Vim's visual mode.

Taglist


   List and jump to functions quickly.This requires that you have ctags installed .    
華夏公益教科書