vim配置
vim–一款让小白头疼,吐槽难用,但同时又是一款让大神爱不释手的开发神器,因为如果使用vim开发,你可以高效到不使用鼠标,这几乎是所有Windows下的IDE所不能实现的,相信当你的码代码的速度会因为点鼠标而变慢的时候,你就真正到达大神们的境界了。
虽然本人的水平并没有达到上面所说的水平,但是本人还是分享一些自己使用或者参考别人的vim的配置与插件,因为如果不使用vim开发,在Windows与linux间来回切换确实效率太低下了,而且Windows与linux间回车的定义也不一致,你自己在Windows下写的代码再拷贝回linux上库的话经常会有格式上的问题,git提交的代码因为格式问题会被公司的大佬们喷的,所以,不管从开发效率还是代码格式上来说,使用vim开发都是一种最优解。
功能快捷键
不显示行号:Ctrl/Command + n
显示行号:Ctrl/Command + b
新建一个tab :Ctrl/Command + d
在tab间切换:Ctrl/Command + e
关闭当前tab:Ctrl/Command + q
鼠标可以滑动,选择光标:Ctrl/Command + a
关闭鼠标可以滑动,选择光标:Ctrl/Command + z
打开NERDTree:F7
使用LeaderF搜索文件:space + f
我的 .vimrc配置
" An example for a vimrc file.
"
" Maintainer: Bram Moolenaar <Bram@vim.org>
" Last change: 2011 Apr 15
"
" To use it, copy it to
" for Unix and OS/2: ~/.vimrc
" for Amiga: s:.vimrc
" for MS-DOS and Win32: $VIM\_vimrc
" for OpenVMS: sys$login:.vimrc
" ---------------------------------------------------
set nocompatible " be iMproved
filetype off " required!
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
" let Vundle manage Vundle
" required!
Bundle 'gmarik/vundle'
" vim-go
Plugin 'fatih/vim-go'
set rtp+=~/.vim/bundle/vim-go/
let g:go_highlight_functions = 1
let g:go_highlight_methods = 1
let g:go_highlight_structs = 1
let g:go_def_mapping_enabled = 0
" tagbar
Bundle 'majutsushi/tagbar'
nmap <F4> :TagbarToggle<CR>
let g:tagbar_right=1
let g:tagbar_width=40
" gocode
Plugin 'nsf/gocode', {'rtp': 'vim/'}
" supertab
let g:SuperTabDefaultCompletionType="context"
" theme
colorscheme molokai
set t_Co=256
set background=dark
" for paste
nnoremap <F2> :set invpaste paste?<CR>
imap <F2> <C-O>:set invpaste paste?<CR>
set pastetoggle=<F2>
" nerdtree
Bundle 'scrooloose/nerdtree'
let g:NERDTreeDirArrows = 1
map <F7> :NERDTreeToggle<CR>
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
" CtrlP
set runtimepath^=~/.vim/bundle/ctrlp.vim
let g:ctrlp_by_filename = 1
let g:ctrlp_working_path_mode = '0'
let g:ctrlp_match_window = 'bottom,order:ttb,min:1,max:10,results:20'
let g:ctrlp_extensions = ['buffertag', 'dir', 'undo', 'line', 'changes', 'mixed', 'bookmarkdir']
let g:ctrlp_map = '<c-p>'
let g:ctrlp_cmd = 'CtrlP'
set wildignore+="*/tmp/*,*.so,*.o,*.a,*.obj,*.swp,*.zip,*.pyc,*.pyo,*.class,.DS_Store"
let g:ctrlp_custom_ignore = {
\ 'dir': '\v[\/]\.(git|hg|svn)$',
\ 'file': '\v\.(exe|so|dll|o|mod.c)$',
\ 'link': 'SOME_BAD_SYMBOLIC_LINKS',
\ }
" vim-airline
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'
let g:airline#extensions#tabline#enabled = 1
let g:airline_theme='molokai'
"nnoremap <C-Q> :bp<CR>
"nnoremap <C-W> :bn<CR>
" spell check
:map <C-K> :setlocal spell spelllang=en_us<CR>
" cscope
if has('cscope')
set cscopetag
set csto=0
set cscopeverbose
set nocsverb
if filereadable("cscope.out")
cs add cscope.out
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
set csverb
nmap <C-f>s :cs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-f>g :cs find g <C-R>=expand("<cword>")<CR><CR>
nmap <C-f>c :cs find c <C-R>=expand("<cword>")<CR><CR>
nmap <C-f>t :cs find t <C-R>=expand("<cword>")<CR><CR>
nmap <C-f>e :cs find e <C-R>=expand("<cword>")<CR><CR>
nmap <C-f>f :cs find f <C-R>=expand("<cword>")<CR><CR>
nmap <C-f>i :cs find i ^<C-R>=expand("<cword>")<CR>$<CR>
nmap <C-f>d :cs find d <C-R>=expand("<cword>")<CR><CR>
endif
""LeaderF
Plugin 'Yggdroot/LeaderF'
let mapleader = "\<space>"
""LeaderF config
noremap <leader>f :Leaderf file /Users/bytedance/code/<CR>
noremap <leader>j :LeaderfFunction<CR>
noremap <leader>t :LeaderfTag<CR>
Plugin 'vim-scripts/AutoComplPop' " 自动补全功能
" for python indent
" autocmd BufNewFile,BufRead *.py set tabstop=4 softtabstop=4 shiftwidth=4 expandtab
" autocmd BufNewFile,BufRead *.h,*.hh,*.cc,*.c,*.cxx,Makefile set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab
" vim-autoformat
Plugin 'Chiel92/vim-autoformat'
let g:formatdef_custom = '"astyle --options=none --lineend=linux --mode=c --style=linux --indent=force-tab=8 --indent-preprocessor --indent-col1-comments --attach-namespaces --attach-classes --attach-inlines --attach-extern-c --min-conditional-indent=0 --max-instatement-indent=80 --pad-oper --pad-header --unpad-paren --align-pointer=name --align-reference=name --max-code-length=80 --break-after-logical"'
let g:formatters_cpp = ['custom']
let g:formatters_c = ['custom']
" au BufWrite *.cc,*.hh,*.c,*.h :Autoformat
noremap <F5> :Autoformat<CR>
function! UpdateCtags()
let curdir=getcwd()
while !filereadable("./tags")
cd ..
if getcwd() == "/"
break
endif
endwhile
if filewritable("./tags")
!ctags -R --file-scope=yes --langmap=c:+.h --languages=c,c++ --links=yes --c-kinds=+p --c++-kinds=+p --fields=+iaS --extra=+q
endif
execute ":cd " . curdir
endfunction
nmap <F10> :call UpdateCtags()<CR>
filetype plugin indent on
syntax on
" for Chinese encoding
set fileencodings=utf-8,gbk
" hmcopyright
function! HMBANNER()
0r ~/.vim/templates/copyright
" language time en_US.UTF-8
exe "%s/@TIMESTAMP@/" . strftime("%a %b %d %H:%M:%S %Y"). "/g"
endfunction
autocmd BufNewFile *.c call HMBANNER()
autocmd BufNewFile *.h call HMBANNER()
" ---------------------------------------------------
" When started as "evim", evim.vim will already have done these settings.
if v:progname =~? "evim"
finish
endif
" allow backspacing over everything in insert mode
set backspace=indent,eol,start
set nobackup " do not keep a backup file, use versions instead
set nowritebackup
set noswapfile
" set history=50 " keep 50 lines of command line history
set ruler " show the cursor position all the time
set showcmd " display incomplete commands
set incsearch " do incremental searching
" For Win32 GUI: remove 't' flag from 'guioptions': no tearoff menu entries
" let &guioptions = substitute(&guioptions, "t", "", "g")
" Don't use Ex mode, use Q for formatting
map Q gq
" CTRL-U in insert mode deletes a lot. Use CTRL-G u to first break undo,
" so that you can undo CTRL-U after inserting a line break.
inoremap <C-U> <C-G>u<C-U>
" In many terminal emulators the mouse works just fine, thus enable it.
set mouse=v
:map <C-A> :set mouse=a<CR>
:map <C-Z> :set mouse=v<CR>
set nu
:map <C-N> :set nonu<CR>
:map <C-B> :set nu<CR>
:map <C-D> :tabnew<CR>
:map <C-E> :tabp<CR>
:map <C-Q> :tabc<CR>
" mark line and column of cursor
set cursorline
set cursorcolumn
" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
syntax on
set hlsearch
endif
" Only do this part when compiled with support for autocommands.
if has("autocmd")
" Enable file type detection.
" Use the default filetype settings, so that mail gets 'tw' set to 72,
" 'cindent' is on in C files, etc.
" Also load indent files, to automatically do language-dependent indenting.
filetype plugin indent on
" Put these in an autocmd group, so that we can delete them easily.
augroup vimrcEx
au!
" For all text files set 'textwidth' to 78 characters.
autocmd FileType text setlocal textwidth=78
" When editing a file, always jump to the last known cursor position.
" Don't do it when the position is invalid or when inside an event handler
" (happens when dropping a file on gvim).
" Also don't do it when the mark is in the first line, that is the default
" position when opening a file.
autocmd BufReadPost *
\ if line("'\"") > 1 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
augroup END
else
set autoindent " always set autoindenting on
endif " has("autocmd")
" highlight extra white space"
highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /\s\+$/
set listchars=tab:>-,trail:~,extends:>,precedes:<
set list
" Convenient command to see the difference between the current buffer and the
" file it was loaded from, thus the changes you made.
" Only define it when not defined already.
if !exists(":DiffOrig")
command DiffOrig vert new | set bt=nofile | r ++edit # | 0d_ | diffthis
\ | wincmd p | diffthis
endif
安装方法
vim ~/.vimrc
将上面的内容拷贝到~/.vimrc
中,保存退出后,然后再创建.vim
文件
mkdir ~/.vim
安装vundle
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
安装需要的插件,有两种方法安装插件。
(1) 运行 vim ,再运行 :PluginInstall
vim
在vim 中输入一下字符,耐心等待安装即可
:PlugInstall
(2) 通过命令行直接安装
vim +PluginInstall +qall
安装过程中可能会由于git设置的问题,导致下载失败,可以修改一下设置
git config --global http.postBuffer 1048576000
如果安装不成功,可以尝试手动git clone对应的插件到~/.vim/bundle
molokai 安装
cd ~/.vim
git clone git://github.com/tomasr/molokai.git
cp -r ~/.vim/molokai/colors ~/.vim/
部分vim插件对vim的版本有要求可以按照下面的操作升级vim,安装步骤参考的链接
vim升级安装方法
第一步,下载源码
到Vim官方Github仓库下载目前最新的Vim Release版本
wget https://codeload.github.com/vim/vim/tar.gz/v8.2.1258
第二步,解压
mv v8.2.1258 vim-v8.2.1258.tar.gz
tar -xvzf vim-v8.2.1258.tar.gz
第三步,编译安装
cd vim-8.2.1258/
./configure --prefix=$HOME/.local --enable-python3interp=yes && make && make install
这里注意一下我们需要用configure配置一下安装的路径,将Vim8安装到自己账户的目录下,避免干扰到系统上的其他用户
--enable-python3interp=yes
添加 python3 支持
在这里可能会遇到 no terminal library found 错误:
no terminal library found
checking for tgetent()… configure: error: NOT FOUND! You need to install a terminal library; for example ncurses. Or specify the name of the library with –with-tlib.
解决方法:
Ubuntu下解决方法:
sudo apt install libncurses5-dev
CentOS 下
yum install ncurses-devel.x86_64
完成后重新进行这一步,建议删除
第四步,链接
利用alias将vim指令定向到刚刚安装的vim8,同时修改.bashrc确保之后一直能生效
alias vim='~/.local/bin/vim'
echo "alias vim='~/.local/bin/vim'" >> ~/.bashrc
第五步,检查
vim --version
可能会出现一下错误
LeaderF requires Vim compiled with python and/or a compatible python version
需要编译vim的实收使用python不要使用python3
cd vim-8.2.1258/
./configure --prefix=$HOME/.local --enable-pythoninterp=yes && make && make install
这样就可以了
创建新文件模板设置
mkdir ~/.vim/templates
vim ~/.vim/templates/copyright
然后填入一下内容
/*
* Copyright (C)
* Author: JiaHao
* Create: @TIMESTAMP@
*/
@TIMESTAMP@
用来自动替换创建文件时间,另外,可能机器上设置language time en_US.UTF-8
会有问题,可以修改.vimrc
文件,屏蔽掉这个地方