什么是 vim?
Vim 是从 vi 发展出来的一个文本编辑器。代码补全、编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用。

简单的来说, vi 是老式的字处理器,不过功能已经很齐全了,但是还是有可以进步的地方。 vim 则可以说是程序开发者的一项很好用的工具。

连 vim 的官方网站 (https://www.vim.org/) 自己也说 vim 是一个程序开发工具而不是文字处理软件。

常用快捷键

撤销 u
反撤销 ctrl+r
显示文件名 ctrl+g

显示行号

  • 临时显示行号

如果只是临时显示vim 的行号,只须按ESC 键退出编辑内容模式,输入 :set number 后按回车键,就可以显示行号了。行号显示只是暂时的,退出vim 后再次打开vim 就不显示行号了。

  • 永久显示行号

如果想让vim永久显示行号,则需要修改vim配置文件vimrc。如果没有此文件可以创建一个。在启动vim时,当前用户根目录下的 vimrc 文件会被自动读取,因此一般在当前用户的根目录下创建 vimrc 文件,即使用下面的命令:

$vim ~/.vimrc

如果要对所有用户生效

$sudo vim /etc/vim/vimrc

在打开的 vimrc 文件中最后一行输入 set number ,然后保存退出。再次用 vim 打开文件时,就会显示行号了。

查找替换

:s/output/gt      #使用gt替换output
:s/output/gt/g    #使用gt替换当前行所有output
:%s/output/gt     #使用gt替换文件中所有output
&   #重复执行上一个命令 

r   替换当前字符   
R   Replace模式 ESC返回

主题及设置

# 查看所有可用的vim主题
ls /usr/share/vim/vim74/colors/
# 通过.vimrc更改 vim 主题等设置
colorscheme morning
set number | nonumber
set spell | nospell 

拼写检查

]s # 移动到下一个拼写错误
[s #      上一个拼写错误
z= # 选择正确的拼写
zg # 添加用户拼写
zw # 删除用户拼写	

vim插件打造IDE

  • 安装Vundle
mkdir -p ~/.vim/bundle/
git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
#在.vimrc 中添加bundle的配置
  • vim安装插件

  • Set up Vundle:

git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
  • Configure Plugins: Put this at the top of your .vimrc to use Vundle. Remove plugins you do not need, they are for illustration purposes.
if &compatible
  set nocompatible
end

filetype off
set rtp+=~/.vim/bundle/Vundle.vim/

call vundle#rc()

" Let Vundle manage Vundle
Bundle 'gmarik/vundle'

" Define bundles via Github repos
" 标签导航
Bundle 'majutsushi/tagbar'
Bundle 'vim-scripts/ctags.vim'
" 静态代码分析
Bundle 'scrooloose/syntastic'
" 文件搜索
Bundle 'kien/ctrlp.vim'
" 目录树导航
Bundle "scrooloose/nerdtree"
" 美化状态栏
Bundle "Lokaltog/vim-powerline"
" 主题风格
Bundle "altercation/vim-colors-solarized"
" python自动补全
Bundle 'davidhalter/jedi-vim'
Bundle "klen/python-mode"
" 括号匹配高亮
Bundle 'kien/rainbow_parentheses.vim'
" 可视化缩进
Bundle 'nathanaelkane/vim-indent-guides'
" if filereadable(expand("~/.vimrc.bundles.local"))
"   source ~/.vimrc.bundles.local
" endif

filetype on
  • Install Plugins: Launch vim and run: PluginInstall
Plugin 'nathangrigg/vim-beancount'
  • vim F5运行
map <F5> :call CompileRunGcc()<CR>
    func! CompileRunGcc()
        exec "w"
if &filetype == 'c'
            exec "!g++ % -o %<"
            exec "!time ./%<"
elseif &filetype == 'cpp'
            exec "!g++ % -o %<"
            exec "!time ./%<"
elseif &filetype == 'sh'
            :!time bash %
elseif &filetype == 'python'
            exec "!time python %"
endif
    endfunc
  • vimrc for python
"运行快捷键"
    map <F5> :call CompileRunGcc()<CR>
        func! CompileRunGcc()
            exec "w"
    if &filetype == 'c'
                exec "!g++ % -o %<"
                exec "!time ./%<"
    elseif &filetype == 'cpp'
                exec "!g++ % -o %<"
                exec "!time ./%<"
    elseif &filetype == 'sh'
                :!time bash %
    elseif &filetype == 'python'
                exec "!time python %"
    endif
        endfunc
    
    "打开文件类型检查"
    " filetype on"
    "filetype off
    "set rtp+=~/.vim/bundle/vundle/
    "call vundle#rc()
    
    "if filereadable(expand("~/.vimrc.bundles"))
    "  source ~/.vimrc.bundles
    "endif
    
    "打开语法高亮显示"
    syntax on
    
    filetype off
    set rtp+=~/.vim/bundle/vundle/
    call vundle#rc()
    
    if filereadable(expand("~/.vimrc.bundles"))
      source ~/.vimrc.bundles
    endif
    
    " tagbar标签导航
    nmap <Leader>tb :TagbarToggle<CR>
    let g:tagbar_ctags_bin='/usr/bin/ctags'
    let g:tagbar_width=30
    autocmd BufReadPost *.cpp,*.c,*.h,*.hpp,*.cc,*.cxx call tagbar#autoopen()
    let g:jedi#auto_initialization = 1
    
    " 主题 solarized
    let g:solarized_termtrans=1
    let g:solarized_contrast="normal"
    let g:solarized_visibility="normal"
    " 配色方案
    set background=dark
    set t_Co=256
    colorscheme solarized
    
    " 目录文件导航NERD-Tree
    " \nt 打开nerdree窗口,在左侧栏显示
    nmap <leader>nt :NERDTree<CR>
    let NERDTreeHighlightCursorline=1
    let NERDTreeIgnore=[ '\.pyc$', '\.pyo$', '\.obj$', '\.o$', '\.so$', '\.egg$', '^\.git$', '^\.svn$', '^\.hg$' ]
    let g:netrw_home='~/bak'
    "close vim if the only window left open is a NERDTree
    autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | end
    
    " ctrlp文件搜索
    " 打开ctrlp搜索
    let g:ctrlp_map = '<leader>ff'
    let g:ctrlp_cmd = 'CtrlP'
    " 相当于mru功能,show recently opened files
    map <leader>fp :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_working_path_mode=0
    let g:ctrlp_match_window_bottom=1
    let g:ctrlp_max_height=15
    let g:ctrlp_match_window_reversed=0
    let g:ctrlp_mruf_max=500
    let g:ctrlp_follow_symlinks=1
    
    " vim-powerline美化状态
    " let g:Powerline_symbols = 'fancy'
    let g:Powerline_symbols = 'unicode'
    
    " 括号匹配高亮
    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
    
    " 可视化缩进
    let g:indent_guides_enable_on_vim_startup = 0  " 默认关闭
    let g:indent_guides_guide_size            = 1  " 指定对齐线的尺寸
    let g:indent_guides_start_level           = 2  " 从第二层开始可视化显示缩进    

为了防止配置文件太乱,我们可以通过/.vimrc.bundles管理我们安装的插件。首先创建文件/.vimrc.bundles,然后添加代码如下:

  if &compatible
  set nocompatible " be iMproved _pei
end

filetype off    " required _pei
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()

" Let Vundle manage Vundle
Bundle 'gmarik/vundle'

" Define bundles via Github repos
" 标签导航
Bundle 'majutsushi/tagbar'
Bundle 'vim-scripts/ctags.vim'
" 静态代码分析
Bundle 'scrooloose/syntastic'
" 文件搜索
Bundle 'kien/ctrlp.vim'
" 目录树导航
Bundle "scrooloose/nerdtree"
" 美化状态栏
Bundle "Lokaltog/vim-powerline"
" 主题风格
Bundle "altercation/vim-colors-solarized"
" python自动补全
Bundle 'davidhalter/jedi-vim'
Bundle "klen/python-mode"
" 括号匹配高亮
Bundle 'kien/rainbow_parentheses.vim'
" 可视化缩进
Bundle 'nathanaelkane/vim-indent-guides'
if filereadable(expand("~/.vimrc.bundles.local"))
  source ~/.vimrc.bundles.local
endif
" ..............
Bundle 'sukima/xmledit'
Bundle 'sjl/gundo.vim'
Bundle 'jiangmiao/auto-pairs'
Bundle 'Valloric/YouCompleteMe'
"Bundle 'scrooloose/syntastic'
"Bundle 'scrooloose/nerdcommenter'
Bundle 't9md/vim-quickhl'
"Bundle 'Lokaltog/vim-powerline'

filetype on

我们已经指定好了各个插件的路径,接下里就是安装各个插件了。在shell中输入vim,进入命令行模式输入BundleInstall。 Processing表示正在安装,安装成功后那一行前面会变”+”号

注意:由于tagbar依赖于ctags,所以我们还需要用指令安装ctags:

sudo apt-get install ctags

已经安装好了各个插件,接下里就可以直接用了吗?答案是否定的,我们还需要继续对自己安装的插件进行配置。配置这里也很简单,下面是我的配置,编写~/.vimrc: REF

依次按键\tb,就会调出标签导航;
依次按键\ff,就会调出文件搜索;
依次按键\nt,就会调出目录导航;
ctrl+ww 切换窗口

所有可用颜色在目录,插件安装方法可参考这里 REF

  • Vundle常用命令
    Vundle

  • BundleList -列举出列表中(.vimrc中)配置的所有插件

  • :BundleInstall -安装列表中全部插件

  • :BundleInstall! -更新列表中全部插件

  • :BundleSearch foo -查找foo插件

  • :BundleSearch! foo -刷新foo插件缓存

  • :BundleClean -清除列表中没有的插件 (若要清除,则注释掉.vimrc中的对应内容,再执行此命令即可)

  • :BundleClean! -清除列表中没有的插件

Vi 编辑文件时中文乱码

  • (1) 临时解决办法:

每次打开 vim 后,输入

:set encoding=utf-8
  • (2) 一次性解决办法:

在当前用户目录下,新建 .vimrc文件,在里面添加

:set encoding=utf-8