vim可以說是一款很強大的編輯軟件,其強大在於vim插件和vim配置文件,可以按照用戶的習慣改造vim
一.我的vim效果圖
簡單介紹一下,實現的功能:
1).實現vim的簡單配置(代碼高亮,縮進風格等等)
2).實現查看庫文件的實現或者自定義函數的實現。eg:查看printf的實現...
3).實現查看當前編譯文本中定義那些函數
4).實現括號自動補齊
5).實現類,結構體,聯合的成員函數,成員變量的自動出現可供選擇。
二.本機環境,使用工具介紹
vmware,ubuntu,xshell,xmanager,gedit,vim
三.配置vim
1).安裝vim
$sudo apt-get install vim vim-scripts vim-doc
其中vim-scripts 是vim的一個基本插件,支持代碼縮進,代碼高亮等等
2).vim配置文件
一般來說 vim有自己的配置文件,我們一般是不去修改這些文件,而是在用戶目錄底下創建自己的配置文件。vim自己的配置文件一般在 /etc/vim /usr/share/vim 這兩個目錄底下
使用 $cd ~ 切換到用戶文件夾,$touch .vimrc文件,在其中添加自己風格的配置
$cd ~
$touch .vimrc
爲了方便大段文字的粘貼(符合windows底下的複製粘貼風格),我們首先裝一個gedit的插件
$sudo apt-get install gedit
這裏需要說明一點的是,需要注意的是,如果你在使用xshell的話,需要將除了基本配置xshell外,額外增加一點配置(這點非常重要,我自己被坑了2個多小時,都是眼淚,寶寶心裏苦),如圖:
配置完成,還需要安裝xmamager,纔可以使用gedit。如果你直接在虛擬機內部操作的話,那就不用這些操作了。
下面是網上一些大牛的vim基本配置,可以借鑑參考一下
" This line should not be removed as it ensures that various options are
" properly set to work with the Vim-related packages available in Debian.
debian.vim
" Uncomment the next line to make Vim more Vi-compatible
" NOTE: debian.vim sets 'nocompatible'. Setting 'compatible' changes numerous
" options, so any other options should be set AFTER setting 'compatible'.
set nocompatible
" Vim5 and later versions support syntax highlighting. Uncommenting the
" following enables syntax highlighting by default.
if has("syntax")
syntax on " 語法高亮
endif
colorscheme ron " elflord ron peachpuff default 設置配色方案,vim自帶的配色方案保存在/usr/share/vim/vim72/colors目錄下
" detect file type
filetype on
filetype plugin on
" If using a dark background within the editing area and syntax highlighting
" turn on this option as well
set background=dark
" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
"have Vim load indentation rules and plugins according to the detected filetype
filetype plugin indent on
endif
" The following are commented out as they cause vim to behave a lot
" differently from regular Vi. They are highly recommended though.
"set ignorecase " 搜索模式裏忽略大小寫
"set smartcase " 如果搜索模式包含大寫字符,不使用 'ignorecase' 選項。只有在輸入搜索模式並且打開 'ignorecase' 選項時纔會使用。
set autowrite " 自動把內容寫回文件: 如果文件被修改過,在每個 :next、:rewind、:last、:first、:previous、:stop、:suspend、:tag、:!、:make、CTRL-] 和 CTRL-^命令時進行;用 :buffer、CTRL-O、CTRL-I、'{A-Z0-9} 或 `{A-Z0-9} 命令轉到別的文件時亦然。
set autoindent " 設置自動對齊(縮進):即每行的縮進值與上一行相等;使用 noautoindent 取消設置
"set smartindent " 智能對齊方式
set tabstop=4 " 設置製表符(tab鍵)的寬度
set softtabstop=4 " 設置軟製表符的寬度
set shiftwidth=4 " (自動) 縮進使用的4個空格
set cindent " 使用 C/C++ 語言的自動縮進方式
set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s "設置C/C++語言的具體縮進方式
"set backspace=2 " 設置退格鍵可用
set showmatch " 設置匹配模式,顯示匹配的括號
set linebreak " 整詞換行
set whichwrap=b,s,<,>,[,] " 光標從行首和行末時可以跳到另一行去
"set hidden " Hide buffers when they are abandoned
set mouse=a " Enable mouse usage (all modes) "使用鼠標
set number " Enable line number "顯示行號
"set previewwindow " 標識預覽窗口
set history=50 " set command history to 50 "歷史記錄50條
"--狀態行設置--
set laststatus=2 " 總顯示最後一個窗口的狀態行;設爲1則窗口數多於一個的時候顯示最後一個窗口的狀態行;0不顯示最後一個窗口的狀態行
set ruler " 標尺,用於顯示光標位置的行號和列號,逗號分隔。每個窗口都有自己的標尺。如果窗口有狀態行,標尺在那裏顯示。否則,它顯示在屏幕的最後一行上。
"--命令行設置--
set showcmd " 命令行顯示輸入的命令
set showmode " 命令行顯示vim當前模式
"--find setting--
set incsearch " 輸入字符串就顯示匹配點
set hlsearch
將上面的配置信息,自己選擇的使用gedit拷貝到剛纔用戶目錄底下的.vimrc
這是xmanager的效果圖
3).ctags的安裝與配置
ctags可以建立源碼的標籤索引(標籤就是一個表示符被定義的地方,如函數定義),使得程序猿在編程時能夠迅速定位函數、變量、宏定義的位置去查看原型。
$sudo apt-get install ctags
建立源碼索引
eg:加入你需要查看C\C++的一些庫文件的實現,那麼你需要在 /usr/include/c++的目錄底下輸入命令$ctags -R * 生成一個tags文件,當然具體情況具體分析
現在你會發現在 /usr/include/c++ 目錄底下多了一個tags文件,這個文件相當於一個索引,包含c++庫文件中包含的一些函數的實現,這會使你在vim地下編程的時候方便查看庫文件中函數的實現,當然,現在配置還不能實現對庫函數的查找,需要在剛纔的配置文件.vimrc中加入配置文件。
在.vimrc中加入如下信息
"--ctags setting--
" 按下F5重新生成tag文件,並更新taglist
map <F5> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR> :TlistUpdate<CR>
imap <F5> <ESC>:!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR> :TlistUpdate<CR>
set tags=tags
set tags+=./tags "add current directory's generated tags file
set tags+=~/home/wu/tags "add new tags file(剛剛生成tags的路徑,在ctags -R 生成tags文件後,不要將tags移動到別的目錄,否則ctrl+]時,會提示找不到源碼文件)
set tags+=/usr/include/c++/tags
測試一下看是否可以找到:
在底行模式,使用光標定位到需要查看的函數名上面 ctrl+] 查看, ctrl+o 回到剛纔編寫的文本
4).實現括號的代碼補齊
在配置.vimrc文件,網上大牛的配置,大家參考一下
:inoremap <S-ENTER> <c-r>=SkipPair()<CR>
:inoremap <S-SPACE> <ESC>la
:inoremap <C-ENTER> <ESC>A;<CR>
:inoremap ( ()<ESC>i
:inoremap ) <c-r>=ClosePair(')')<CR>
:inoremap { <c-r>=ClsoeBrace()<CR>
:inoremap } <c-r>=ClosePair('}')<CR>
:inoremap [ []<ESC>i
:inoremap ] <c-r>=ClosePair(']')<CR>
:inoremap ;; <ESC>A;<CR>
function ClosePair(char)
if getline('.')[col('.') - 1] == a:char
return "\<Right>"
else
return a:char
endif
endf
function Semicolon()
"echo getline('.')[col('.')]
if getline('.')[col('.')] == ')'
return "<ESC>A;"
elseif getline('.')[col('.')] == '}'
return "\<ESC>A;"
elseif getline('.')[col('.')] == ']'
return "\<ESC>A;"
else
return ";"
endif
endf
function SkipPair()
if getline('.')[col('.') - 1] == ')'
return "\<ESC>o"
else
normal j
let curline = line('.')
let nxtline = curline
while curline == nxtline
if getline('.')[col('.') - 1] == '}'
normal j
let nxtline = nxtline + 1
let curline = line('.')
continue
else
return "\<ESC>i"
endif
endwhile
return "\<ESC>o"
endif
endf
function ClsoeBrace()
if getline('.')[col('.') - 2] == '='
return "{}\<ESC>i"
elseif getline('.')[col('.') - 3] == '='
return "{}\<ESC>i"
elseif getline('.')[col('.') - 1] == '{'
return "{}\<ESC>i"
elseif getline('.')[col('.') - 2] == '{'
return "{}\<ESC>i"
elseif getline('.')[col('.') - 2] == ','
return "{}\<ESC>i"
elseif getline('.')[col('.') - 3] == ','
return "{}\<ESC>i"
else
return "{\<ENTER>}\<ESC>O"
endif
endf
配置到這裏估計很多人都熟悉套路了,本篇文章只是起到一個拋磚引玉的作用,關於命令的使用和各個插件的使用,還需要各位去網上找資料
以上就是本人在學習過程中的一些經驗總結。當然,本人能力有限,難免會有紕漏,希望大家可以指正。