Vim視窗常用指令

這篇會紀錄我常用的Vim指令所以沒有什麼好講的。

1. 同時打開多個檔案,並且垂直顯示多個檔案

 $> vim -O2 file1.txt file2.txt

-O 垂直顯示
-o  水平顯示

p.s. 如果要重新打開不同的檔案就用下面的指令
[ESC] 
:e [file name]

ctrl+w s : 改變顯示方式為水平顯示
ctrl+w v  :  改變顯示方式為垂直顯示
ctrl+w q  :  關閉目前的視窗

ctrl+w -> : 跳到右邊的視窗
ctrl+w <- : 跳到左邊的視窗
p.s.
-> :右方向鍵
<-   : 左方向鍵 

ctrl+w >: 重新調整目前視窗大小,如果是水平的話向右邊擴大
ctrl+w <  :  重新調整目前視窗大小,如果是水平的話向右邊縮小

2. 使用vim來編輯binary檔

只要下:%! xxd就會用16進位來編輯拉
如果要存檔要用:%! xxd -r


3. 搜尋特定文字並且刪除

reference : http://vim.wikia.com/wiki/Search_and_replace

如果你的文字檔的內容長的像以下的樣子:

001:,2,3,4,5
002:,2,3,4,5
003:,2,3,4,5
004:,2,3,4,5
005:,2,3,4,5
more......

當你想將":"這個符號給刪掉的話,可以用以下的方法快速刪除. 不懂意思的話可以看一下參考網站。

[ESC]
:1,$s/://g

4. 將每一行的結尾加上特定文字

如果你的文字檔長的像:


001:,2,3,4,5
002:,2,3,4,5
003:,2,3,4,5
004:,2,3,4,5
005:,2,3,4,5

你想在每行的最後面加上" ,",像:


001:,2,3,4,5,
002:,2,3,4,5,
003:,2,3,4,5,
004:,2,3,4,5,
005:,2,3,4,5,


[ESC]
:1,$s/$/,/g


5.  取消上一次搜尋的highlight

[ESC]
:noh

6.  Comment out multiple lines

You can alternatively select your lines with VISUAL LINE (SHIFT-V), then type : s/^/#
This tells the selected lines that you wish to substitute the start of the line with the # char.








留言

熱門文章