- vim基础配置:
set background=dark
set number
filetype on
set ruler
"set cursorline " 突出显示当前行
set tabstop=4 " 设置tab键的宽度
set shiftwidth=4 " 换行时行间交错使用4个空格
set autoindent " 自动对齐
set backspace=2 " 设置退格键可用
set cindent shiftwidth=4 " 自动缩进4空格
set smartindent " 智能自动缩进
set ai! " 设置自动缩进
set hlsearch " 开启高亮显示结果
"set laststatus=2 " 开启状态栏信息
"set cmdheight=2 " 命令行的高度,默认为1,这里设为2
set showcmd " Show (partial) command in status line.
set showmatch " Show matching brackets.
set ignorecase " Do case insensitive matching
set smartcase " Do smart case matching
set incsearch " Incremental search
set autowrite " Automatically save before commands like :next and :make
set hidden " Hide buffers when they are abandoned
set mouse=a " Enable mouse usage (all modes)
" line enables syntax highlighting by default.
if has("syntax")
syntax on
endif
" 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
endif
" 设置编码
set fenc=utf-8
set encoding=utf-8
set fileencodings=utf-8,gbk,cp936,latin-1
" 解决consle输出乱码
language messages zh_CN.utf-8
" ======= 引号 && 括号自动匹配 ======= "
:inoremap ( ()<ESC>i
:inoremap ) <c-r>=ClosePair(')')<CR>
:inoremap { {}<ESC>i
:inoremap } <c-r>=ClosePair('}')<CR>
:inoremap [ []<ESC>i
:inoremap ] <c-r>=ClosePair(']')<CR>
"":inoremap < <><ESC>i
"":inoremap > <c-r>=ClosePair('>')<CR>
":inoremap " ""<ESC>i
:inoremap ' ''<ESC>i
:inoremap ` ``<ESC>i
- ctags and Tlist
安装ctags 5.8
用下面的命令解压安装:
$ tar -xzvf ctags-5.6.tar.gz $ cd ctags-5.6 $ make # make install // 需要root权限 |
然后去你的源码目录, 如果你的源码是多层的目录, 就去最上层的目录, 在该目录下运行命令: ctags -R
我现在以 vim71 的源码目录做演示
$ cd /home/wooin/vim71 $ ctags -R |
$ vim /home/wooin/vim71/src/main.c |
:set tags=/home/wooin/vim71/tags |
将光标移到某个函数名上,按Ctrl + ],Vim将自动跳转到该函数的定义,按Ctrl + T返回
- MiniBufExplorer
上面那个狭长的窗口就是MiniBufExplorer窗口, 其中列出了当前所有已经打开的buffer, 当你把光标置于这个窗口时, 有下面几个快捷键可以用:
<Tab> | 向前循环切换到每个buffer名上 |
<S-Tab> | 向后循环切换到每个buffer名上 |
<Enter> | 在打开光标所在的buffer |
d | 删除光标所在的buffer |
以下的两个功能需要在~/.vimrc中增加:
let g:miniBufExplMapCTabSwitchBufs = 1 |
<C-Tab> | 向前循环切换到每个buffer上,并在但前窗口打开 |
<C-S-Tab> | 向后循环切换到每个buffer上,并在但前窗口打开 |
如果在~/.vimrc中设置了下面这句:
let g:miniBufExplMapWindowNavVim = 1 |
C-w,h j k l 向"左,下,上,右"切换窗口.
在~/.vimrc中设置:
let g:miniBufExplMapWindowNavArrows = 1 |
- 插件A
下面介绍它的用法:
作为一个C程序员, 日常Coding时在源文件与头文件间进行切换是再平常不过的事了, 直接用vim打开其源/头文件其实也不是什么麻烦事, 但是只用一个按键就切换过来了, 这是多么贴心的功能啊....
安装好a.vim后有下面的几个命令可以用了:
:A | 在新Buffer中切换到c/h文件 |
:AS | 横向分割窗口并打开c/h文件 |
:AV | 纵向分割窗口并打开c/h文件 |
:AT | 新建一个标签页并打开c/h文件 |
我在~/.vimrc中增加了一句:
nnoremap <silent> <F12> :A<CR> |
"======== 加载语法模板和作者、时间信息 ========"
" :LoadTemplate 根据文件后缀自动加载模板
let g:template_path='~/.vim/template/'
" :AuthorInfoDetect 自动添加作者、时间等信息,本质是NERD_commenter && authorinfo的结合 可修改authorinfo.vim中插入到具体选项
let g:vimrc_author='wayne'
let g:vimrc_email='wayne.zeng87@gmail.com'
let g:vimrc_company='TrueWayne'
" Ctrl + E 一步加载语法模板和作者、时间信息
map <c-e> <ESC>:LoadTemplate<CR><ESC>:AuthorInfoDetect<CR><ESC>Gi
imap <c-e> <ESC>:LoadTemplate<CR><ESC>:AuthorInfoDetect<CR><ESC>Gi
vmap <c-e> <ESC>:LoadTemplate<CR><ESC>:AuthorInfoDetect<CR><ESC>Gi
"======== java complete======="
setlocal completefunc=javacomplete#CompleteParamsInfo
autocmd FileType java set omnifunc=javacomplete#Complete
"======== ;alias======="
let mapleader=";"
nmap <silent> <leader>tl :Tlist<cr>
nmap <silent> <lender>cal :Calendar<cr>
"nmap <silent> <lender>cah :CalendarH<cr>
" ======= 编译 && 运行 ======= "
" 编译C源文件
func! CompileGcc()
exec "w"
let compilecmd="!gcc -Wall -pedantic -std=c99 "
let compileflag="-o %<"
exec compilecmd." % ".compileflag
endfunc
" 编译C++源文件
func! CompileCpp()
exec "w"
let compilecmd="!g++ -Wall -pedantic -std=c++98 "
let compileflag="-o %<"
exec compilecmd." % ".compileflag
endfunc
" 编译Java源文件
func! CompileJava()
exec "w"
exec "!javac %"
endfunc
" 根据文件类型自动选择相应的编译函数
func! CompileCode()
exec "w"
if &filetype == "c"
exec "call CompileGcc()"
elseif &filetype == "cpp"
exec "call CompileCpp()"
elseif &filetype == "java"
exec "call CompileJava()"
elseif &filetype == "lua"
exec "!lua %<.lua"
elseif &filetype == "perl"
exec "!perl %<.pl"
elseif &filetype == "python"
exec "!python %<.py"
elseif &filetype == "ruby"
exec "!ruby %<.rb"
endif
endfunc
" 运行可执行文件
func! RunResult()
exec "w"
if &filetype == "c"
exec "! %<"
elseif &filetype == "cpp"
exec "! %<"
elseif &filetype == "java"
exec "!java %<"
elseif &filetype == "lua"
exec "!lua %<.lua"
elseif &filetype == "perl"
exec "!perl %<.pl"
elseif &filetype == "python"
exec "!python %<.py"
elseif &filetype == "ruby"
exec "!ruby %<.rb"
endif
endfunc
" Ctrl + C 一键保存、编译
map <c-c> :call CompileCode()<CR>
imap <c-c> <ESC>:call CompileCode()<CR>
vmap <c-c> <ESC>:call CompileCode()<CR>
" Ctrl + R 一键保存、运行
map <c-r> :call RunCode()<CR>
imap <c-r> <ESC>:call RunCode()<CR>
vmap <c-r> <ESC>:call RunCode()<CR>