每次新电脑,win配置一遍vim,WSL配置一遍,WSL的root又要再复制一遍。理一下流程。
1. 安装
下载vim90安装包,一路next安装。添加环境变量。
https://github.com/vim/vim-win32-installer/releases/download/
2. 配置文件
win为_vimrc
,linux为.vimrc
。
" Vim with all enhancements
source $VIMRUNTIME/vimrc_example.vim " Use the internal diff if available.
" Otherwise use the special 'diffexpr' for Windows.
if &diffopt !~# 'internal'
set diffexpr=MyDiff()
endif
function MyDiff()
let opt = '-a --binary '
if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
let arg1 = v:fname_in
if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
let arg1 = substitute(arg1, '!', '\!', 'g')
let arg2 = v:fname_new
if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
let arg2 = substitute(arg2, '!', '\!', 'g')
let arg3 = v:fname_out
if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
let arg3 = substitute(arg3, '!', '\!', 'g')
if $VIMRUNTIME =~ ' '
if &sh =~ '\<cmd'
if empty(&shellxquote)
let l:shxq_sav = ''
set shellxquote&
endif
let cmd = '"' . $VIMRUNTIME . '\diff"'
else
let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
endif
else
let cmd = $VIMRUNTIME . '\diff'
endif
let cmd = substitute(cmd, '!', '\!', 'g')
silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3
if exists('l:shxq_sav')
let &shellxquote=l:shxq_sav
endif
endfunction
" my setting, 2021/03/03
" leader key
let mapleader=";"
" 开启文件类型侦测
filetype on
" 根据侦测到的不同类型加载对应的插件
filetype plugin on
" 设置快捷键将选中文本块复制至系统剪贴板
vnoremap <Leader>y "+y
" 设置快捷键将系统剪贴板内容粘贴至 vim
nmap <Leader>p "+p
" 定义快捷键关闭当前分割窗口
nmap <Leader>q :q<CR>
" 定义快捷键保存所有窗口内容
nmap <Leader>w :w<CR>
" 定义快捷键保存所有窗口内容并退出 vim
nmap <Leader>wq :wa<CR>:q<CR>
" 不做任何保存直接退出 vim
nmap <Leader>Q :qa!<CR>
" 定义快捷键在结对符之间跳转
nmap <Leader>M %
" 跳转至右方的窗口
nnoremap <Leader>lw <C-W>l
" 跳转至左方的窗口
nnoremap <Leader>hw <C-W>h
" 跳转至上方的子窗口
nnoremap <Leader>kw <C-W>k
" 跳转至下方的子窗口
nnoremap <Leader>jw <C-W>j
" 让配置变更立即生效
autocmd BufWritePost $MYVIMRC source $MYVIMRC
" 开启实时搜索功能
set incsearch
" 搜索时大小写不敏感
set ignorecase
" 关闭兼容模式
set nocompatible
" vim 自身命令行模式智能补全
set wildmenu
" 配色方案
set background=dark
colorscheme molokai
" 禁止光标闪烁
set gcr=a:block-blinkon0
" 禁止显示滚动条
set guioptions-=l
set guioptions-=L
set guioptions-=r
set guioptions-=R
" 禁止显示菜单和工具条
set guioptions-=m
set guioptions-=T
" 总是显示状态栏
set laststatus=2
" 显示光标当前位置
set ruler
" 开启行号显示
set number
" 高亮显示当前行/列
set cursorline
" set cursorcolumn
" 高亮显示搜索结果
set hlsearch
" 设置 gvim 显示字体
" set guifont=YaHei\ Consolas\ Hybrid\ 20 " this is linux setting
" set guifont=Sarasa_Term_SC:h12
" 设置光标
set guicursor=a:block-blinkon0
" Necessary to show Unicode glyphs
set encoding=utf-8
" Explicitly tell Vim that the terminal supports 256 colors
set t_Co=256
" highlight 80 column
set cc=80
" 将制表符扩展为空格
set expandtab
" 设置编辑时制表符占用空格数
set tabstop=4
" 设置格式化时制表符占用空格数
set shiftwidth=4
" 让 vim 把连续数量的空格视为一个制表符
set softtabstop=4
" 自动补全
inoremap ( ()<ESC>i
inoremap { {}<ESC>i
inoremap [ []<ESC>i
""inoremap " ""<ESC>i
""inoremap ' ''<ESC>i
" 打开文件时恢复光标位置
autocmd BufReadPost *
\ if line("'\"") > 1 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
" 在 Vim 8 中安装matchit
packadd! matchit
" 括号匹配高亮颜色
"hi MatchParen ctermbg=16 ctermfg=226 guibg=lightblue
" no ~ tmp file
set nobackup
set nowritebackup
set noswapfile
set noundofile
" json highlight
autocmd BufNewFile,BufRead *.json set ft=javascript
" vundle 环境设置
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
" vundle 管理的插件列表必须位于 vundle#begin() 和 vundle#end() 之间
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'tomasr/molokai'
Plugin 'octol/vim-cpp-enhanced-highlight'
Plugin 'Lokaltog/vim-powerline'
Plugin 'Yggdroot/indentLine'
Plugin 'nathanaelkane/vim-indent-guides'
" 插件列表结束
call vundle#end()
filetype plugin indent on
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""powerline setting
" 设置状态栏主题风格
let g:Powerline_colorscheme='solarized16'
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""indentLine setting
let g:indentLine_enabled = 0
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" vim-indent-guides setting
" 随 vim 自启动
let g:indent_guides_enable_on_vim_startup=1
" " 从第二层开始可视化显示缩进
let g:indent_guides_start_level=2
" " 色块宽度
let g:indent_guides_guide_size=1
" " 快捷键 i 开/关缩进可视化
:nmap <silent> <Leader>i <Plug>IndentGuidesToggle
" fortran, f90, free format
let s:extfname = expand("%:e")
if s:extfname ==? "f90"
let fortran_free_source = 1
unlet! fortran_fixed_source
endif
3. 插件管理
- 下载插件管理工具vundle
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
插件安装::PluginInstall
,常用插件见_vimrc
。
注:配色方案molokai
无法直接生效,;w
保存以后才生效。解决方法:
- win: 复制
~\.vim\bundle\molokai\colors\molokai.vim
到~/vimfiles/colors/
- linux:
cp -r ~/.vim/bundle/molokai/colors ~/.vim/
参考:
https://github.com/yangyangwithgnu/use_vim_as_ide
https://blog.csdn.net/weixin_43238031/article/details/88539489