vim 常用功能
常用功能的 plugin 及快捷鍵:
- 游標停在字上的時候會自動 highlight 同樣的字
開啟 vim 後輸入z/
,目前還沒找到怎麼一開 vim 就啟動自動 highlight。 - 跳到游標所在 variable 或 function 的宣告
- 在 .cpp 跟 .h 之間切換
a.vim (:A
) - 跳到游標所在的 function 的定義
cscope (ctrl+\ g
)
ctags (g]
orctrl+]
) - 跳到游標所在的 variable local 宣告
ctags (gd
) - 跳到游標所在的 variable global 宣告
ctags (gD
) - 全 project search
cscope (ctrl+\ t
) - 列出 file 中有哪些 function
taglist (F8
) - 自動補齊
於 insert modectrl + n
- 自動補括號
- hex mode
:%! xxd
- 切換 history
ctrl + i
&ctrl + o
- 多行註解
ESC
、ctrl + V
選範圍、大寫I
、輸入註解符號、ESC
- 多行取消註解
ESC
、ctrl + V
選範圍、delete
My .vimrc
:https://github.com/cjwind/dotfiles/blob/master/vimrc
ctags taglist
$ sudo apt-get install ctags
切到 project 資料夾產生 tag 資訊:
$ ctags --extra=+f -R *
加上 --extra=+f
可以在 vim 中使用 :tag <filename>
跳到該檔案,:tag <filename>
後再 ctrl + t
可回原本檔案。
到 http://www.vim.org/scripts/script.php?script_id=273 抓 taglist_45.zip
,解開後將 taglist.vim
放到 ~/vim/plugin
。
~/.vimrc
加入相關設定:
1 | let Tlist_Ctags_Cmd = '/usr/bin/ctags' |
ctrl + w
再加方向鍵可以切換 window,例如加右鍵就是跳到右邊的 window。
patch
有時候切換 tab 會出現 error Taglist error: Error detected while processing function <SNR>29_Tlist_Refresh_Folds
,可用 patch 解決:$ patch -p0 ~/.vim/plugin/taglist.vim taglist.diff
cscope
$ sudo apt-get install cscope
切到 project 的資料夾產生 cscope 資料庫:
$ cscope -RC
之後用 vim 開啟 source code,可用 :cs
指令使用 cscope 的功能。也可以在 ~/.vim/plugin
中放 cscope_map.vim 加快捷鍵:
1 | ctrl+\ s "s表Symbol,列出所有參考到游標所在字串的地方,包含定義和呼叫。 |
自動 highlight
在 .vim/plugin
加入 autohighlight.vim
:
1 | " Highlight all instances of word under cursor, when idle. |
輸入 z/
可開關自動 highlight 的功能。
a.vim
在 header 及 source 之間切換。script
切換指令為 :A
。
自動補括號
將 script 放到 ~/.vim/plugin/
底下。