My code works, I don’t know why.

國王的耳朵是驢耳朵

給自己剪貼用的vim設定

| Comments

分享使用vim 的心得,加上使用Vundle plugin管理工具功能配合外部程式碼分享軟體cscopectags來trace C語言的程式碼以及編輯Python程式碼相關設定。

  • 致謝,感謝網友Scott介紹vim register概念,葉闆介紹的tagbar,和Kyle Lin介紹的airline。

  • 2020/Feb/09: 由於vimrc 一直在更新,現在我自己用的.vimrc會持續地更新在這邊,這邊的文章就當作示範教學了。

目錄

測試環境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ lsb_release  -a
No LSB modules are available.
Distributor ID:   Ubuntu
Description:  Ubuntu 16.04.3 LTS
Release:  16.04
Codename: xenial


$ ctags --version
Exuberant Ctags 5.9~svn20110310, Copyright (C) 1996-2009 Darren Hiebert
...

$ cscope --version
cscope: version 15.8b

$ vim --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Nov 24 2016 16:44:48)
Included patches: 1-1689
...

設定.vimrc以及Vundle plugins

事前準備

您需要確認

  • vim版本為7.4以上
  • 安裝ctags和cscope,指令如下 sudo apt-get install exuberant-ctags cscope

安裝Vundle

Vundle是vim plugin 管理工具,他可以透過URL, github, 以及local FS等方式安裝甚至更新Plugin。類似的工具還有不少,我只是挑看到的第一個而已。

Vundle常用的指令如下,還蠻容易望文生義所以我就不解釋了

  • :PluginList
  • :PluginInstall
  • :PluginClean
  • :PluginUpdate

安裝方式如下

  • 首先你要下載Vundle,指令如下 git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

  • 接下來在你的.vimrc加入下面這段,我是從官方網頁改的,其實只是把他的範例Plugin幹掉並加上分隔線及分隔線內的註解而已

.vimrc 要加的部份
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"====================================================================
" Start vundle
"====================================================================
set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

"===============================================================
" Write your plugins here
"===============================================================
Plugin 'Yggdroot/indentLine'

"====================================================================
" Run vundle
"====================================================================
" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList       - lists configured plugins
" :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line

注意下面列出的這幾行statements,你要新增或移除Plugin就是改這個地方。這些Plugin將會在後面介紹。剛好我要安裝的Plugin都是在GitHub上開發或有mirror。而Vundle可以用直接指定Plugin 專案在GitHub相對路徑即可安裝。這些描述也是Vundle載入Plugin 的順序,沒寫對順序有可能有相依問題請自行注意。

例如https://github.com/Yggdroot/indentLine 就寫成Yggdroot/indentLine

我安裝的Plugin
1
2
3
4
5
6
7
8
9
10
11
12
13
"===============================================================
" Write your plugins here
"===============================================================
Plugin 'Yggdroot/indentLine'
Plugin 'ntpeters/vim-better-whitespace'
Plugin 'vim-airline/vim-airline'
Plugin 'tpope/vim-fugitive'
Plugin 'chazy/cscope_maps'
Plugin 'vim-scripts/taglist.vim'
Plugin 'scrooloose/nerdtree'
Plugin 'wesleyche/SrcExpl'
Plugin 'wesleyche/Trinity'
Plugin 'majutsushi/tagbar'
  • 確定新增/刪除Plugin後,就可以執行vim/gvim,使用下面命令
    • :PluginInstall
    • :PluginClean

我安裝的Vundle Plugins

因為安裝方式已經在上面了,這邊就以介紹為主

編輯器相關

airline

安裝準備

先看圖,圖中最下方的那行就是airline,可以顯示一些有用的資訊

由左到右我們可以看到Vim 模式,Git branch 等資訊。以及一些比較特別的符號,這表示我們需要

  • 讓airline取得git資訊
  • 讓airline取得特別符號

也就是說,在安裝airline前要做一些前置動作如下

  • 讓airline取得git資訊
    • 很簡單,安裝vim-fugitive plugin即可
  • 讓airline取得特別符號 這也不難,就是安裝特殊字型,並且設定GUI時存取這些字型。方式如下

取得字型

1
git clone https://github.com/powerline/fonts

安裝字型

1
cd fonts && ./install.sh

.vimrc中指定安裝的字型

1
set guifont=Inconsolata\ for\ Powerline\ 20

設定airline

把下面的資料放入.vimrc即可

1
2
let g:airline_powerline_fonts = 1
set laststatus=2

indentLine

當Ident為空白增加以下的Indent 對齊參考資線

注意此Plugin在Ident為tab同時又加上顯示tab字元時自動失效,目前workaround就是顯示tab字元為|,接下來以.延伸作為辨別。範例如下:

vim-better-whitespace

trailing space顯示成明顯的紅色

Trace 程式碼相關

cscope_maps

簡單來說,就是把cscope指令對應到Hot key

先列出find部份的指令

1
2
3
4
5
6
7
8
9
find : Query for a pattern            (Usage: find c|d|e|f|g|i|s|t name)
       c: Find functions calling this function
       d: Find functions called by this function
       e: Find this egrep pattern
       f: Find this file
       g: Find this definition
       i: Find files #including this file
       s: Find this C symbol
       t: Find this text string

他的使用方法也很簡單,就是先把游標移動到你要查的statement,再按ctrl + \ + c|d|e|f|g|i|s|t 其中一個

舉例來說,我在下圖中把游標移動到core_sys_select函數後按下ctrl + \ + c的結果如下

SrcExpl

當啟動時,您的游標在那個敘述,Source explorer 會切割視窗,印出該敘述的定義。舉例來說,當我游標在138行的free_poll_entry的話,顯示的畫面如下。

taglist

列出目前檔案所有symbol並且可以選擇symbol切換到該symbol在檔案中的位置

nerdtree

以樹狀顯示目前檔案所在目錄結構,看圖就知道

Trinity

看完以上三個,你可能會覺得奇怪好像沒提到怎麼啟動。這就是Trinity大顯身手的地方了。你安裝Trinity後,再Vundle後面加上下面的敘述就可以有

  • F8 : 同時打開或關閉nerdtree, Source explorer, 以及tag list
  • F9 : 打開或關閉Source explorer
  • F10: 打開或關閉tag list
  • F11: 打開或關閉nerdtree
.vimrc 的Trinity設定
1
2
3
4
5
6
7
8
9
10
11
12
13
14
"====================================================================
" Trinity Settings
"====================================================================
" Open and close all the three plugins on the same time
nmap <F8>  :TrinityToggleAll<CR>

" Open and close the Source Explorer separately
nmap <F9>  :TrinityToggleSourceExplorer<CR>

" Open and close the Taglist separately
nmap <F10> :TrinityToggleTagList<CR>

" Open and close the NERD Tree separately
nmap <F11> :TrinityToggleNERDTree<CR>

以下是按下F8 的畫面

tagbar

網友推荐的taglist改良版 plugin,為什麼不換掉taglist呢?因為我喜歡source explorer。除了安裝Plugin外,我也順便設定按下F7可以切換,設定如下。

.vimrc 的Trinity設定
1
2
3
4
5
"====================================================================
" Tagbar Settings
"====================================================================
" Open and close the tagbar separately
nmap <F7> :TagbarToggle<CR>

以下是按下F7 的畫面,可以注意右邊視窗會更進一步地顯示資料結構的成員名稱

Markdown 語法支援

vim-pandoc-syntax

單純就是讓vim可以顯示Markdown syntax highlight,範例如下圖:

Python開發相關

準備工作

主要是語法檢查套件相關安裝,指令如下

.vimrc 的Trinity設定
1
sudo apt install -y flake8 python-rope pylint

python-mode

之前有介紹過,偷懶跳過。也許Python用到一陣子可以上手後可以再分享心得。

syntastic

泛用形語法檢查工具,請參考Syntax checking hacks for vim說明。 目前是我靠他幫忙檢查寫的程式是否符合PEP8規範,要注意的是Ubuntu 16.04中vim 套件預設只支援Python 3,要使用vim 編寫Python 2的朋友請自行估狗。我之前是自行編譯vim解決的。

語法檢查範例如下圖

python_match

讓Python 也可以使用vim中切換配對的快捷鍵%

python

提供下列快捷鍵,節錄自Plugin註解:

  • ]t – Jump to beginning of block
  • ]e – Jump to end of block
  • ]v – Select (Visual Line Mode) block
  • ]< – Shift block to left
  • ]> – Shift block to right
  • ]# – Comment selection
  • ]u – Uncomment selection
  • ]c – Select current/previous class
  • ]d – Select current/previous function
  • ]<up> – Jump to previous line with the same/lower indentation
  • ]<down> – Jump to next line with the same/lower indentation

indentpython

確保你的程式碼符合PEP8的indent規範

和Plugin 無關的設定

以下都加在.vimrc中,建議加到Vundle設定結束後以確保可能會用到的Plugin已經啟動

編輯器和顯示特殊字元相關設定

  • 設定gvim 的配色,請自行找Color scheme
    • colorscheme koehler
  • 設定gvim 的字型和大小
    • set guifont=Inconsolata\ for\ Powerline\ 32
  • 將找到的字串設成高亮度
    • set hlsearch
  • 游標在的該行背景高亮度
    • set cursorline
  • 顯示行號
    • set nu
  • 第八十字元地方顯示高亮度區塊(這是連續兩個描述)
    • set colorcolumn=80
    • highlight ColorColumn guibg=#202020
  • 顯示tab (這是連續兩個描述)
    • set listchars=tab:»\
      • 注意\後面有一個空白
    • set list

效果如下圖

Indent相關設定

  • Tab相關設定
    • set ts=4
      • tab space 為4個字元
    • set expandtab
      • 不使用tab,用空白字元代替
    • set shiftwidth=4
      • Auto indent的移動字元數量
  • visual 模式下一次移動一個indent
    • vnoremap < <gv
      • 往左移動一個indent
    • vnoremap > >gv
      • 往右移動一個indent

其他

  • set clipboard+=unnamed
    • PRIMARY selection的register "*包含vim的unnamed register。白話講就是其他的APP如gedit中滑鼠選字後可以用"*p貼到vim,同樣的"*y6y的結果可以貼在其他的APP如gedit上。這部份有vim register副本,建議到參考資料的副本區一讀。再次感謝Scott大大。

參考資料

懶人包

  • 安裝相關軟體,Vundle和airline字型
1
2
3
4
sudo apt-get install -y exuberant-ctags cscope vim-gtk git flake8 python-rope pylint
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
git clone https://github.com/powerline/fonts
cd fonts && ./install.sh
  • 剪貼下面的文字並存放到 ~/.vimrc
.vimrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"====================================================================
" Start vundle
"====================================================================
set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

"===============================================================
" Write your plugins here
"===============================================================
" Layouts
Plugin 'Yggdroot/indentLine'
Plugin 'ntpeters/vim-better-whitespace'

" Markdown
Plugin 'vim-pandoc/vim-pandoc-syntax'

" Python related
Plugin 'python-mode/python-mode'
Plugin 'vim-scripts/indentpython.vim'
Plugin 'vim-syntastic/syntastic'
Plugin 'vim-scripts/python_match.vim'
Plugin 'vim-scripts/python.vim'

" Misc tools
Plugin 'kien/ctrlp.vim'
Plugin 'vim-airline/vim-airline'
Plugin 'tpope/vim-fugitive'
Plugin 'Valloric/YouCompleteMe'
Plugin 'chazy/cscope_maps'
Plugin 'vim-scripts/taglist.vim'
Plugin 'scrooloose/nerdtree'
Plugin 'wesleyche/SrcExpl'
Plugin 'wesleyche/Trinity'
Plugin 'majutsushi/tagbar'

"====================================================================
" Run vundle
"====================================================================
" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList       - lists configured plugins
" :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line

"====================================================================
" Tagbar Settings
"====================================================================
" Open and close the tagbar separately
nmap <F7> :TagbarToggle<CR>

"====================================================================
" Trinity Settings
"====================================================================
" Open and close all the three plugins on the same time
nmap <F8>  :TrinityToggleAll<CR>

" Open and close the Source Explorer separately
nmap <F9>  :TrinityToggleSourceExplorer<CR>

" Open and close the Taglist separately
nmap <F10> :TrinityToggleTagList<CR>

" Open and close the NERD Tree separately
nmap <F11> :TrinityToggleNERDTree<CR>

"====================================================================
" Airline settings
"====================================================================
let g:airline_powerline_fonts = 1
set laststatus=2

"====================================================================
" syntastic settings
"====================================================================
set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*

let g:loaded_syntastic_c_checker = 1
let g:loaded_syntastic_cpp_checker = 1
let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0

autocmd VimEnter * SyntasticToggleMode " disable syntastic by default

"====================================================================
" pymode settings
"====================================================================
let g:pymode_lint = 1    " Prefer to use syntastic to check lint
let g:pymode_folding = 0 " Unfold all

"====================================================================
" Editor and display Settings
"====================================================================
colorscheme koehler         " Color for gvim

set hlsearch                " Highlight search
set guifont=Inconsolata\ for\ Powerline\ 32 " Font
set cursorline              " Hight background at current cursor line
set nu                      " Display line numbers

" Set background color at colum 80
set colorcolumn=80
highlight ColorColumn guibg=#202020

" Show tabs
set listchars=tab:\|.
set list

" Ensure syntax is on
syntax on

"====================================================================
" Indent Settings
"====================================================================
" Tabs
set ts=4
set expandtab
set shiftwidth=4

" visual indent shift
vnoremap < <gv
vnoremap > >gv

"====================================================================
" MISC Settings
"====================================================================
" Shared unamed regitered with primary selection
set clipboard+=unnamed

" uft-8 encoding: https://stackoverflow.com/questions/16507777/set-encoding-and-fileencoding-to-utf-8-in-vim
set encoding=utf-8
set fileencoding=utf-8

"====================================================================
" Python Settings
"====================================================================
au BufNewFile,BufRead *.py
    \ set tabstop=4 |
    \ set softtabstop=4 |
    \ set shiftwidth=4 |
    \ set textwidth=79 |
    \ set expandtab |
    \ set autoindent |
    \ set fileformat=unix
let python_highlight_all=1

"====================================================================
" pandoc Settings
"====================================================================
" Disable autoconeal for markdown
augroup pandoc_syntax
    au! BufNewFile,BufFilePre,BufRead *.md set filetype=markdown.pandoc
    au! BufNewFile,BufFilePre,BufRead *.markdown set filetype=markdown.pandoc
augroup END

let g:pandoc#syntax#conceal#use = 0

"====================================================================
" YCM Settings
"====================================================================
let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/.ycm_extra_conf.py'
let g:ycm_show_diagnostics_ui = 0

"====================================================================
" Console tab setting:
" Ref https://superuser.com/questions/410982/in-vim-how-can-i-quickly-switch-between-tabs
"====================================================================
nnoremap <S-Left> :tabprevious<CR>
nnoremap <S-Right> :tabnext<CR>
  • gvim -> :PluginInstall 安裝Plugin重新開啟收工

其他

  • cscope產生database供vim使用
    • cscope -bqkR
      • k表示使用kernel mode,不把/usr/include之類的加入資料庫。Cross compile也不會使用host 的header file,所以請自行斟酌。其他參數請自己問男人。
  • ctags產生database供vim使用
    • ctags -R
  • 要在vim使用到ctags和cscope的話,請記得vim一定要開在database同一層目錄!

舉例來說,你在/tmp/linux-stable目錄下了上面兩個指令。要開啟檔案請在/tmp/linux-stable目錄中指定對應路徑。範例如下

1
user@host:/tmp/linux-stable$ gvim fs/select.c

Comments