vim

Vim for Myself

Final Config

Posted by Zz on March 10, 2021
vim

Intro

macOs 终极终端+编辑器配置,以后不再折腾了。。。

Oh-my-zsh + iterm2 有现成的 : https://github.com/sirius1024/iterm2-with-oh-my-zsh

本文主要记录 Vim。

1 Plug

1.1 vim-plug

首先安装 vim-plug,这是用来管理 vim 插件的工具,安装方法很简单:

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

使用时,在 .vimrc 中加入两行:

call plug#begin('~/.vim/plugged')
call plug#end()

中间为需要使用的插件代码。

:PlugInstall 就可以安装插件,:PlugUpdate 更新插件。

1.2 deoplete

代码补全插件,neocomplete 的升级版,由于 neocomplete 在 vim8 之后不适用,因此只能用这个。官方链接:https://github.com/Shougo/deoplete.nvim

if has('nvim')
  Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
else
  Plug 'Shougo/deoplete.nvim'
  Plug 'roxma/nvim-yarp'
  Plug 'roxma/vim-hug-neovim-rpc'
endif
let g:deoplete#enable_at_startup = 1

vim9+py3.12后又出现了新的问题,解决方法参考:https://github.com/neovim/pynvim/issues/538

# 安装最新master的pynvim
pip install 'pynvim @ git+https://github.com/neovim/pynvim'

1.3 Airline

状态栏美化插件,功能比较多,需要安装额外的插件,官方链接:https://github.com/vim-airline/vim-airline

Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'airblade/vim-gitgutter'
Plug 'mhinz/vim-signify'
Plug 'neoclide/coc-git'
Plug 'tpope/vim-fugitive'
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#branch = ''
let g:airline_symbols#readonly = ''
let g:airline_symbols#linenr = '☰'
let g:airline_symbols#maxlinenr = ''
let g:airline_symbols#dirty='⚡'
let g:airline_theme = 'solarized'
let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#left_sep = ' '
let g:airline#extensions#tabline#left_alt_sep = '|'
let g:airline#extensions#tabline#formatter = 'default'
let g:airline_powerline_fonts = 1
let g:airline#extensions#tabline#exclude_preview = 1

乱码字符是需要安装 powerline,这些设置在 :help airline 中都能找到。

1.4 easymotion

精准快速跳转插件,官方链接:https://github.com/easymotion/vim-easymotion.

Plug 'easymotion/vim-easymotion'

使用方法:

  1. <Leader><Leader>+w:前向跳转;
  2. <Leader><Leader>+s+第一个字符+具体位置:双向跳转

这里我把 <Leader>设置为空格,下面配置文件会有。

1.5 surround

快速更改或删除字符所在的括号,官方链接:https://github.com/tpope/vim-surround。

Plug 'tpope/vim-surround'

使用方法:

  1. 更改:c+s+源括号+目标括号;
  2. 删除:d+s+源括号;

1.6 Copilot Vim

该插件为基于Copilot的自动补全代码的vim插件

cd ~/.vim/plugged
git clone https://github.com/github/copilot.vim.git

# step1. 在vimrc中添加
Plug 'github/copilot.vim'

# step2. 在vim命令行中执行
:Copilot setup
:Copilot enable

2 Customerized Config

以下是我自己的个性化配置,包括 UI,快捷键组合,以及一些其他优秀 vim 配置的优秀功能等。

2.1 Restore

function! ResCur()
  if line("'\"") <= line("$")
    silent! normal! g`"
    return 1
  endif
endfunction

augroup resCur
  autocmd!
  autocmd BufWinEnter * call ResCur()
augroup END

2.2 Undo

set backup                  " Backups are nice ...
if has('persistent_undo')
  set undofile                " So is persistent undo ...
  set undolevels=1000         " Maximum number of changes that can be undone
  set undoreload=10000        " Maximum number lines to save for undo on a buffer reload
endif

2.3 Mapping

nnoremap ; :
map <C-J> <C-W>j<C-W>_
map <C-K> <C-W>k<C-W>_
map <C-L> <C-W>l<C-W>_
map <C-H> <C-W>h<C-W>_
let mapleader = "\<space>"
inoremap <silent><expr> <TAB>  pumvisible() ? "\<C-n>" : "\<TAB>" #设置自动补全 tab 选择

2.4 UI

  • 主题:solarized,需要下载,链接:https://github.com/altercation/vim-colors-solarized

  • 高亮光标所在行列,标记最大列位置:

    set colorcolumn=80
    set cursorcolumn
    set cursorline
    
  • 显示命令备选:

    set wildmenu
    set wildmode=list:longest,full
    
  • 显示行尾空格并且保存时自动删除行尾空格:

    highlight WhitespaceEOL ctermbg=red guibg=red
    match WhitespaceEOL /\s\+$/
    autocmd BufWritePre * :%s/\s\+$//e
    
  • 设置 tab 为两个空格,自动缩进并且缩进为两个空格:

    set autoindent
    set shiftwidth=2
    set expandtab
    set tabstop=2
    set softtabstop=2
    
  • 设置 backspace 删除:

    set backspace=indent,eol,start
    
  • 设置搜索匹配项高亮:

    set hlsearch
    
  • 设置 ctags 和 vs 相对路径:

    set tags=tags;
      
    set autochdir
    
  • 设置 vim-gitgutter 更新频率:

    set updatetime=100
    
  • 设置 cuda 语法高亮(vim8 无需设置):

    将 cu.vim.txt 放到 /usr/share/vim/vim*/syntax,改名为 cu.vim

    au BufNewFile,BufRead *.cu set ft=cu
    au BufNewFile,BufRead *.cuh set ft=cu
    

3 Full Config

" UI
syntax on
syntax enable
set background=dark
colorscheme solarized
set number
set relativenumber
set colorcolumn=80
set cursorcolumn
set cursorline
set backspace=indent,eol,start
hi ColorColumn ctermbg=0
hi CursorLine cterm=None ctermbg=0
hi CursorLineNr cterm=bold ctermbg=0
hi CursorColumn ctermbg=0
set mouse=a
filetype plugin indent on
set wildmenu
set wildmode=list:longest,full
set autoindent
set shiftwidth=2
set expandtab
set tabstop=2
set softtabstop=2
set hlsearch
set tags=tags;
set updatetime=100
set autochdir
highlight WhitespaceEOL ctermbg=red guibg=red
match WhitespaceEOL /\s\+$/
autocmd BufWritePre * :%s/\s\+$//e
" au BufNewFile,BufRead *.cu set ft=cu
" au BufNewFile,BufRead *.cuh set ft=cu

" customized
" 1. restore the position of cursor
function! ResCur()
  if line("'\"") <= line("$")
    silent! normal! g`"
    return 1
  endif
endfunction

augroup resCur
  autocmd!
  autocmd BufWinEnter * call ResCur()
augroup END

" 2. undo file
" set backup                  " Backups are nice ...
if has('persistent_undo')
  set undofile                " So is persistent undo ...
  set undolevels=1000         " Maximum number of changes that can be undone
  set undoreload=10000        " Maximum number lines to save for undo on a buffer reload
  set undodir=~/.vimundo
endif

" Mapping
nnoremap ; :
map <C-J> <C-W>j<C-W>_
map <C-K> <C-W>k<C-W>_
map <C-L> <C-W>l<C-W>_
map <C-H> <C-W>h<C-W>_
let mapleader = "\<space>"
inoremap <silent><expr> <TAB>  pumvisible() ? "\<C-n>" : "\<TAB>"

" Plug
call plug#begin('~/.vim/plugged')

" deoplete
if has('nvim')
  Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
else
  Plug 'Shougo/deoplete.nvim'
  Plug 'roxma/nvim-yarp'
  Plug 'roxma/vim-hug-neovim-rpc'
endif
let g:deoplete#enable_at_startup = 1

“ Copilot
Plug 'github/copilot.vim'

" airline
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'airblade/vim-gitgutter'
Plug 'mhinz/vim-signify'
Plug 'neoclide/coc-git'
Plug 'tpope/vim-fugitive'
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#branch = ''
let g:airline_symbols#readonly = ''
let g:airline_symbols#linenr = '☰'
let g:airline_symbols#maxlinenr = ''
let g:airline_symbols#dirty='⚡'
let g:airline_theme = 'solarized'
let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#left_sep = ' '
let g:airline#extensions#tabline#left_alt_sep = '|'
let g:airline#extensions#tabline#formatter = 'default'
let g:airline_powerline_fonts = 1
let g:airline#extensions#tabline#exclude_preview = 1

" easymotion
Plug 'easymotion/vim-easymotion'

" surround
Plug 'tpope/vim-surround'
call plug#end()