概述
vim是从 vi 发展出来的一个文本编辑器。功能丰富,在程序员中被广泛使用,而且可以根据自己需要对其进行配置,下面我们来介绍一些vim的基本配置。
首先将你的Linux切换到超级用户,输入命令 su,输入密码再回车切换到root用户,cd进入当前用户主工作目录,输入vim .vimrc进行vim配置.
以下是我的配置文件,值得说明的一点是最后一个设置set filetype=c是设置默认语言为C语言,在没加这个设置之前我使用vim修改文件发现vim的的设置并没有启用,看了一些解决方案后发现原来是没加程序后缀名,vim无法识别是什么类型文件,当做普通文档格式处理,加上这个设置后即使文件后无.c也自动当成C语言文件格式处理。
还有一个功能自动补齐括号本来设置了在输入程序时发现不合适的地方又删掉了,自动补齐括号的代码为:
inoremap ( ()iinoremap [ []iinoremap { {}iinoremap < <>i
至于删掉的原因请先看看我用来测试的代码。
如图所示,在代码中我使用了一个for循环,如果加了自动补齐括号功能,当我输入<号时,会出现如下情况:
for(i=0;i<>10;i++)
由于是自动补齐,可能在需要输入<,>等符号时它会自动帮你补齐错误的代码,所以为了避免这种错误我建议大家如果要设置自动补齐功能别贪图省事把<,>的自动补齐一起设置上,毕竟<,>还是用得很多的。
下面是一些vim的配置和解释:
" 开启语法高亮syntax enable" 开启语法高亮syntax on" 设置字体set guifont=Monaco 12" 设置历史记录条数set history=2000" 检测文件类型filetype on" 针对不同的文件,采用不同的缩进方式filetype indent on" 允许插件filetype plugin on" 启动自动补全filetype plugin indent on" 文件修改之后自动读入set autoread" 启动后不显示提示"set shortmess=atI" 设置取消备份,禁止临时文件生成set nobackup set noswapfile" create undo fileset undolevels=1000 " how many undosset undoreload=10000 " number of lines to save for undoif v:version >= 730set undofile" keep a persistent backup fileset undodir=/tmp/vimundo/ endif"set wildignore=*.swp,*.bak,*.pyc,*.class,.svn" 显示当前横竖线"set cursorline"set cursorcolumn" 设置退出Vim之后内容显示在终端屏幕,可以用于查看和复制" 好处:误删什么,如果以前屏幕打开可以用来找回" set t_ti= t_te=" 设置在Vim中可以使用鼠标,防止终端无法拷贝set mouse=a"==========================================" Display Settings"=========================================="" 显示当前行号和列号set ruler" 在状态栏显示正在输入的命令set showcmd" 左下角显示当前Vim模式set showmode" 光标移动至少保留的行数"set scrolloff=7" 命令行(在状态行下)的高度,默认为1,这里是2set statusline=%<%f %h%m%r%=%k[%{(&fenc=="")?&enc:&fenc}%{(&bomb?",BOM":"")}] %-14.(%l,%c%V%) %P" 总是显示状态栏(Powerline需要2行)set laststatus=2" 显示行号set number" 指定不折行set nowrap" 设置代码匹配,包括括号匹配情况set showmatch" how many tenths of a second to blink when matching brackets" setmatchtime=2" 开启及时搜索(is)set incsearch" 设置搜索高亮(hlsearch)set hls" 设置搜索时忽略大小写set ignorecase" 当搜索的时候尝试smartset smartcase" 设置代码折叠"set foldenable" 折叠方法" manual 手工折叠" indent 缩进折叠" expr 表达式折叠" syntax 语法折叠" diff 对没有更改的文件折叠" marker 标记折叠"set foldmethod=indent"set foldlevel=99" 设置C/C++方式自动对齐set autoindent set cindent set smartindent" 设置tab宽度set tabstop=4" 设置自动对齐空格数set shiftwidth=4" 按退格键时可以一次删除4个空格"set softtabstop=4" 编辑的时候将所有的tab设置为空格(expandtab)set et" 使用Backspace直接删除tabset smarttab" 不在单词中间折行set lbr"==========================================" FileEncode Settings"==========================================" 设置编码方式set encoding=utf-8" 设置打开文件的编码格式set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1 set helplang=cn" 只对终端影响(默认)set termencoding=utf-8" use UNIX as the standard file typeset ffs=unix,dos,mac" 如遇Unicode值大于255的文本,不必等到空格再折行。set formatoptions+=m" 合并两行中文时,不在中间加空格:set formatoptions+=B"==========================================" Other Settings"==========================================" 自动补全配置让Vim补全菜单行为跟IDE一致set completeopt=longest,menu" 增强模式中的命令行自动完成操作set wildmenu" ignore compiled files" set wildignore=*.o,*~,*.pyc,*.class"离开插入模式后自动关闭预览窗口autocmd InsertLeave * if pumvisible() == 0|pclose|endif"回车即选中当前项inoremap <expr> <CR> pumvisible() ? "<C-y>" : "<CR>""上下左右键的行为 会显示其他信息inoremap <expr> <Down> pumvisible() ? "<C-n>" : "<Down>"inoremap <expr> <Up> pumvisible() ? "<C-p>" : "<Up>"inoremap <expr> <PageDown> pumvisible() ? "<PageDown><C-p><C-n>" : "<PageDown>"inoremap <expr> <PageUp> pumvisible() ? "<PageUp><C-p><C-n>" : "<PageUp>"" if this not work ,make sure .viminfo is writable for youif has("autocmd") au BufReadPost * if line("'"") > 1 && line("'"") <= line("$") | exe "normal! g'"" | endifendif"==========================================" HotKey Settings"=========================================="" 去掉搜索高亮"noremap <silent><leader>/ :nohls<CR>map <leader>/ :nohls<CR> map <leader>tn :tabnew<cr> map <leader>to :tabonly<cr> map <leader>tc :tabclose<cr> map <leader>tm :tabmove" Opens a new tab with the current buffer's path" Super useful when editing files in the same directorymap <leader>te :tabedit <c-r>=expand("%:p:h")<cr>/" Quickly edit/reload the vimrc filenmap <silent> <leader>ev :e $MYVIMRC<CR> nmap <silent> <leader>sv :so $MYVIMRC<CR>" 手动刷新tags(含cscope)nmap tg :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q *<CR>:set tags+=./tags<CR>:!cscope -Rbq<CR>:cs add ./cscope.out .<CR>" 切换buffernnoremap [b :bp<CR> nnoremap ]b :bn<CR>" cscope设置if has("cscope") set csprg=/usr/bin/cscope" 制定cscope命令set csto=0" ctags查找顺序,0表示先cscope数据库再标签文件,1表示先标签文件爱set cst" 同时搜索tag文件和cscope数据库set cscopequickfix=s-,c-,d-,i-,t-,e-"使用QucikFix窗口来显示cscope查找结果set nocsverb if filereadable("cscope.out")"如果当前目录下有cscope.out则加载进Vimcs add cscope.out elseif $CSCOPE_DB != """否则只要环境变量不为空就添加制定的数据库到Vimcs add $CSCOPE_DB endif set csverb endif"map <F4>:!cscope -Rbq<CR>:cs add ./cscope.out .<CR><CR><CR> :cs reset<CR>" 查找符号nmap <leader>css :cs find s <C-R>=expand("<cword>")<CR><CR> :copen<CR><CR>" 查找定义nmap <leader>csg :cs find g <C-R>=expand("<cword>")<CR><CR>" 查找被这个函数调用的函数nmap <leader>csd :cs find d <C-R>=expand("<cword>")<CR><CR> :copen<CR><CR>" 查找调用这个函数的函数nmap <leader>csc :cs find c <C-R>=expand("<cword>")<CR><CR>:copen<CR><CR>" 查找这个字符串nmap <leader>cst :cs find t <C-R>=expand("<cword>")<CR><CR>:copen<CR><CR>" 查找这个egrep匹配模式nmap <leader>cse :cs find e <C-R>=expand("<cword>")<CR><CR>:copen<CR><CR>" 查找这个文件nmap <leader>csf :cs find f <C-R>=expand("<cfile>")<CR><CR>" 查找include这个文件的文件nmap <leader>csi :cs find i <C-R>=expand("<cfile>")<CR><CR> :copen<CR><CR>" 设定是否使用QuickFix来显示结果set cscopequickfix=s-,c-,d-,i-,t-,e-" QuickFix open and closennoremap <F11> :copen<CR> nnoremap <F12> :cclose<CR>" QucikFix next and prevnnoremap <F9> :cn<CR> nnoremap <F10> :cp<CR>"==========================================" Theme Settings"==========================================" Set extra options when running in GUI mode"if has("gui_running")" set guifont=Monaco 12" set guioptions-=T" set guioptions+=e" set guioptions-=r" set guioptions-=L" set guitablabel=%M %t" set showtabline=1 " set linespace=2 " set noimd" set t_Co=256"endif" 设置主题set background=dark colorscheme molokai"colorscheme solarizedset t_Co=256" 添加水平滚动条"set guioptions+=b" 取消菜单栏和导航栏set guioptions-=m set guioptions-=T" 去除左右两边滚动条set go-=r set go-=L" 设置水平行数和竖直列数set lines=35set columns=99" 使pathogen生效(插件管理器,只需将插件放入bundle,将pathogen.vim放入autoload即可)"execute pathogen#infect()""==========================================" Vundle Settings"==========================================" 关闭vi的一致性模式,避免以前版本的一些Bug和局限set nocompatible filetype off " Vundle必须要求使用filetype off,如果不设置该选项,UltiSnips无法使用if has('win32') || has('win64')set rtp+=$HOME/vimfiles/vundle call vundle#rc('$HOME/vimfiles/bundle/')else set rtp+=~/.vim/bundle/vundle call vundle#rc()endi" let Vundle manage Vundle" requiredBundle 'gmarik/vundle'" My bundles here:" original repos on GitHubBundle 'tpope/vim-fugitive' Bundle 'Lokaltog/vim-easymotion'" 自动补全单引号,双引号等Bundle 'Raimondi/delimitMate'" 快速加入修改环绕字符Bundle 'tpope/vim-surround'" for repeat -> enhance surround.vim, . to repeat commandBundle 'tpope/vim-repeat'" 多光标编辑 (该插件的ctrl-p可以正常使用,当到达最后一个的时候ctrlp才会起作用)Bundle 'terryma/vim-multiple-cursors'" 对括号等进行光标的控制辅助"Bundle 'rstacruz/sparkup', {'rtp': 'vim/'}"Bundle 'tpope/vim-rails.git'Bundle 'Valloric/ListToggle' Bundle 'L9'" vim-scripts reposBundle 'a.vim' Bundle 'grep.vim'"Bundle 'easytags.vim'"Bundle 'c.vim'" non-GitHub repos"Bundle 'git://git.wincent.com/command-t.git'" Git repos on your local machine (i.e. when working on your own plugin)"Bundle 'file:///Users/gmarik/path/to/plugin'"==========================================" ctrlp settings"==========================================Bundle 'kien/ctrlp.vim'"let g:ctrlp_map = '<leader>p'"let g:ctrlp_cmd = 'CtrlP'"map <leader>f :CtrlPMRU<CR>"set wildignore+=*/tmp/*,*.so,*.swp,*.zip " MacOSX/Linux"let g:ctrlp_custom_ignore = {" 'dir': 'v[/].(git|hg|svn|rvm)$'," 'file': 'v.(exe|so|dll|zip|tar|tar.gz)$'," 'link': 'SOME_BAD_SYMBOLIC_LINKS'," }let g:ctrlp_custom_ignore = { 'file': 'v.(exe|so|dll|zip|tar|tar.gz|lib|out|png|img|bak|db|o)' }" 在每次进行切换分支或者重新设定custome_ignore选项的时候,必须手动清除CtrlP的缓存," 也可以使用下句不让它进行缓存处理,但扫描时间会比较耗时"let g:ctrlp_use_caching = 0let g:ctrlp_working_path_mode=0let g:ctrlp_match_window_bottom=1let g:ctrlp_max_height=15let g:ctrlp_match_window_reversed=0let g:ctrlp_mruf_max=500let g:ctrlp_follow_symlinks=1nnoremap <leader>b :CtrlPBuffer<CR> nnoremap <leader>d :CtrlPDir<CR>"==============================================================" ctrlp-funky settings ctrlp插件 - 不用ctag进行函数快速跳转"==============================================================Bundle 'tacahiroy/ctrlp-funky'"nnoremap <Leader>fu :CtrlPFunky<Cr>nnoremap <Leader>f :CtrlPFunky<Cr>" narrow the list down with a word under cursor"nnoremap <Leader>fU :execute 'CtrlPFunky ' . expand('<cword>')<Cr>let g:ctrlp_funky_syntax_highlight = 1let g:ctrlp_extensions = ['funky']"==========================================" nerdcommenter settings 快速注释"==========================================Bundle 'scrooloose/nerdcommenter'" 注释与代码之间留存的空格数let g:NERDSpaceDelims = 1"==========================================" Airline settings"=========================================="Bundle 'bling/vim-airline'"Bundle 'bling/vim-bufferline'"if !exists('g:airline_symbols')"let g:airline_symbols = {}"endif"let g:airline_left_sep = '▶'"let g:airline_left_alt_sep = '❯'"let g:airline_right_sep = '◀'"let g:airline_right_alt_sep = '❮'"let g:airline_symbols.linenr = '¶'"let g:airline_symbols.branch = '⎇'"" 开启tabline"let g:airline#extensions#tabline#enabled = 1"" tabline中当前buffer两端的分隔符"let g:airline#extensions#tabline#left_sep = ' '"" tabline为激活的buffer的两端字符"let g:airline#extensions#tabline#left_alt_sep = '|'"" tabline中buffer显示编号"let g:airline#extensions#tabline#buffer_nr_show = 1"let g:airline#extensions#buffline#enabled = 1"let g:airline#extensions#bufferline#overwrite_variables = 1"" airline主题"let g:airline_theme = 'powerlineish' "括号显示增强"Bundle 'kien/rainbow_parentheses.vim'"let g:rbpt_colorpairs = [" ['brown', 'RoyalBlue3']," ['Darkblue', 'SeaGreen3']," ['darkgray', 'DarkOrchid3']," ['darkgreen', 'firebrick3']," ['darkcyan', 'RoyalBlue3']," ['darkred', 'SeaGreen3']," ['darkmagenta', 'DarkOrchid3']," ['brown', 'firebrick3']," ['gray', 'RoyalBlue3']," ['black', 'SeaGreen3']," ['darkmagenta', 'DarkOrchid3']," ['Darkblue', 'firebrick3']," ['darkgreen', 'RoyalBlue3']," ['darkcyan', 'SeaGreen3']," ['darkred', 'DarkOrchid3']," ['red', 'firebrick3']," ]"let g:rbpt_max = 40"let g:rbpt_loadcmd_toggle = 0"au VimEnter * RainbowParenthesesToggle"au Syntax * RainbowParenthesesLoadRound"au Syntax * RainbowParenthesesLoadSquare"au Syntax * RainbowParenthesesLoadBraces" 主题 solarized Bundle 'altercation/vim-colors-solarized'"let g:solarized_termcolors=256let g:solarized_termtrans=1let g:solarized_contrast="normal"let g:solarized_visibility="normal"" 主题 molokai Bundle 'tomasr/molokai'"==========================================" TagList settings"==========================================Bundle 'taglist.vim'" increase the width of the taglist window"let Tlist_WinWidth=20" 自动打开TagList的windowlet Tlist_Auto_Open=0" 当只剩下Tlist的时候自动关闭let Tlist_Exit_OnlyWindow=1" 打开tags用单击let Tlist_Use_SingleClick=1" close tag folders for inactive bufferslet Tlist_File_Fold_Auto_Close=1" show the fold indiactor column in the taglist windowlet Tlist_Enable_Fold_Column=1" 自动更新TagList包含最新编辑的文件let Tlist_Auto_Update=1" 显示一个文件的taglet Tlist_Show_One_File=1" 在右侧显示窗口let Tlist_Use_Right_Window=1" 按照名称排序let Tlist_Sort_Type="name"" 让TagList始终解释文件中的tag,不管TagList窗口有没有打开let Tlist_Process_File_Always=1" 设置leader键nnoremap <leader>tl :TlistToggle<CR>"==========================================" YouCompleteMe settings"==========================================Bundle 'Valloric/YouCompleteMe'"let g:ycm_key_list_select_completion=['<c-n>']"let g:ycm_key_list_previous_completion=['<c-p>']let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py' let g:ycm_collect_indentifiers_from_tags_files=1let g:ycm_seed_identifiers_with_syntax=1" 避免YCM每次加载都对用户提示是否加载let g:ycm_confirm_extra_conf=0let g:ycm_autoclose_preview_window_after_completion=1let g:ycm_complete_in_comments = 1"在注释输入中也能补全let g:ycm_complete_in_strings = 1"在字符串输入中也能补全let g:ycm_collect_identifiers_from_comments_and_strings = 1"注释和字符串中的文字也会被收入补全" 跳到定义或声明nnoremap <F2> :YcmCompleter GoToDefinitionElseDeclaration<CR>" 强制进行编译nnoremap <F5> :YcmForceCompileAndDiagnostics<CR>
最后
以上就是开朗过客为你收集整理的vim的简单配置的全部内容,希望文章能够帮你解决vim的简单配置所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复