工欲善其事,必先利其器,倒腾一下vim的配置,做个记录。
".vimrc里的内容:
""===============for base configure==============
set t_Co=256
if ! has("gui_running")
set t_Co=256
endif
if &diff
highlight DiffAdd cterm=bold ctermfg=10 ctermbg=17 gui=none guifg=bg guibg=Red
highlight DiffDelete cterm=bold ctermfg=10 ctermbg=17 gui=none guifg=bg guibg=Red
highlight DiffChange cterm=bold ctermfg=10 ctermbg=17 gui=none guifg=bg guibg=Red
highlight DiffText cterm=bold ctermfg=10 ctermbg=88 gui=none guifg=bg guibg=Red
endif
set ts=4
set sw=4
" set mouse=a
set expandtab
set autoindent
autocmd BufNewFile, BufRead *.c, *.cpp, *.java :CFOLD
set nu
set smartindent
set showmatch
set ruler "??????????
set nohls "?????????
set incsearch "??????
set foldenable "??????
set backspace=indent,eol,start
syntax on "????
"set background=dark
"colorscheme gruvbox
set fileencodings=utf-8,gb2312,gb18030,gbk,ucs-bom,cp936,latin1
set termencoding=utf-8
set fileformats=unix
set encoding=prc
set hlsearch
"set nohlsearch
set encoding=utf-8
set clipboard=unnamedplus
set cursorline
"hi CursorLine term=bold cterm=bold ctermbg=237
filetype plugin on
"ctags
set tags=tags;
set autochdir
"" Set mapleader
let mapleader = ","
"nnoremap / /\v
"nnoremap ? ?\v
"map w /\v<><Left>
"nnoremap <F3> :norm yiw<CR>:/<C-R>"<CR><CR>
nnoremap <F3> :/<C-R><C-W><CR><CR>
"***********************************************
""键盘命令,
""若是搭配secureCRT用系统剪切复制功能需要安装xming
""否则容易出错:
""Error: Can't open display: localhost:x.0
"***********************************************
"nmap <leader>w :w!<cr>
"nmap <leader>f :find<cr>
" 映射全选+复制 ctrl+a
map <C-A> ggVGY
map! <C-A> <Esc>ggVGY
" 选中状态下 Ctrl+c 复制
"vmap <C-c> "+y"
vnoremap <C-c> :w !xclip -selection clipboard<CR>
" ==================for plugin==================
call plug#begin('~/.vim/plugged')
Plug 'itchyny/lightline.vim'
Plug 'rking/ag.vim'
Plug 'mileszs/ack.vim'
Plug 'dyng/ctrlsf.vim'
Plug 'scrooloose/nerdtree'
Plug 'Valloric/YouCompleteMe'
Plug 'majutsushi/tagbar'
Plug 'vim-airline/vim-airline'
Plug 'preservim/nerdcommenter'
Plug 'frazrepo/vim-rainbow'
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
Plug 'junegunn/fzf.vim'
Plug 'christoomey/vim-system-copy'
Plug 'Yggdroot/LeaderF', { 'do': './install.sh' }
Plug 'rrethy/vim-illuminate'
Plug 'yegappan/mru'
Plug 'vim-scripts/DrawIt'
Plug 'morhetz/gruvbox'
Plug 'ludovicchabant/vim-gutentags'
Plug 'airblade/vim-gitgutter'
Plug 'neoclide/vim-easygit'
Plug 'tpope/vim-fugitive'
Plug 'w0rp/ale'
Plug 'godlygeek/tabular'
Plug 'tpope/vim-surround'
Plug 'Yggdroot/indentLine'
call plug#end()
"===================for ag search==============="
" for easy using sliver search
" nmap <leader>f :norm yiw<CR>:Ag! -t -Q "<C-R>""
nmap <leader>r :norm yiw<CR>:Ag! -t "\b<C-R>"\b"
"配置ag从当前项目开始搜寻
let g:ag_working_path_mode="r"
let g:ag_highlight=1
" for easy using sliver search
nmap <leader>f :norm yiw<CR>:Ag! -t -Q "<C-R>""
" Locate and return character "above" current cursor position.
function! LookUpwards()
let column_num = virtcol('.')
let target_pattern = '\%' . column_num . 'v.'
let target_line_num = search(target_pattern . '*\S', 'bnW')
if !target_line_num
return""
else
return matchstr(getline(target_line_num), target_pattern)
endif
endfunction
imap <silent> <C-Y> <C-R><C-R>=LookUpwards()<CR>
"ag
nnoremap <leader>ag :Ag! ""<left>
"ack
let g:ackprg = 'ag --nogroup --nocolor --column'
"ctrlsf
nnoremap <Leader>c :CtrlSF <Space>
map <s-j> <c-j>p
map <s-k> <c-k>p
"let g:ctrlsf_ackprg = 'rg'
let g:ctrlsf_default_view_mode = 'compact'
let g:ctrlsf_search_mode = 'async'
let g:ctrlsf_auto_close = {
\ "normal" : 1,
\ "compact": 0
\ }
let g:ctrlsf_auto_focus = {
\ "at" : "start",
\ }
let g:ctrlsf_absolute_file_path = 0
let g:ctrlsf_auto_preview = 1
let g:ctrlsf_confirm_save = 1
" nmap <C-F>f <Plug>CtrlSFPrompt
" vmap <C-F>f <Plug>CtrlSFVwordPath
" vmap <C-F>F <Plug>CtrlSFVwordExec
" nmap <C-F>n <Plug>CtrlSFCwordPath
" nmap <C-F>p <Plug>CtrlSFPwordPath
" nnoremap <C-F>o :CtrlSFOpen<CR>
" nnoremap <C-F>t :CtrlSFToggle<CR>
" inoremap <C-F>t <Esc>:CtrlSFToggle<CR>
"plugin nerdtree===============================
let NERDChristmasTree=0
let NERDTreeWinSize=35
let NERDTreeChDirMode=2
let NERDTreeIgnore=['\~$', '\.pyc$', '\.swp$']
let NERDTreeShowBookmarks=1
let NERDTreeWinPos="left"
"鼠标模式:目录单击,文件双击
let NERDTreeMouseMode=2
"Automatically open a NERDTree if no files where specified
"autocmd vimenter * if !argc() | NERDTree | endif
"Close vim if the only window left open is a NERDTree
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif
" Open a NERDTree
nmap <F1> :NERDTreeToggle<CR>
nmap <F2> :NERDTreeFind<CR>
"plugin nerdtree==============================
"plugin ycm===================================
"设置跳转到方法/函数定义的快捷键
nnoremap <leader>j :YcmCompleter GoToDefinitionElseDeclaration<CR>
"触发补全快捷键
let g:ycm_key_list_select_completion = ['<TAB>', '<c-n>', '<Down>']
let g:ycm_key_list_previous_completion = ['<S-TAB>', '<c-p>', '<Up>']
let g:ycm_auto_trigger = 1
"最小自动触发补全的字符大小设置为 3
let g:ycm_min_num_of_chars_for_completion = 3
"YCM的previw窗口比较恼人,还是关闭比较好
set completeopt-=preview
"plugin ycm end================================
"plugin tagbar=================================
nmap <F9> :TagbarToggle<CR>
" tagbar默认去这个目录中寻找ctags,ctags的默认安装路径也是这个目录
let g:tagbar_ctags_bin='/usr/bin/ctags' " Proper Ctags locations
let g:tagbar_width=36 " Default is 40, seems too wide
noremap <Leader>y :TagbarToggle<CR> " Display panel with (,y)
"启动 时自动focus
let g:tagbar_autofocus = 1
"plugin tagbar end=============================
"plugin airline================================
let g:airline_powerline_fonts=1
"let g:airline#extensions#tabline#enabled = 1 "tabline中当前buffer两端的分隔字符
nmap <tab> :bn<cr> " 设置tab键映射"
"plugin airline end=============================
"plugin nerd comment============================
let NERDSpaceDelims=1 " 让注释符与语句之间留一个空格
let NERDCompactSexyComs=1 " 多行注释时样子更好看
let g:NERDDefaultAlign = 'left' "将行注释符左对齐
" map <F4><leader>ci<cr> "加注释
"nmap <leader>cu "解开注释
"nmap <leader>ca "切换注释的样式:/*....*/和//..的切换
"nmap <leader>c<space> "加上/解开注释, 智能判断
"nmap <leader>cy "先复制, 再注解(p可以进行黏贴)
"nmap <leader>cs "'性感的'注释(我很喜欢这个!)
"plugin nerd comment end=========================
"plugin rainbow==================================
let g:rainbow_active = 1
let g:rainbow_load_separately = [
\ [ '*' , [['(', ')'], ['\[', '\]'], ['{', '}']] ],
\ [ '*.tex' , [['(', ')'], ['\[', '\]']] ],
\ [ '*.cpp' , [['(', ')'], ['\[', '\]'], ['{', '}']] ],
\ [ '*.{html,htm}' , [['(', ')'], ['\[', '\]'], ['{', '}'], ['<\a[^>]*>', '</[^>]*>']] ],
\ ]
let g:rainbow_guifgs = ['RoyalBlue3', 'DarkOrange3', 'DarkOrchid3', 'FireBrick']
let g:rainbow_ctermfgs = ['lightblue', 'lightgreen', 'yellow', 'red', 'magenta']
"plugin rainbow end==============================
"plugin ale======================================
let g:ale_sign_column_always = 1
let g:ale_set_highlights = 0
"自定义error和warning图标
let g:ale_sign_error = '✗'
let g:ale_sign_warning = '⚡'
"在vim自带的状态栏中整合ale
let g:ale_statusline_format = ['✗ %d', '⚡ %d', '✔ OK']
"显示Linter名称,出错或警告等相关信息
let g:ale_echo_msg_error_str = 'E'
let g:ale_echo_msg_warning_str = 'W'
let g:ale_echo_msg_format = '[%linter%] %s [%severity%]'
"普通模式下,sp前往上一个错误或警告,sn前往下一个错误或警告
nmap sp <Plug>(ale_previous_wrap)
nmap sn <Plug>(ale_next_wrap)
"<Leader>s触发/关闭语法检查
nmap <Leader>s :ALEToggle<CR>
"<Leader>d查看错误或警告的详细信息
nmap <Leader>d :ALEDetail<CR>
"文件内容发生变化时不进行检查
let g:ale_lint_on_text_changed = 'never'
let g:ale_lint_on_insert_leave = 0
"打开文件时不进行检查
let g:ale_lint_on_enter = 0
"对于Java如果安装在中文的系统上,错误和警告信息都会乱码,可以进行下面的设置
let g:ale_java_javac_options = '-encoding UTF-8 -J-Duser.language=en'
"使用clang对c和c++进行语法检查,对python使用pylint进行语法检查
let g:ale_linters = {
\ 'c++': ['clang', 'gcc'],
\ 'c': ['clang', 'gcc'],
\ 'python': ['pylint'],
\}
"plugin ale end==================================
"plug fzf=========================================
"<Leader>f在当前目录搜索文件
nnoremap <silent> <Leader>g :Files<CR>
""<Leader>b切换Buffer中的文件
nnoremap <silent> <Leader>b :Buffers<CR>
"<Leader>p在当前所有加载的Buffer中搜索包含目标词的所有行,:BLines只在当前Buffer中搜索
nnoremap <silent> <Leader>p :Lines<CR>
"<Leader>h,改造了下可以调出上次搜索的结果,fzf自身没提供solution,需要自己想办法
nnoremap <silent> <Leader>h :Find<CR>
function! s:remember(command)
execute a:command
" Or save it to a file for persistence
let g:prev_command = a:command
endfunction
command! -nargs=* Remember call s:remember(<q-args>)
command! Redo execute g:prev_command
let cmd = 'rg -g "*.cpp" -g "*.h" -g "*.c" -g "*.java" -g "*.bp" -g "*.mk"
\ --glob "!.git/*" --glob "!.repo/*" --glob "!tags" --glob "!.go"
\ --column --line-number --no-heading --color=always --smart-case
\ --no-ignore --no-ignore-dot '
command! -bang -nargs=* Rg
\ call fzf#vim#grep(cmd.shellescape(<q-args>)."| tee /tmp/fzf-history/result", 1, <bang>0)
command! -bang -nargs=* Find
\ call fzf#vim#grep('cat /tmp/fzf-history/result | tr -d "\017"', 1, <bang>0)
" Mapping selecting mappings
nmap <leader><tab> <plug>(fzf-maps-n)
xmap <leader><tab> <plug>(fzf-maps-x)
omap <leader><tab> <plug>(fzf-maps-o)
" Insert mode completion
imap <c-x><c-k> <plug>(fzf-complete-word)
imap <c-x><c-f> <plug>(fzf-complete-path)
imap <c-x><c-l> <plug>(fzf-complete-line)"
nnoremap <silent> <Leader>z :Rg <C-R><C-W><CR>
xnoremap <silent> <Leader>z y:Rg <C-R>"<CR>
"plug fzf end======================================
"plug leaderf======================================
map <leader>d :LeaderfFunction<CR>
let g:Lf_ReverseOrder = 1
let g:Lf_HideHelp = 1
let g:Lf_UseCache = 0
let g:Lf_UseVersionControlTool = 0 "0:rg or ag,1:gitgrep"
let g:Lf_IgnoreCurrentBufferName = 1
let g:Lf_RootMarkers = ['.repo']
let g:Lf_WorkingDirectoryMode = 'AF'
"let g:Lf_Gtagsconf='/etc/gtags/gtags.conf'
"let g:Lf_RememberLastSearch = 1
" popup mode
let g:Lf_WindowPosition = 'popup'
let g:Lf_PreviewInPopup = 0
let g:Lf_StlSeparator = { 'left': "\ue0b0", 'right': "\ue0b2", 'font': "DejaVu Sans Mono for Powerline" }
let g:Lf_PreviewResult = {'Function': 0, 'BufTag': 0 }
let g:Lf_WindowPosition = 'bottom'
"let g:Lf_ExternalCommand = "rg --files --no-heading --glob '!.git/*' --glob '!.repo/*' --glob '!tags'"
let g:Lf_ExternalCommand = 'fd %s --exclude={.git,.gitignore,.img,.proto,.tags,.svn} --type f'
" 快捷键绑定``
let g:Lf_ShortcutF = "<leader>ff"
"let g:Lf_ShortcutF = '<C-P>'
let g:Lf_ShortcutB = "<leader>fb"
noremap <leader>fb :<C-U><C-R>=printf("Leaderf buffer %s", "")<CR><CR>
noremap <leader>fm :<C-U><C-R>=printf("Leaderf mru %s", "")<CR><CR>
noremap <leader>ft :<C-U><C-R>=printf("Leaderf bufTag %s", "")<CR><CR>
noremap <leader>fl :<C-U><C-R>=printf("Leaderf line %s", "")<CR><CR>
noremap <C-B> :<C-U><C-R>=printf("Leaderf rg --current-buffer -e %s ", expand("<cword>"))<CR>
"noremap <C-F> :<C-U><C-R>=printf("Leaderf rg -e %s ", expand("<cword>"))<CR>
noremap <C-F> :<C-U><C-R>=printf("Leaderf rg -e %s ", expand("<cword>"))<CR>
" search visually selected text literally
xnoremap gf :<C-U><C-R>=printf("Leaderf rg -F -e %s ",leaderf#Rg#visual())<CR>
noremap go :<C-U>Leaderf rg --recall<CR>
" should use `Leaderf gtags --update` first
let g:Lf_GtagsAutoGenerate = 0
let g:Lf_Gtagslabel = 'native-pygments'
noremap <leader>fr :<C-U><C-R>=printf("Leaderf gtags -r %s --auto-jump",expand("<cword>"))<CR><CR>
noremap <leader>fd :<C-U><C-R>=printf("Leaderf gtags -d %s --auto-jump",expand("<cword>"))<CR><CR>
noremap <leader>fo :<C-U><C-R>=printf("Leaderf gtags --recall %s","")<CR><CR>
noremap <leader>fn :<C-U><C-R>=printf("Leaderf gtags --next %s", "")<CR><CR>
noremap <leader>fp :<C-U><C-R>=printf("Leaderf gtags --previous %s","")<CR><CR>
let g:Lf_RgConfig = [
\"--glob=!tags",
\"--glob=!tags.*",
\"--no-messages",
\"-g '*.cpp'",
\"-g '*.c'",
\"-g '*.java'",
\"-g '*.bp'",
\"-g '*.mk'",
\"-g '*.h'",
\"-g '*.hal'",
\"--column",
\"--line-number",
\"--no-heading",
\"--color=always",
\"--smart-case",
\]
" 排除搜索的文件夹和文件
let g:Lf_WildIgnore = {
\ 'dir': ['.repo','.svn','.git','.hg','venv','.vscode','.SpaceVim.d'],
\ 'file': ['.proto','.img','.gitignore','*.sw?','~$*','*.bak','*.exe','*.o','*.so','*.py[co]']
\}
let g:Lf_CommandMap = {'<C-K>': ['<Up>'], '<C-J>': ['<Down>']}
let g:Lf_PopupPalette = {
\ 'light': {
\ 'Lf_hl_match': {
\ 'gui': 'NONE',
\ 'font': 'NONE',
\ 'guifg': 'NONE',
\ 'guibg': '#303136',
\ 'cterm': 'NONE',
\ 'ctermfg': 'NONE',
\ 'ctermbg': '236'
\ },
\ 'Lf_hl_cursorline': {
\ 'gui': 'NONE',
\ 'font': 'NONE',
\ 'guifg': 'NONE',
\ 'guibg': '#303136',
\ 'cterm': 'NONE',
\ 'ctermfg': 'NONE',
\ 'ctermbg': '236'
\ },
\ },
\ }
"plug leaderf end==================================
"
"Plug easy-git====================================
let g:easygit_enable_command = 1
"Plug easy-git end================================
"
let g:system_copy_enable_osc52 = 1
let g:system_copy#copy_command='xclip -sel clipboard'
let g:system_copy#paste_command='xclip -sel clipboard -o'
1、base vim 8.1 + Ubuntu20.04
Vim版本过低可以升级: sudo add-apt-repository ppa:jonathonf/vim sudo apt-get update sudo apt-get install vim
2、vim-plugin的安装,之后的操作都是基于~/.vimrc进行配置
1、curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim 2、.vimrc档里加入想要的插件,命令模式输入:PlugInstall 安装插件
3、fd-find的安装
代替find,速度更快 1、sudo apt install fd-find 2、ln -s /usr/bin/fdfind /usr/bin/fd
4、bat or batcat的安装
代替cat Ubuntu 22.04 sudo apt install bat Ubuntu 20.04 sudo apt install batcat
5、rg和ag的安装
1、sudo apt install ripgrep 2、sudo apt install silversearcher-ag rg目前搜索是最快的,速度远超grep,体验更好
6、xming server的安装,for vim能够正常使用系统剪切复制的功能
1、vim --version | grep clipboard,有提示+clipboard,剪切板是有的 2、vim输入:reg,却看不到 "+,意味着系统剪切板功能有异常,剪切复制不成功 3、secureCRT + 搭配x11使用有问题,需要安装xming, http://sourceforge.net/projects/xming //windows可以显示远程linux gui界面 4、安装x11 server使用正常
7、fzf的安装配置
1、sudo apt install fzf 2、.bashrc里,加入: [ -f ~/.fzf.bash ] && source ~/.fzf.bash 下面内容再加到.fzf.bash里 export FZF_COMPLETION_TRIGGER='~~' #export FZF_DEFAULT_COMMAND='fd --exclude={.git,.gitignore,.img,.proto,.tags,.svn} --type f' export FZF_DEFAULT_COMMAND="rg --files --no-heading --glob '!.git/*' --glob '!.repo/*' --glob '!tags'" export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND" export FZF_DEFAULT_OPTS="--query --preview '[[ $(file --mime {}) =~ binary ]] && echo {} is a binary file || (rougify {} || highlight -O ansi -l {} || coderay {} || cat {}) 2> /dev/null | head -500'" if [ ! -d "/tmp/fzf-history" ]; then mkdir -p /tmp/fzf-history touch /tmp/fzf-history/result fi function zz() { vim $(fzf) } function fdc() { #fd -0 -e cpp -e c -e h -e java | xargs -0 rg "$1" rg -g=*.cpp -g=*.c -g=*.java -g=*.h --no-heading "$1" } function fdm() { #fd -0 -e mk -e bp -e go -e Makefile | xargs -0 rg "$1" rg -g=*.bp -g=*.mk -g=*.xml -g=Makefile -g=*.go -g=*.sh --no-heading "$1" } _fzf_compgen_path() { fd --hidden --follow --exclude ".git|.img|.proto" . "$1" } _fzf_compgen_dir() { fd --type d --hidden --follow --exclude ".git|.img|.proto" . "$1" }
用的比较多的是fzf + rg + fd这个组合
参考: