0%

79. Ubuntu Vim 使用与配置

  • 配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    " ================================================================
    " Author: Dgimo
    " ================================================================

    " 关闭兼容模式, 默认情况下, Vim 会以兼容 Vi 的模式运行, 所以一定要关闭.
    set nocompatible


    " ================================================================
    " 环境判断
    " ================================================================
    " 判断是 Windows 还是 Linux.
    let g:isWindows=0
    let g:isLinux=0
    if (has("win32") || has("win64"))
    let g:isWindows=1
    else
    let g:isLinux=1
    endif
    " 判断是终端还是 gVim
    let g:isGUI=1
    if has("gui_running")
    let g:isGUI=1
    else
    let g:isGUI=0
    endif


    " ================================================================
    " gVim 原有配置
    " ================================================================
    if (g:isWindows)
    " 加载一些默认配置
    source $VIMRUNTIME/vimrc_example.vim
    " 模仿 Windows 快捷键, 例如 ctrl-s, ctrl-c, ctrl-v 等等
    source $VIMRUNTIME/mswin.vim
    " 模仿 Windows 的行为, 这一行很重要, 没有这行的话, 最大化经常出问题.
    behave mswin
    elseif (g:isLinux)
    if (g:isGUI)
    " Source a global configuration file if available
    if filereadable("/etc/vim/gvimrc.local")
    source /etc/vim/gvimrc.local
    endif
    else
    set mouse=a " 在任何模式下启用鼠标
    set t_Co=256 " 在终端启用256色
    set backspace=2 " 设置退格键可用
    " Source a global configuration file if available
    if filereadable("/etc/vim/vimrc.local")
    source /etc/vim/vimrc.local
    endif
    endif
    endif


    " ================================================================
    " UI和基本配置
    " ================================================================
    " 设置字体
    if (g:isWindows)
    set guifont=Consolas:h11
    elseif (g:isLinux)
    set guifont=Monospace\ Regular\ 14
    endif

    " GUI 的设置
    if (g:isGUI)
    " 关闭菜单
    set guioptions-=m
    " 关闭工具栏
    set guioptions-=T
    " 关闭左侧滚动条
    set guioptions-=L
    " 关闭右侧滚动条
    set guioptions-=r
    " 启动时最大化
    autocmd GUIEnter * simalt ~x
    endif

    set nocompatible " required
    filetype off " required

    " set the runtime path to include Vundle and initialize
    set rtp+=~/.vim/bundle/Vundle.vim
    call vundle#begin()

    " alternatively, pass a path where Vundle should install plugins
    "call vundle#begin('~/some/path/here')

    " let Vundle manage Vundle, required
    " Plugin 'gmarik/Vundle.vim'
    Plugin 'VundleVim/Vundle.vim'
    Plugin 'Valloric/YouCompleteMe'
    let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'

    set runtimepath+=~/.vim/bundle/YouCompleteMe
    let g:ycm_collect_identifiers_from_tags_files = 1 " 开启 YCM 基于标签引擎
    let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 注释与字符串中的内容也用于补全
    let g:syntastic_ignore_files=[".*\.py$"]
    let g:ycm_seed_identifiers_with_syntax = 1 " 语法关键字补全
    let g:ycm_complete_in_comments = 1
    let g:ycm_confirm_extra_conf = 0
    let g:ycm_key_list_select_completion = ['<c-n>', '<Down>'] " 映射按键, 没有这个会拦截掉tab, 导致其他插件的tab不能用.
    let g:ycm_key_list_previous_completion = ['<c-p>', '<Up>']
    let g:ycm_complete_in_comments = 1 " 在注释输入中也能补全
    let g:ycm_complete_in_strings = 1 " 在字符串输入中也能补全
    let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 注释和字符串中的文字也会被收入补全
    let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
    let g:ycm_show_diagnostics_ui = 0 " 禁用语法检查
    inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<CR>" | " 回车即选中当前项
    nnoremap <c-j> :YcmCompleter GoToDefinitionElseDeclaration<CR>| " 跳转到定义处
    "let g:ycm_min_num_of_chars_for_completion=2 " 从第2个键入字符就开始罗列匹配项
    highlight PMenu ctermfg=0 ctermbg=242 guifg=black guibg=darkgrey
    highlight PMenuSel ctermfg=242 ctermbg=8 guifg=darkgrey guibg=black

    " Add all your plugins here (note older versions of Vundle used Bundle instead of Plugin)
    " All of your Plugins must be added before the following line
    call vundle#end() " required
    filetype plugin indent on " required


    " 只在需要的时候才重新绘制界面(例如宏执行过程中不需要重绘界面)
    set lazyredraw
    " 发生错误时不要响铃, 也不要闪烁
    set noerrorbells
    set belloff=all

    " 分割窗口时保持相等的宽/高
    set equalalways
    " 竖直split时,在右边开启
    set splitright
    " 水平split时,在下边开启
    set splitbelow
    " 全局复制粘贴
    set clipboard=unnamedplus

    " ================================================================
    " 编码
    " ================================================================
    " 设置vim内部编码
    set encoding=utf-8
    " 设置编辑文件时的编码
    set fileencoding=utf-8
    " 设置 Vim 能识别的编码
    set fileencodings=ucs-bom,utf-8,cp936,gb18030,gb2312,big5,cuc-jp,cuc-kr,latin
    " 设置终端模式(非 GUI 模式)下的编码
    set termencoding=utf-8
    " 防止特殊符号无法显示
    set ambiwidth=double
    " 解决 console 输出乱码
    language messages zh_CN.utf-8


    " ================================================================
    " 文件相关配置
    "
    " 关于备份文件的说明: 备份文件, 临时文件, undo 文件, 最好的做法是把
    " 他们配置到单独的文件夹里, 我这里暂时都不让生成了, 以后可以按需要修改.
    " 例如:
    " set directory=~/.vim/.swp//
    " set backupdir=~/.vim/.backup//
    " set undodir=~/.vim/.undo//
    " 注意最后要有两道//, 表示文件名使用绝对路径
    " ================================================================
    " 自动检测文件类型和缩进格式, 并根据文件类型加载插件
    filetype plugin indent on
    " 文件被外部改动后, 自动加载
    set autoread
    " 不生成备份文件
    set nobackup
    " 不生成临时文件
    set noswapfile
    " 不生成 undo 文件
    set noundofile


    " ================================================================
    " 编辑器配置
    " ================================================================
    " 显示行号
    set number
    " 显示语法高亮
    syntax enable
    syntax on
    " 显示 tab(>---), 空格(^), 换行(¬)
    set list
    set listchars=tab:>-,trail:^ ",eol:¬
    " 突出显示当前行
    set cursorline
    " 开启自动缩进
    set autoindent
    " 智能缩进
    set smartindent

    " 编辑时按一个 Tab 键相当于输入4个空格
    set tabstop=4
    " 格式化时缩进尺寸为4个空格
    set shiftwidth=4
    " 让 Vim 把连续的空格视为一个 Tab, 删除时可以一次删掉一个 Tab 的空格数量
    set softtabstop=4
    " 把制表符转换为多个空格, 具体空格数量参考 tabstop 和 shiftwidth
    set expandtab
    " 在行和段的开始出使用 Tab
    set smarttab

    " 合并两行中文时, 不在中间加空格
    set formatoptions+=B
    " 合并行时不添加多余空格
    set nojoinspaces


    " ================================================================
    " 搜索和匹配
    " ================================================================
    " 高亮显示匹配的括号
    set showmatch
    " 高亮显示搜索到的关键字
    set hlsearch
    " 即时搜索
    set incsearch
    " 智能大小写敏感, 只要有一个字母大写, 就大小写敏感, 否则不敏感
    set ignorecase smartcase


    " ================================================================
    " 操作习惯和快捷键
    " ================================================================
    " 将 kk 配置成 esc
    inoremap kk <esc>
    " ctrl-h 光标左移一格
    inoremap <c-h> <left>
    " ctrl-j 垂直下移一行
    " inoremap <c-j> <down>
    " 下面这个组合比较巧妙, ctrl-o 可以进入临时命令模式, 然后执行一次 gj
    inoremap <c-j> <c-o>gj
    " ctrl-k 垂直上移一行
    " inoremap <c-k> <up>
    inoremap <c-k> <c-o>gk
    " ctrl-l 光标右移一格
    inoremap <c-l> <right>
    " 按 U 执行 redo
    " noremap U <c-r>
    " shift 回车, 在下一行输入
    inoremap <s-cr> <end><cr>
    " ctrl 回车,行尾
    inoremap <c-cr> <esc>A
    " 在可视模式下使用p粘贴时不替换寄存器内容, 这里是利用了黑洞寄存器
    vnoremap p "_dP
    " 对于很长的行, vim会自动换行, 此时 j 或者 k 就会一下跳很多行,
    " 使用 gk,gj 可以避免跳过多行, 但是不方便, 所以做了如下映射.
    nnoremap k gk
    nnoremap j gj
    vnoremap k gk
    vnoremap j gj
    " 按 gb 跳转到前一个标签页, 默认 gT, 按起来不方便
    nnoremap gb gT


    " ================================================================
    " 主题
    " ================================================================
    colorscheme monokai " 设置主题为 molokai

    " 括号补全
    inoremap ( ()<ESC>i
    inoremap [ []<ESC>i
    inoremap { {}<ESC>i
    inoremap < <><ESC>i
    inoremap ' ''<ESC>i
    inoremap " ""<ESC>i
  • 特殊配置

  1. 括号补全
    1
    2
    3
    4
    5
    6
    7
    " 括号补全
    inoremap ( ()<ESC>i
    inoremap [ []<ESC>i
    inoremap { {}<ESC>i
    inoremap < <><ESC>i
    inoremap ' ''<ESC>i
    inoremap " ""<ESC>i
  2. YouCompleteMe
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    set nocompatible              " required
    filetype off " required

    " set the runtime path to include Vundle and initialize
    set rtp+=~/.vim/bundle/Vundle.vim
    call vundle#begin()

    " alternatively, pass a path where Vundle should install plugins
    "call vundle#begin('~/some/path/here')

    " let Vundle manage Vundle, required
    " Plugin 'gmarik/Vundle.vim'
    Plugin 'VundleVim/Vundle.vim'
    Plugin 'Valloric/YouCompleteMe'
    let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'

    set runtimepath+=~/.vim/bundle/YouCompleteMe
    let g:ycm_collect_identifiers_from_tags_files = 1 " 开启 YCM 基于标签引擎
    let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 注释与字符串中的内容也用于补全
    let g:syntastic_ignore_files=[".*\.py$"]
    let g:ycm_seed_identifiers_with_syntax = 1 " 语法关键字补全
    let g:ycm_complete_in_comments = 1
    let g:ycm_confirm_extra_conf = 0
    let g:ycm_key_list_select_completion = ['<c-n>', '<Down>'] " 映射按键, 没有这个会拦截掉tab, 导致其他插件的tab不能用.
    let g:ycm_key_list_previous_completion = ['<c-p>', '<Up>']
    let g:ycm_complete_in_comments = 1 " 在注释输入中也能补全
    let g:ycm_complete_in_strings = 1 " 在字符串输入中也能补全
    let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 注释和字符串中的文字也会被收入补全
    let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
    let g:ycm_show_diagnostics_ui = 0 " 禁用语法检查
    inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<CR>" | " 回车即选中当前项
    nnoremap <c-j> :YcmCompleter GoToDefinitionElseDeclaration<CR>| " 跳转到定义处
    "let g:ycm_min_num_of_chars_for_completion=2 " 从第2个键入字符就开始罗列匹配项
    highlight PMenu ctermfg=0 ctermbg=242 guifg=black guibg=darkgrey
    highlight PMenuSel ctermfg=242 ctermbg=8 guifg=darkgrey guibg=black

    " Add all your plugins here (note older versions of Vundle used Bundle instead of Plugin)
    " All of your Plugins must be added before the following line
    call vundle#end() " required
    filetype plugin indent on " required

    【注意】:放到开头

  • 安装插件

  1. git
    1. git —version
    2. sudp apt-get install git
  2. cmake
    1. sudo apt-get install cmake
  3. Vundle
    1. git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
    2. cd ~
    3. sudo gedit /usr/share/vim/vimrc
    4. 编辑配置文件,在文件的开头添加如下代码:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      set nocompatible        "  必需
      filetype off            " 必需
      "  将运行时的路径设置为包括Vundle并初始化
      set rtp+=~/.vim/bundle/Vundle.vim
      call vundle#begin()
      " 使Vuldle管理自己
      Plugin ‘VundleVim/Vundle.vim’
      "最后要写入YouCompleteMe管理语句的位置
      " 你所有的插件必需在这一行之前添加
      call vundle#end()            “ required
      filetype plugin indent on    “ required
    5. 启动Vim,输入 :PluginInstall
    6. 如果显示 Done 则说明安装成功
  4. YouCompleteMe
    1. cd ~/.vim/bundle
    2. git clone https://github.com/Valloric/YouCompleteMe.git
    3. cd ~/.vim/bundle/YouCompleteMe
    4. ./install.py --clang-completer
    5. .vimrc中加入:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      " YouCompleteMe
      set runtimepath+=~/.vim/bundle/YouCompleteMe
      let g:ycm_collect_identifiers_from_tags_files = 1 " 开启 YCM 基于标签引擎
      let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 注释与字符串中的内容也用于补全
      let g:syntastic_ignore_files=[".*\.py$"]
      let g:ycm_seed_identifiers_with_syntax = 1 " 语法关键字补全
      let g:ycm_complete_in_comments = 1
      let g:ycm_confirm_extra_conf = 0
      let g:ycm_key_list_select_completion = ['<c-n>', '<Down>'] " 映射按键, 没有这个会拦截掉tab, 导致其他插件的tab不能用.
      let g:ycm_key_list_previous_completion = ['<c-p>', '<Up>']
      let g:ycm_complete_in_comments = 1 " 在注释输入中也能补全
      let g:ycm_complete_in_strings = 1 " 在字符串输入中也能补全
      let g:ycm_collect_identifiers_from_comments_and_strings = 1 " 注释和字符串中的文字也会被收入补全
      let g:ycm_global_ycm_extra_conf='~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
      let g:ycm_show_diagnostics_ui = 0 " 禁用语法检查
      inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<CR>" | " 回车即选中当前项
      nnoremap <c-j> :YcmCompleter GoToDefinitionElseDeclaration<CR>| " 跳转到定义处
      "let g:ycm_min_num_of_chars_for_completion=2 " 从第2个键入字符就开始罗列匹配项
  • YouCompleteMe使用

  1. 快捷键
    1. ctrl + n:向下选择
    2. ctrl + p:向上选择