更新Vim到8.2
安装依赖
sudo apt-get install libncurses5-dev libgnome2-dev libgnomeui-dev \
libgtk2.0-dev libatk1.0-dev libbonoboui2-dev \
libcairo2-dev libx11-dev libxpm-dev libxt-dev python-dev \
python3-dev git ruby-dev liblua5.1 lua5.1-dev libperl-dev
删除旧版本
dpkg -l | grep vim
sudo apt remove vim vim-addon-manager vim-common vim-doc vim-nox vim-runtime vim-scripts vim-tiny
下载源码
git clone https://gitee.com/vim/vim.git ~/Program/Github/vim/
配置vim支持python3.8,并且编译
#!/bin/bash
INSTALL_PREFIX=/usr/local/vim8
./configure --with-features=huge \
--enable-multibyte \
--enable-pythoninterp=yes \
--with-python-config-dir=/usr/lib/python2.7/config \
--enable-python3interp=yes \
--with-python3-command=/usr/bin/python3 \
--with-python3-config-dir=/usr/lib/python3.8/config-3.8-x86_64-linux-gnu \
--enable-rubyinterp=no \
--enable-perlinterp=no \
--enable-luainterp=no \
--enable-cscope \
--disable-gui \
--without-x \
--enable-fail-if-missing \
--prefix=$INSTALL_PREFIX
make -j32 VIMRUNTIMEDIR=$INSTALL_PREFIX/share/vim/vim82
sudo make install
其中考虑到远程xshell使用,没有gui,则编译时需要–disable-gui以及–without-x
–enable-python3interp=yes开启了python3支持
–with-python3-command=/usr/bin/python3指定了python3解释器路径
–with-python3-config-dir=/usr/lib/python3.7/config-3.7-x86_64-linux-gnu指定了Python3的config目录
VIMRUNTIMEDIR按照具体版本而定
安装Vim-Plug
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
详细请见https://github.com/junegunn/vim-plug(需要海外网络)
安装fugitive、nerdtree
创建~/.vim/vimrc,添加内容如下:
call plug#begin()
" The default plugin directory will be as follows:
" - Vim (Linux/macOS): '~/.vim/plugged'
" - Vim (Windows): '~/vimfiles/plugged'
" - Neovim (Linux/macOS/Windows): stdpath('data') . '/plugged'
" You can specify a custom plugin directory by passing it as the argument
" - e.g. `call plug#begin('~/.vim/plugged')`
" - Avoid using standard Vim directory names like 'plugin'
Plug 'preservim/nerdtree'
Plug 'tpope/vim-fugitive'
" Initialize plugin system
call plug#end()
保存后重新打开Vim,在命令行输入“:PlugInstall”,开始下载插件
VIM环境配置:
VIM使用技巧
将vim置于后台:
ctrl+z
查看后台vim任务
jobs
重新返回
fg xxx
vim环境安装:
sudo apt-get install vim-addon-manager
sudo apt-get install vim-scripts
sudo apt-get install vim-doc
sudo vim-addons status
sudo vim-addons install x(比如winmanager)
sudo vim-addons remove x(比如winmanager)
推荐安装:
ctags:
sudo apt-get install ctags
cscope:
sudo apt-get install cscope
taglist:
sudo vim-addons install taglist
winmanager:
sudo vim-addons install winmanager
omnicppcomplete:
sudo vim-addons install omnicppcomplete
AutoComplPop:
下载链接:http://www.vim.org/scripts/script.php?script_id=1879
解压:unzip vim-autocomplpop.zip,将解压后的文件拷贝到~/.vim/ 下的相应目录里:
autoload/* -> ~/.vim/autoload/
doc/* -> ~/.vim/doc/
plugin/* -> ~/.vim/plugin/
echofunc:
echofunc下载地址:http://www.vim.org/scripts/script.php?script_id=1735
下载完成后,把echofunc.vim文件放到 ~/.vim/plugin文件夹中
quickfix:
无须安装
vim多文件操作:
vim -o file1 file2 水平分窗打开多个文件
vim -O file1 file2 垂直分窗打开多个文件
:vsplit 垂直分窗
:split 水平分窗
Ctrl+w+方向键,选择窗口
vim常规操作技巧
一般vim自带自动缩进,如果需要粘贴文件,需要使用:
:set paste
禁止自动缩进,操作完成退出:
:set nopaste
MiniBufExplorer快捷键:
:bn 下一个文件
:bp 上一个文件
:b"num"打开指定的buffer,"num"指的是buffer开始的那个数字,比如需要打开编号为7的buffer,则输入:b7
ctags快捷键
ctags -R * 创建tag文件
ctrl+] 跟进tag
ctrl+t 返回
ctrl+W+] 新窗口显示当前光标下单词的标签,光标跳到标签处
:ts显示所有tags
vim -t tag 打开标签文件
cscope快捷键
以下命令将ctrl+\替换成ctrl+@则水平分割窗口,替换成两次ctrl+@,则垂直分割窗口
ctrl+\ s 查找symbol字符
ctrl+o 后退
ctrl+i 前进
以上两个方向导航类似于source insight里面前后箭头
:cw 打开搜索结果窗口
自定义快捷键
F5:刷新Tag
F6: make clean
F7: make
F8: 搜索(cw)或者编译窗口选择前一个
F9: 搜索(cw)或者编译窗口选择后一个
Shift + # 高亮关键词
winmanager快捷键
:VMToggle进入文件窗口编辑模式(wm)
:TlistToggle打开Taglist
:E(x)在当前路径打开文件浏览器
:cs find s xxx直接使用cscope查找关键字
:%s/From/To/gc 全局查找From替换为To,c代表逐个确认
在Taglist中,可以使用=折叠所有文件标签,使用x扩大或者收缩显示窗口
让VIM支持GB2312中文编码
编辑~/.vim/vimrc,添加:
let &termencoding=&encoding
set fileencodings=utf-8,gbk,ucs-bom,cp936
解决xterm终端无法使用鼠标复制粘贴
编辑~/.vim/vimrc,添加:
set mouse=r
快速创建工程ctags和cscope数据库:
#!/bin/sh
ctags -R --c++-kinds=+p --fields=+iaS --extra=+q
find . -name "*.h" -o -name "*.c" -o -name "*.cpp" -o -name “*.S”> cscope.files
cscope -Rbkq -i cscope.files
vim环境配置另可参考:
https://www.linuxidc.com/Linux/2017-12/149870.htm
附上~/.vim/vimrc配置:
runtime! debian.vim
" Uncomment the next line to make Vim more Vi-compatible
" NOTE: debian.vim sets 'nocompatible'. Setting 'compatible' changes numerous
" options, so any other options should be set AFTER setting 'compatible'.
set nocompatible
" Vim5 and later versions support syntax highlighting. Uncommenting the
" following enables syntax highlighting by default.
if has("syntax")
syntax on
endif
colorscheme ron
" detect file type
filetype on
filetype plugin on
" If using a dark background within the editing area and syntax highlighting
" turn on this option as well
set background=dark
" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
"have Vim load indentation rules and plugins according to the detected filetype
filetype plugin indent on
endif
" The following are commented out as they cause vim to behave a lot
" differently from regular Vi. They are highly recommended though.
"set ignorecase
"set smartcase
set autowrite
set autoindent
"set smartindent
set tabstop=4
set softtabstop=4
set shiftwidth=4
set cindent
set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s
set backspace=2
set showmatch
set linebreak
set whichwrap=b,s,<,>,[,]
"set hidden " Hide buffers when they are abandoned
set mouse=r
set number
"set previewwindow
set history=50
set laststatus=2
set ruler
set showcmd
set showmode
"--find setting--
set incsearch
set hlsearch
"--ctags setting--
map <F5> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR> :TlistUpdate<CR>
imap <F5> <ESC>:!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR><CR> :TlistUpdate<CR>
set tags=tags
set tags+=./tags "add current directory's generated tags file
"set tags+=~/test/tags
"-- omnicppcomplete setting --
imap <F3> <C-X><C-O>
imap <F2> <C-X><C-I>
set completeopt=menu,menuone
let OmniCpp_MayCompleteDot = 1 " autocomplete with .
let OmniCpp_MayCompleteArrow = 1 " autocomplete with ->
let OmniCpp_MayCompleteScope = 1 " autocomplete with ::
let OmniCpp_SelectFirstItem = 2 " select first item (but don't insert)
let OmniCpp_NamespaceSearch = 2 " search namespaces in this and included files
let OmniCpp_ShowPrototypeInAbbr = 1 " show function prototype in popup window
let OmniCpp_GlobalScopeSearch=1 " enable the global scope search
let OmniCpp_DisplayMode=1 " Class scope completion mode: always show all members
"let OmniCpp_DefaultNamespaces=["std"]
let OmniCpp_ShowScopeInAbbr=1 " show scope in abbreviation and remove the last column
let OmniCpp_ShowAccess=1
"-- Taglist setting --
let Tlist_Ctags_Cmd='ctags'
let Tlist_Use_Right_Window=1
let Tlist_Show_One_File=0
let Tlist_File_Fold_Auto_Close=1
let Tlist_Exit_OnlyWindow=1
let Tlist_Process_File_Always=1
let Tlist_Inc_Winwidth=0
"-- WinManager setting --
let g:winManagerWindowLayout='TagList'
"let g:persistentBehaviour=0
nmap wm :WMToggle<cr>
"--fold setting--
set foldmethod=syntax
set foldlevel=100
"set foldcolumn=5
"-- NERDTree --"
nmap tr :NERDTree<cr>
"-- fugitive --"
nmap gb :Git blame<cr>
"-- QuickFix setting --
map <F6> :make clean<CR><CR><CR>
map <F7> :make<CR><CR><CR> :copen<CR><CR>
map <F8> :cp<CR>
map <F9> :cn<CR>
imap <F6> <ESC>:make clean<CR><CR><CR>
imap <F7> <ESC>:make<CR><CR><CR> :copen<CR><CR>
imap <F8> <ESC>:cp<CR>
imap <F9> <ESC>:cn<CR>
附上.vim/plugin/cscope_map.vim配置:
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" CSCOPE settings for vim
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" This file contains some boilerplate settings for vim's cscope interface,
" plus some keyboard mappings that I've found useful.
"
" USAGE:
" -- vim 6: Stick this file in your ~/.vim/plugin directory (or in a
" 'plugin' directory in some other directory that is in your
" 'runtimepath'.
"
" -- vim 5: Stick this file somewhere and 'source cscope.vim' it from
" your ~/.vimrc file (or cut and paste it into your .vimrc).
"
" NOTE:
" These key maps use multiple keystrokes (2 or 3 keys). If you find that vim
" keeps timing you out before you can complete them, try changing your timeout
" settings, as explained below.
"
" Happy cscoping,
"
" Jason Duell jduell@alumni.princeton.edu 2002/3/7
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" This tests to see if vim was configured with the '--enable-cscope' option
" when it was compiled. If it wasn't, time to recompile vim...
if has("cscope")
set csprg=/usr/bin/cscope "指定用来执行cscope的命令
""""""""""""" Standard cscope/vim boilerplate
" use both cscope and ctag for 'ctrl-]', ':ta', and 'vim -t'
set cscopetag
set csto=0 "设置cstag命令查找次序:0先找cscope数据库再找标签文件;1先找标签文件再找cscope数据库"
set cst "同时搜索cscope数据库和标签文件
set cscopequickfix=s-,c-,d-,i-,t-,e- " 使用QuickFix窗口来显示cscope查找结果
set nocsverb
" add any cscope database in current directory
if filereadable("cscope.out")
cs add cscope.out
" else add the database pointed to by environment variable
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
" show msg when any other cscope db added
set csverb
" set cscopeverbose
""""""""""""" My cscope/vim key mappings
"
" The following maps all invoke one of the following cscope search types:
"
" 's' symbol: find all references to the token under cursor
" 'g' global: find global definition(s) of the token under cursor
" 'c' calls: find all calls to the function name under cursor
" 't' text: find all instances of the text under cursor
" 'e' egrep: egrep search for the word under cursor
" 'f' file: open the filename under cursor
" 'i' includes: find files that include the filename under cursor
" 'd' called: find functions that function under cursor calls
"
" Below are three sets of the maps: one set that just jumps to your
" search result, one that splits the existing vim window horizontally and
" diplays your search result in the new window, and one that does the same
" thing, but does a vertical split instead (vim 6 only).
"
" I've used CTRL-\ and CTRL-@ as the starting keys for these maps, as it's
" unlikely that you need their default mappings (CTRL-\'s default use is
" as part of CTRL-\ CTRL-N typemap, which basically just does the same
" thing as hitting 'escape': CTRL-@ doesn't seem to have any default use).
" If you don't like using 'CTRL-@' or CTRL-\, , you can change some or all
" of these maps to use other keys. One likely candidate is 'CTRL-_'
" (which also maps to CTRL-/, which is easier to type). By default it is
" used to switch between Hebrew and English keyboard mode.
"
" All of the maps involving the <cfile> macro use '^<cfile>$': this is so
" that searches over '#include <time.h>" return only references to
" 'time.h', and not 'sys/time.h', etc. (by default cscope will return all
" files that contain 'time.h' as part of their name).
" To do the first type of search, hit 'CTRL-\', followed by one of the
" cscope search types above (s,g,c,t,e,f,i,d). The result of your cscope
" search will be displayed in the current window. You can use CTRL-T to
" go back to where you were before the search.
"
nmap <C-\>s :cs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>g :cs find g <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>c :cs find c <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>t :cs find t <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>e :cs find e <C-R>=expand("<cword>")<CR><CR>
nmap <C-\>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
nmap <C-\>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
nmap <C-\>d :cs find d <C-R>=expand("<cword>")<CR><CR>
" Using 'CTRL-spacebar' (intepreted as CTRL-@ by vim) then a search type
" makes the vim window split horizontally, with search result displayed in
" the new window.
"
" (Note: earlier versions of vim may not have the :scs command, but it
" can be simulated roughly via:
" nmap <C-@>s <C-W><C-S> :cs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>s :scs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>g :scs find g <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>c :scs find c <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>t :scs find t <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>e :scs find e <C-R>=expand("<cword>")<CR><CR>
nmap <C-@>f :scs find f <C-R>=expand("<cfile>")<CR><CR>
nmap <C-@>i :scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
nmap <C-@>d :scs find d <C-R>=expand("<cword>")<CR><CR>
" Hitting CTRL-space *twice* before the search type does a vertical
" split instead of a horizontal one (vim 6 and up only)
"
" (Note: you may wish to put a 'set splitright' in your .vimrc
" if you prefer the new window on the right instead of the left
nmap <C-@><C-@>s :vert scs find s <C-R>=expand("<cword>")<CR><CR>
nmap <C-@><C-@>g :vert scs find g <C-R>=expand("<cword>")<CR><CR>
nmap <C-@><C-@>c :vert scs find c <C-R>=expand("<cword>")<CR><CR>
nmap <C-@><C-@>t :vert scs find t <C-R>=expand("<cword>")<CR><CR>
nmap <C-@><C-@>e :vert scs find e <C-R>=expand("<cword>")<CR><CR>
nmap <C-@><C-@>f :vert scs find f <C-R>=expand("<cfile>")<CR><CR>
nmap <C-@><C-@>i :vert scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>
nmap <C-@><C-@>d :vert scs find d <C-R>=expand("<cword>")<CR><CR>
""""""""""""" key map timeouts
"
" By default Vim will only wait 1 second for each keystroke in a mapping.
" You may find that too short with the above typemaps. If so, you should
" either turn off mapping timeouts via 'notimeout'.
"
"set notimeout
"
" Or, you can keep timeouts, by uncommenting the timeoutlen line below,
" with your own personal favorite value (in milliseconds):
"
"set timeoutlen=4000
"
" Either way, since mapping timeout settings by default also set the
" timeouts for multicharacter 'keys codes' (like <F1>), you should also
" set ttimeout and ttimeoutlen: otherwise, you will experience strange
" delays as vim waits for a keystroke after you hit ESC (it will be
" waiting to see if the ESC is actually part of a key code like <F1>).
"
"set ttimeout
"
" personally, I find a tenth of a second to work well for key code
" timeouts. If you experience problems and have a slow terminal or network
" connection, set it higher. If you don't set ttimeoutlen, the value for
" timeoutlent (default: 1000 = 1 second, which is sluggish) is used.
"
"set ttimeoutlen=100
endif
常用配置
对于像python这样的语言,需要临时使用tab,而不是自动将tab替换成空格,可以下面命令进行切换
set noexpandtab
还原,可以用
set expandtab