vim配置
一、简介
直接复制博主的vim配置后主要有以下特点:
- 按F5可以直接编译并执行C、C++、java代码以及执行shell脚本,按“F8”可进行C、C++代码的调试
- 自动插入文件头 ,新建C、C++源文件时自动插入表头:包括文件名、作者、联系方式、建立时间等,读者可根据需求自行更改
- 映射“Ctrl + A”为全选并复制快捷键,方便复制代码
- 按“F2”可以直接消除代码中的空行
- “F3”可列出当前目录文件,打开树状文件目录
- 支持鼠标选择、方向键移动
- 代码高亮,自动缩进,显示行号,显示状态行
- 按“Ctrl + P”可自动补全
- []、{}、()、“”、’ '等都自动补全
二、精简版vim
下面是精简的,没有插件的vim配置文件,保存到自己的.vimrc文件就能使用。
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 显示相关
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"set shortmess=atI " 启动的时候不显示那个援助乌干达儿童的提示
"winpos 5 5 " 设定窗口位置
"set lines=40 columns=155 " 设定窗口大小
"set nu " 显示行号
set go= " 不要图形按钮
"color asmanian2 " 设置背景主题
set guifont=Courier_New:h10:cANSI " 设置字体
"syntax on " 语法高亮
autocmd InsertLeave * se nocul " 用浅色高亮当前行
autocmd InsertEnter * se cul " 用浅色高亮当前行
"set ruler " 显示标尺
set showcmd " 输入的命令显示出来,看的清楚些
"set cmdheight=1 " 命令行(在状态行下)的高度,设置为1
"set whichwrap+=<,>,h,l " 允许backspace和光标键跨越行边界(不建议)
"set scrolloff=3 " 光标移动到buffer的顶部和底部时保持3行距离
set novisualbell " 不要闪烁(不明白)
set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")} "状态行显示的内容
set laststatus=1 " 启动显示状态行(1),总是显示状态行(2)
set foldenable " 允许折叠
set foldmethod=manual " 手动折叠
"set background=dark "背景使用黑色
set nocompatible "去掉讨厌的有关vi一致性模式,避免以前版本的一些bug和局限
" 显示中文帮助
if version >= 603
set helplang=cn
set encoding=utf-8
endif
" 设置配色方案
"colorscheme murphy
"字体
"if (has("gui_running"))
" set guifont=Bitstream\ Vera\ Sans\ Mono\ 10
"endif
set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936
set termencoding=utf-8
set encoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936
set fileencoding=utf-8
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""""新文件标题""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"新建.c,.h,.sh,.java文件,自动插入文件头
autocmd BufNewFile *.cpp,*.[ch],*.sh,*.java exec ":call SetTitle()"
""定义函数SetTitle,自动插入文件头
func SetTitle()
"如果文件类型为.sh文件
if &filetype == 'sh'
call setline(1,"\#########################################################################")
call append(line("."), "\# File Name: ".expand("%"))
call append(line(".")+1, "\# Author: ****")
call append(line(".")+2, "\# mail: ****.com")
call append(line(".")+3, "\# Created Time: ".strftime("%c"))
call append(line(".")+4, "\#########################################################################")
call append(line(".")+5, "\#!/bin/bash")
call append(line(".")+6, "")
else
call setline(1, "/*************************************************************************")
call append(line("."), " > File Name: ".expand("%"))
call append(line(".")+1, " > Author: ma6174")
call append(line(".")+2, " > Mail: ma6174@163.com ")
call append(line(".")+3, " > Created Time: ".strftime("%c"))
call append(line(".")+4, " ************************************************************************/")
call append(line(".")+5, "")
endif
if &filetype == 'cpp'
call append(line(".")+6, "#include<iostream>")
call append(line(".")+7, "using namespace std;")
call append(line(".")+8, "")
endif
if &filetype == 'c'
call append(line(".")+6, "#include<stdio.h>")
call append(line(".")+7, "")
endif
"新建文件后,自动定位到文件末尾
autocmd BufNewFile * normal G
endfunc
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"键盘命令
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
nmap <leader>w :w!<cr>
nmap <leader>f :find<cr>
" 映射全选+复制 ctrl+a
map <C-A> ggVGY
map! <C-A> <Esc>ggVGY
map <F12> gg=G
" 选中状态下 Ctrl+c 复制
vmap <C-c> "+y
"去空行
nnoremap <F2> :g/^\s*$/d<CR>
"比较文件
nnoremap <C-F2> :vert diffsplit
"新建标签
map <M-F2> :tabnew<CR>
"列出当前目录文件
map <F3> :tabnew .<CR>
"打开树状文件目录
map <C-F3> \be
"C,C++ 按F5编译运行
map <F5> :call CompileRunGcc()<CR>
func! CompileRunGcc()
exec "w"
if &filetype == 'c'
exec "!g++ % -o %<"
exec "! ./%<"
elseif &filetype == 'cpp'
exec "!g++ % -o %<"
exec "! ./%<"
elseif &filetype == 'java'
exec "!javac %"
exec "!java %<"
elseif &filetype == 'sh'
:!./%
endif
endfunc
"C,C++的调试
map <F8> :call Rungdb()<CR>
func! Rungdb()
exec "w"
exec "!g++ % -g -o %<"
exec "!gdb ./%<"
endfunc
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""实用设置
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 设置当文件被改动时自动载入
set autoread
" quickfix模式
autocmd FileType c,cpp map <buffer> <leader><space> :w<cr>:make<cr>
"代码补全
set completeopt=preview,menu
"允许插件
filetype plugin on
"共享剪贴板
set clipboard+=unnamed
"从不备份
set nobackup
"make 运行
:set makeprg=g++\ -Wall\ \ %
"自动保存
set autowrite
set ruler " 打开状态栏标尺
set cursorline " 突出显示当前行
set magic " 设置魔术
set guioptions-=T " 隐藏工具栏
set guioptions-=m " 隐藏菜单栏
"set statusline=\ %<%F[%1*%M%*%n%R%H]%=\ %y\ %0(%{&fileformat}\ %{&encoding}\ %c:%l/%L%)\
" 设置在状态行显示的信息
set foldcolumn=0
set foldmethod=indent
set foldlevel=3
set foldenable " 开始折叠
" 不要使用vi的键盘模式,而是vim自己的
set nocompatible
" 语法高亮
set syntax=on
" 去掉输入错误的提示声音
set noeb
" 在处理未保存或只读文件的时候,弹出确认
set confirm
" 自动缩进
set autoindent
set cindent
" Tab键的宽度
set tabstop=4
" 统一缩进为4
set softtabstop=4
set shiftwidth=4
" 不要用空格代替制表符
set noexpandtab
" 在行和段开始处使用制表符
set smarttab
" 显示行号
set number
" 历史记录数
set history=1000
"禁止生成临时文件
set nobackup
set noswapfile
"搜索忽略大小写
set ignorecase
"搜索逐字符高亮
set hlsearch
set incsearch
"行内替换
set gdefault
"编码设置
set enc=utf-8
set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936
"语言设置
set langmenu=zh_CN.UTF-8
set helplang=cn
" 我的状态行显示的内容(包括文件类型和解码)
"set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}
"set statusline=[%F]%y%r%m%*%=[Line:%l/%L,Column:%c][%p%%]
" 总是显示状态行
set laststatus=2
" 命令行(在状态行下)的高度,默认为1,这里是2
set cmdheight=2
" 侦测文件类型
filetype on
" 载入文件类型插件
filetype plugin on
" 为特定文件类型载入相关缩进文件
filetype indent on
" 保存全局变量
set viminfo+=!
" 带有如下符号的单词不要被换行分割
set iskeyword+=_,$,@,%,#,-
" 字符间插入的像素行数目
set linespace=0
" 增强模式中的命令行自动完成操作
set wildmenu
" 使回格键(backspace)正常处理indent, eol, start等
set backspace=2
" 允许backspace和光标键跨越行边界
set whichwrap+=<,>,h,l
" 可以在buffer的任何地方使用鼠标(类似office中在工作区双击鼠标定位)
set mouse=a
set selection=exclusive
set selectmode=mouse,key
" 通过使用: commands命令,告诉我们文件的哪一行被改变过
set report=0
" 在被分割的窗口间显示空白,便于阅读
set fillchars=vert:\ ,stl:\ ,stlnc:\
" 高亮显示匹配的括号
set showmatch
" 匹配括号高亮的时间(单位是十分之一秒)
set matchtime=1
" 光标移动到buffer的顶部和底部时保持3行距离
set scrolloff=3
" 为C程序提供自动缩进
set smartindent
" 高亮显示普通txt文件(需要txt.vim脚本)
au BufRead,BufNewFile * setfiletype txt
"自动补全
:inoremap ( ()<ESC>i
:inoremap ) <c-r>=ClosePair(')')<CR>
:inoremap { {<CR>}<ESC>O
:inoremap } <c-r>=ClosePair('}')<CR>
:inoremap [ []<ESC>i
:inoremap ] <c-r>=ClosePair(']')<CR>
:inoremap " ""<ESC>i
:inoremap ' ''<ESC>i
function! ClosePair(char)
if getline('.')[col('.') - 1] == a:char
return "\<Right>"
else
return a:char
endif
endfunction
filetype plugin indent on
"打开文件类型检测, 加了这句才可以用智能补全
set completeopt=longest,menu
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" CTags的设定
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let Tlist_Sort_Type = "name" " 按照名称排序
let Tlist_Use_Right_Window = 1 " 在右侧显示窗口
let Tlist_Compart_Format = 1 " 压缩方式
let Tlist_Exist_OnlyWindow = 1 " 如果只有一个buffer,kill窗口也kill掉buffer
let Tlist_File_Fold_Auto_Close = 0 " 不要关闭其他文件的tags
let Tlist_Enable_Fold_Column = 0 " 不要显示折叠树
autocmd FileType java set tags+=D:\tools\java\tags
"autocmd FileType h,cpp,cc,c set tags+=D:\tools\cpp\tags
"let Tlist_Show_One_File=1 "不同时显示多个文件的tag,只显示当前文件的
"设置tags
set tags=tags
"set autochdir
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"其他东东
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"默认打开Taglist
let Tlist_Auto_Open=1
""""""""""""""""""""""""""""""
" Tag list (ctags)
""""""""""""""""""""""""""""""""
let Tlist_Ctags_Cmd = '/usr/bin/ctags'
let Tlist_Show_One_File = 1 "不同时显示多个文件的tag,只显示当前文件的
let Tlist_Exit_OnlyWindow = 1 "如果taglist窗口是最后一个窗口,则退出vim
let Tlist_Use_Right_Window = 1 "在右侧窗口中显示taglist窗口
" minibufexpl插件的一般设置
let g:miniBufExplMapWindowNavVim = 1
let g:miniBufExplMapWindowNavArrows = 1
let g:miniBufExplMapCTabSwitchBufs = 1
let g:miniBufExplModSelTarget = 1
三、插件安装及配置
1. 主题插件
首先需要创建主题目录
mkdir ~/.vim/colors
主题插件可以下载http://www.vim.org/scripts/script.php?script_id=2340下载molokai主题,拷贝到~/.vim/colors目录下,也可以直接下载一个博主已经配置好的vim,可以直接下载后拷贝其中的color目录到~/.vim/
目录中。
开启主题的配置为以下配置,具体可以根据自己习惯设置主题
" 设置配色方案
"colorscheme murphy
colorscheme molokai
2. vbundle管理插件
插件如果一个一个安装的话, 比较麻烦, 可以使用vbundle这个自动安装和管理插件的插件。
可以下载到本地后进行配置。
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
不过我没有下载,直接在~/.vimrc
中插入的配置
set nocompatible " be iMproved, 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 'VundleVim/Vundle.vim'
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList - lists configured plugins
" :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
需要添加插件的话,可以在call vundle#begin()
和call vundle#end()
之间输入要安装使用的插件即可。
其中以下配置需要注意,在安装完所有插件后需要将以下配置注释掉,
set nocompatible " be iMproved, required
filetype off " required
否则后期vim打开的文件中不会显示taglist
注释掉后才会默认自动打开并显示taglist
3. nerdtree
在call vundle#begin()
和call vundle#end()
之间输入
Plugin 'scrooloose/nerdtree'
然后输入命令
:PluginInstall
就会自动安装nerdtree插件,同时还需要配置 nerdtree:
let NERDTreeQuitOnOpen=1 "打开文件时关闭树
let NERDTreeShowBookmarks=1 "显示书签
配置快捷键:
let mapleader = ","
map <leader>ne :NERDTreeToggle<CR>
map <leader>tl :TlistToggle<cr>
nnoremap <leader>ma :set mouse=a<cr>
nnoremap <leader>mu :set mouse=<cr>
输入,ne
或者
" 通过F3键来开启和关闭NERDTree
map <F3> :NERDTreeMirror<CR>
map <F3> :NERDTreeToggle<CR>
" 启动vim时自动打开NERDTree,并将光标放在Tree的Tag
autocmd VimEnter * NERDTree
" 启动vim时自动打开NERDTree,并将光标放在vim打开的文件
autocmd VimEnter * NERDTree | wincmd p
" 如果退出vim后只剩Tree的Tag的话,则自动退出Tree的Tag
autocmd BufEnter * if winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | quit | endif
nerdtree目录结构跟随vim打开的文件自动跳转
" Check if NERDTree is open or active
function! IsNERDTreeOpen()
return exists("t:NERDTreeBufName") && (bufwinnr(t:NERDTreeBufName) != -1)
endfunction
" Call NERDTreeFind iff NERDTree is active, current window contains a modifiable
" file, and we're not in vimdiff
function! SyncTree()
if &modifiable && IsNERDTreeOpen() && strlen(expand('%')) > 0 && !&diff
NERDTreeFind
wincmd p
endif
endfunction
" Highlight currently open buffer in NERDTree
autocmd BufEnter * call SyncTree()
function! ToggleNerdTree()
set eventignore=BufEnter
NERDTreeToggle
set eventignore=
endfunction
nmap <C-n> :call ToggleNerdTree()<CR>
4. tagbar插件(目前没发现具体用法)
在call vundle#begin()
和call vundle#end()
之间输入
Plugin 'majutsushi/tagbar'
然后输入命令
:PluginInstall
设置键:
nmap <leader>tb :TagbarToggle<CR>
博主的效果如下:
5. autopair插件
这个插件就是给括号自动配对的.
在call vundle#begin()
和call vundle#end()
之间输入
Plugin 'jiangmiao/auto-pairs'
然后输入命令
:PluginInstall
6. minibuffer 插件
在call vundle#begin()
和call vundle#end()
之间输入
Plugin 'minibufexpl.vim'
然后输入命令
:PluginInstall
配置插件
let g:miniBufExplMapWindowNavVim = 1
let g:miniBufExplMapWindowNavArrows = 1
let g:miniBufExplMapCTabSwitchBufs = 1
配置快捷键:
nmap <leader>mmbe :MiniBufExplorer<CR>
nmap <leader>mmbc :CMiniBufExplorer<CR>
nmap <leader>mmbu :UMiniBufExplorer<CR>
nmap <leader>mmbt :TMiniBufExplorer<CR>
7. taglist 插件
功能类似于 tagbar,我安装tagbar插件后同样安装的taglist插件,后期使用的默认也是taglist插件。安装:在call vundle#begin()
和call vundle#end()
之间输入
Plugin 'taglist.vim'
然后输入命令
:PluginInstall
配置插件
let Tlist_Use_Right_Window=1 "taglist 显示在右侧
let Tlist_Exit_OnlyWindow=1 "taglist 只剩下一个窗口时,自动关闭
let Tlist_File_Fold_Auto_Close=1
配置快捷键:
map tl :TlistToggle
nnoremap ev :vsplit $MYVIMRC
nnoremap sv :source $MYVIMRC
7.1 ubuntu中taglist不显示
ubuntu
中配置了vim
,但是taglist
无法正常显示函数及变量列表;
查看文件目录下面也生成了tags
的文件,说明ctags
也安装没问题;
- 打开文件:
vim ~/.vim/plugin/taglist.vim
- 在
taglist.vim
中查找/loaded_taglist
; - 然后在
if !exists('loaded_taglist')
前面添加下面语句:
let Tlist_Ctags_Cmd="/usr/local/bin/ctags"
(其中/usr/local/bin为ctags
的安装目录) - 然后在文件夹目录中重新执行执行
ctags -R
,taglist
就显示正常了。
原因分析:
可能是在ubuntu
中安装了eclipse
,倒是taglist
默认不再使用ctags
了,然后
let Tlist_Ctags_Cmd="/usr/local/bin/ctags"
来使能ctags
。
注:
在使用cscope
和ctags
时,对于不同的目录(如kernel
和uboot
等)需要频繁要使用cscope -Rbq
和ctags -R
生成数据文件。可以写一个脚本文件方便使用,将该脚本复制到不同目录,直接运行就ok
了。
#!/bin/sh
#-----------------------------------------------------------------------
# generate plugin' ctags,cscope and lookupfile file
# edit by haoxq
#----------------------------------------------------------------------
#generate cstags flie for cstags plugin
ctags -R
#generate cscope flie for cscope plugin
cscope -Rbq
#generate tag file for lookupfile plugin
echo -e "!_TAG_FILE_SORTED\t2\t/2=foldcase/" > filenametags
find . -not -regex '.*\.png∥gif
' -type f -printf "%f\t%p\t1\n" | \
sort -f >> filenametags
8. nerd comment 插件
这个插件是用来自动添加注释的插件。安装:
Plugin 'scrooloose/nerdcommenter'
9. markdown 插件安装
安装:
Plugin 'godlygeek/tabular'
Plugin 'plasticboy/vim-markdown'
配置:
let g:vim_markdown_math = 1
let g:vim_markdown_frontmatter = 1
let g:vim_markdown_toml_frontmatter = 1
let g:vim_markdown_json_frontmatter = 1
10. 代码折叠
配置:
"使用语法高亮定义代码折叠
set foldmethod=syntax
"打开文件是默认不折叠代码
set foldlevelstart=99
zc 折叠
zC 对所在范围内所有嵌套的折叠点进行折叠
zo 展开折叠
zO 对所在范围内所有嵌套的折叠点展开
[z 到当前打开的折叠的开始处。]z 到当前打开的折叠的末尾处。
zj 向下移动。到达下一个折叠的开始处。关闭的折叠也被计入。
zk 向上移动到前一折叠的结束处。关闭的折叠也被计入。
11. 安装ctags
sudo apt install -y ctags
有的版本上是:
sudo apt-get install universal-ctags
12. 当剩余的窗口都不是文件编辑窗口时,自动退出vim
autocmd BufEnter * if 0 == len(filter(range(1, winnr('$')), 'empty(getbufvar(winbufnr(v:val)
目的是当只有最后一个窗口时可以自动退出vim
。但是这样对于剩余多个类似NERDTree
的nofile
、nowrite
窗口时(例如,vim还有NERDTree
和Undotree
两个窗口),或者剩余quickfix
窗口时并没有作用。vimrc
增加以下一行配置
autocmd BufEnter * if 0 == len(filter(range(1, winnr('$')), 'empty(getbufvar(winbufnr(v:val), "&bt"))')) | qa! | endif
四、安装插件失败
其中有些插件PluginInstall
时候会提示安装失败,此时需要按l
查看失败的log,主要是查看下载链接,然后手动输入插件的下载链接进行下载后拷贝到~/.vim/bundle/
目录中
auto-pairs
minibufexpl.vim
nerdtree
tabular
tagbar
taglist.vim
vim-markdown
Vundle.vim
五、最终配置
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 显示相关
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"set shortmess=atI " 启动的时候不显示那个援助乌干达儿童的提示
"winpos 5 5 " 设定窗口位置
"set lines=40 columns=155 " 设定窗口大小
set nu " 显示行号
set go= " 不要图形按钮
"color asmanian2 " 设置背景主题
set guifont=Courier_New:h10:cANSI " 设置字体
syntax on " 语法高亮
autocmd InsertLeave * se nocul " 用浅色高亮当前行
autocmd InsertEnter * se cul " 用浅色高亮当前行
"set ruler " 显示标尺
set showcmd " 输入的命令显示出来,看的清楚些
"set cmdheight=1 " 命令行(在状态行下)的高度,设置为1
"set whichwrap+=<,>,h,l " 允许backspace和光标键跨越行边界(不建议)
"set scrolloff=3 " 光标移动到buffer的顶部和底部时保持3行距离
set novisualbell " 不要闪烁(不明白)
set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")} "状态行显示的内容
set laststatus=1 " 启动显示状态行(1),总是显示状态行(2)
set foldenable " 允许折叠
set foldmethod=manual " 手动折叠
"set background=dark "背景使用黑色
set nocompatible "去掉讨厌的有关vi一致性模式,避免以前版本的一些bug和局限
" 显示中文帮助
if version >= 603
set helplang=cn
set encoding=utf-8
endif
" 设置配色方案
colorscheme murphy
"colorscheme molokai
"字体
"if (has("gui_running"))
" set guifont=Bitstream\ Vera\ Sans\ Mono\ 10
"endif
set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936
set termencoding=utf-8
set encoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936
set fileencoding=utf-8
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""""新文件标题""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"新建.c,.h,.sh,.java文件,自动插入文件头
autocmd BufNewFile *.cpp,*.[ch],*.sh,*.java exec ":call SetTitle()"
""定义函数SetTitle,自动插入文件头
func SetTitle()
"如果文件类型为.sh文件
if &filetype == 'sh'
call setline(1,"\#########################################################################")
call append(line("."), "\# File Name: ".expand("%"))
call append(line(".")+1, "\# Author: ****")
call append(line(".")+2, "\# mail: ****.com")
call append(line(".")+3, "\# Created Time: ".strftime("%c"))
call append(line(".")+4, "\#########################################################################")
call append(line(".")+5, "\#!/bin/bash")
call append(line(".")+6, "")
else
call setline(1, "/*************************************************************************")
call append(line("."), " > File Name: ".expand("%"))
call append(line(".")+1, " > Author: ma6174")
call append(line(".")+2, " > Mail: ma6174@163.com ")
call append(line(".")+3, " > Created Time: ".strftime("%c"))
call append(line(".")+4, " ************************************************************************/")
call append(line(".")+5, "")
endif
if &filetype == 'cpp'
call append(line(".")+6, "#include<iostream>")
call append(line(".")+7, "using namespace std;")
call append(line(".")+8, "")
endif
if &filetype == 'c'
call append(line(".")+6, "#include<stdio.h>")
call append(line(".")+7, "")
endif
"新建文件后,自动定位到文件末尾
autocmd BufNewFile * normal G
endfunc
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"键盘命令
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
nmap <leader>w :w!<cr>
nmap <leader>f :find<cr>
" 映射全选+复制 ctrl+a
map <C-A> ggVGY
map! <C-A> <Esc>ggVGY
map <F12> gg=G
" 选中状态下 Ctrl+c 复制
vmap <C-c> "+y
"去空行
nnoremap <F2> :g/^\s*$/d<CR>
"比较文件
nnoremap <C-F2> :vert diffsplit
"新建标签
map <M-F2> :tabnew<CR>
"列出当前目录文件
map <F3> :tabnew .<CR>
"打开树状文件目录
map <C-F3> \be
"C,C++ 按F5编译运行
map <F5> :call CompileRunGcc()<CR>
func! CompileRunGcc()
exec "w"
if &filetype == 'c'
exec "!g++ % -o %<"
exec "! ./%<"
elseif &filetype == 'cpp'
exec "!g++ % -o %<"
exec "! ./%<"
elseif &filetype == 'java'
exec "!javac %"
exec "!java %<"
elseif &filetype == 'sh'
:!./%
endif
endfunc
"C,C++的调试
map <F8> :call Rungdb()<CR>
func! Rungdb()
exec "w"
exec "!g++ % -g -o %<"
exec "!gdb ./%<"
endfunc
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""实用设置
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 设置当文件被改动时自动载入
set autoread
" quickfix模式
autocmd FileType c,cpp map <buffer> <leader><space> :w<cr>:make<cr>
"代码补全
set completeopt=preview,menu
"允许插件
filetype plugin on
"共享剪贴板
set clipboard+=unnamed
"从不备份
set nobackup
"make 运行
:set makeprg=g++\ -Wall\ \ %
"自动保存
set autowrite
set ruler " 打开状态栏标尺
set cursorline " 突出显示当前行
set magic " 设置魔术
set guioptions-=T " 隐藏工具栏
set guioptions-=m " 隐藏菜单栏
"set statusline=\ %<%F[%1*%M%*%n%R%H]%=\ %y\ %0(%{&fileformat}\ %{&encoding}\ %c:%l/%L%)\
" 设置在状态行显示的信息
set foldcolumn=0
set foldmethod=indent
set foldlevel=3
set foldenable " 开始折叠
" 不要使用vi的键盘模式,而是vim自己的
set nocompatible
" 语法高亮
set syntax=on
" 去掉输入错误的提示声音
set noeb
" 在处理未保存或只读文件的时候,弹出确认
set confirm
" 自动缩进
set autoindent
set cindent
" Tab键的宽度
set tabstop=4
" 统一缩进为4
set softtabstop=4
set shiftwidth=4
" 不要用空格代替制表符
set noexpandtab
" 在行和段开始处使用制表符
set smarttab
" 显示行号
set number
" 历史记录数
set history=1000
"禁止生成临时文件
set nobackup
set noswapfile
"搜索忽略大小写
set ignorecase
"搜索逐字符高亮
set hlsearch
set incsearch
"行内替换
set gdefault
"编码设置
set enc=utf-8
set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936
"语言设置
set langmenu=zh_CN.UTF-8
set helplang=cn
" 我的状态行显示的内容(包括文件类型和解码)
"set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}
"set statusline=[%F]%y%r%m%*%=[Line:%l/%L,Column:%c][%p%%]
" 总是显示状态行
set laststatus=2
" 命令行(在状态行下)的高度,默认为1,这里是2
set cmdheight=2
" 侦测文件类型
filetype on
" 载入文件类型插件
filetype plugin on
" 为特定文件类型载入相关缩进文件
filetype indent on
" 保存全局变量
set viminfo+=!
" 带有如下符号的单词不要被换行分割
set iskeyword+=_,$,@,%,#,-
" 字符间插入的像素行数目
set linespace=0
" 增强模式中的命令行自动完成操作
set wildmenu
" 使回格键(backspace)正常处理indent, eol, start等
set backspace=2
" 允许backspace和光标键跨越行边界
set whichwrap+=<,>,h,l
" 可以在buffer的任何地方使用鼠标(类似office中在工作区双击鼠标定位)
set mouse=a
set selection=exclusive
set selectmode=mouse,key
" 通过使用: commands命令,告诉我们文件的哪一行被改变过
set report=0
" 在被分割的窗口间显示空白,便于阅读
set fillchars=vert:\ ,stl:\ ,stlnc:\
" 高亮显示匹配的括号
set showmatch
" 匹配括号高亮的时间(单位是十分之一秒)
set matchtime=1
" 光标移动到buffer的顶部和底部时保持3行距离
set scrolloff=3
" 为C程序提供自动缩进
set smartindent
" 高亮显示普通txt文件(需要txt.vim脚本)
au BufRead,BufNewFile * setfiletype txt
"自动补全
:inoremap ( ()<ESC>i
:inoremap ) <c-r>=ClosePair(')')<CR>
:inoremap { {<CR>}<ESC>O
:inoremap } <c-r>=ClosePair('}')<CR>
:inoremap [ []<ESC>i
:inoremap ] <c-r>=ClosePair(']')<CR>
:inoremap " ""<ESC>i
:inoremap ' ''<ESC>i
function! ClosePair(char)
if getline('.')[col('.') - 1] == a:char
return "\<Right>"
else
return a:char
endif
endfunction
filetype plugin indent on
"打开文件类型检测, 加了这句才可以用智能补全
set completeopt=longest,menu
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 插件安装管理
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"set nocompatible " be iMproved, 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 'VundleVim/Vundle.vim'
Plugin 'scrooloose/nerdtree'
Plugin 'majutsushi/tagbar'
Plugin 'jiangmiao/auto-pairs'
Plugin 'minibufexpl.vim'
"Plugin 'taglist.vim'
Plugin 'godlygeek/tabular'
Plugin 'plasticboy/vim-markdown'
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList - lists configured plugins
" :PluginInstall - installs plugins; append `!` to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append `!` to refresh local cache
" :PluginClean - confirms removal of unused plugins; append `!` to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" nerdtree
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let NERDTreeQuitOnOpen=1 "打开文件时关闭树
let NERDTreeShowBookmarks=1 "显示书签
let mapleader = ","
map <leader>ne :NERDTreeToggle<CR>
map <leader>tl :TlistToggle<CR>
nnoremap <leader>ma :set mouse=a<cr>
nnoremap <leader>mu :set mouse=<cr>
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" tagbar
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let mapleader = ","
nmap <leader>tb :TagbarToggle<CR>
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" CTags的设定
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let Tlist_Sort_Type = "name" " 按照名称排序
let Tlist_Use_Right_Window = 1 " 在右侧显示窗口
let Tlist_Compart_Format = 1 " 压缩方式
let Tlist_Exist_OnlyWindow = 1 " 如果只有一个buffer,kill窗口也kill掉buffer
let Tlist_File_Fold_Auto_Close = 0 " 不要关闭其他文件的tags
let Tlist_Enable_Fold_Column = 0 " 不要显示折叠树
autocmd FileType java set tags+=D:\tools\java\tags
"autocmd FileType h,cpp,cc,c set tags+=D:\tools\cpp\tags
"let Tlist_Show_One_File=1 "不同时显示多个文件的tag,只显示当前文件的
"设置tags
set tags=tags
"set autochdir
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"其他东东
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"默认打开Taglist
let Tlist_Auto_Open=1
""""""""""""""""""""""""""""""
" Tag list (ctags)
""""""""""""""""""""""""""""""""
let Tlist_Ctags_Cmd = '/usr/bin/ctags'
let Tlist_Show_One_File = 1 "不同时显示多个文件的tag,只显示当前文件的
let Tlist_Exit_OnlyWindow = 1 "如果taglist窗口是最后一个窗口,则退出vim
let Tlist_Use_Right_Window = 1 "在右侧窗口中显示taglist窗口
" minibufexpl插件的一般设置
let g:miniBufExplMapWindowNavVim = 1
let g:miniBufExplMapWindowNavArrows = 1
let g:miniBufExplMapCTabSwitchBufs = 1
let g:miniBufExplModSelTarget = 1
六、快捷键
- 搜索单个函数:当光标在某个单词上面的时候 按 shift + #键(或 shift + * )即可
七、安装使用cscope
1. 下载
在cscop官网http://ctags.sourceforge.net中下载cscope源码包
2. 编译安装
tar -zxvf *****
cd catgs-*
./configure
make -j$(nproc --all)
sudo make install
3. vim配置
-
参考文章http://cscope.sourceforge.net/cscope_maps.vim,介绍了cscope快捷键的设置,我使用的是其中一种
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " 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") """"""""""""" Standard cscope/vim boilerplate " use both cscope and ctag for 'ctrl-]', ':ta', and 'vim -t' set cscopetag " check cscope for definition of a symbol before checking ctags: set to 1 " if you want the reverse search order. set csto=0 " 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 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
-
在 ~/.vimrc中添加以下内容,方便后期调试(.vimrc文件中“ 表示注释)
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " cscope的设定 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" if has("cscope") set csprg=/usr/bin/cscope "指定:cstag的搜索顺序。0表示先搜索cscope数据库,若不匹配,再搜索tag文件,1 "则相反 set csto=0 ":tag/Ctrl-]/vim -t将使用:cstag,而不是默认的:tag set cst " +(将结果追加到quickfix窗口)、-(清空上一次的结果)、0(不使用quickfix。没有指定也相当于标志为0))) set cscopequickfix=s-,c-,d-,i-,t-,e- " 使用QuickFix窗口来显示cscope查找结果 set nocsverb "增加cscope数据库时,将不会打印成功或失败信息 set cspc=3 "指定在查找结果中显示多少级文件路径,默认值0表示显示全路径,1表示只显示文件名" if filereadable("cscope.out") cs add $PWD/cscope.out $PWD "cs add cscope.out else " 子目录打开,向上查找 let cscope_file=findfile("cscope.out", ".;") let cscope_pre=matchstr(cscope_file, ".*/") if !empty(cscope_file) && filereadable(cscope_file) exe "cs add" cscope_file cscope_pre endif endif set nocsverb endif set cscopequickfix=s-,c-,d-,i-,t-,e- 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> " 使用时,将光标停留在要查找的对象上,按下<C->g,即先按“Ctrl+\”,然后很快再按“g”,将会查找该对象的定义。
- 主要是
cs add $PWD/cscope.out $PWD
这句,不然会在以后每次调试时候需要手动添加cscope生成的文件 - Ctrl+]将跳到光标所在变量或函数的定义处 Ctrl+T返回
- :cs find s ---- 查找C语言符号,即查找函数名、宏、枚举值等出现的地方
- :cs find g ---- 查找函数、宏、枚举等定义的位置,类似ctags所提供的功能
- :cs find d ---- 查找本函数调用的函数
- :cs find c ---- 查找调用本函数的函数
- :cs find t: ---- 查找指定的字符串
- :cs find e ---- 查找egrep模式,相当于egrep功能,但查找速度快多了
- :cs find f ---- 查找并打开文件,类似vim的find功能
- :cs find i ---- 查找包含本文件的文
- 主要是
4. 使用
- Cscope缺省只解析C文件(.c和.h)、lex文件(.l)和yacc文件(.y)。如果你想使用cscope解析C++或Java后缀的文件,那么你需要把这些文件的名字和路径保存在一个名为cscope.files的文件。当cscope发现在当前目录中存在cscope.files时,就会为cscope.files中列出的所有文件生成索引数据库。首先进入到你程序所在目录,执行下面命令:
find ./ -type f > cscope.files
- 生成.out文件
cscope -Rbkq
- 执行完毕之后,当前目录会多出三个文件
- 在缺省情况下,cscope在生成数据库后就会进入它自己的查询界面,我们一般不用这个界面,所以使用了”-b”选项。如果你已经进入了这个界面,按CTRL-D退出。
-R: 在生成索引文件时,搜索子目录树中的代码
-b: 只生成索引文件,不进入cscope的界面
-q: 生成cscope.in.out和cscope.po.out文件,加快cscope的索引速度
-k: 在生成索引文件时,不搜索/usr/include目录
-i: 如果保存文件列表的文件名不是cscope.files时,需要加此选项告诉cscope到哪儿去找源文件列表。可以使用”-“,表示由标准输入获得文件列表。
-Idir: 在-I选项指出的目录中查找头文件
-u: 扫描所有文件,重新生成交叉索引文件
-C: 在搜索时忽略大小写
-Ppath: 在以相对路径表示的文件前加上的path,这样,你不用切换到你数据库文件所在的目录也可以使用它了。
要在vim中使用cscope的功能,需要在编译vim时选择”+cscope”。vim的cscope接口先会调用cscope的命令行接口,然后分析其输出结果找到匹配处显示给用户。 - 打开任意一个文件后在末行模式下输入一下内容:
: cs show
- QuickFix 窗口
"QuickFix"窗口,以前也是一个vim的插件来的, 只不过现在成了vim的标准插件, 不用你在去安装了,QuickFix窗口的主要作用就是上面看到的那个功能: 输出一些供选择的结果, 可以被很多命令调用,更详细的介绍和使用方法请用下面的命令打开QuickFix的手册来学习吧::helpquickfix
:copen
#打开quickfix窗口:ccolse
#关闭quickfix窗口:cw
#打开quickfix窗口:botright copen
#在底部打开quickfix窗口- 目前默认的QuickFix窗口打开后是在右下角,不利于查看,因此通过快捷键映射方法打开QuickFix窗口
nmap <F4> :botright copen<cr> nmap <F5> :cclose<cr>
- 返回
- ctrl+]跳转到函数、变量等的定义处
- ctrl+o往回跳转(一步一步)
- ctrl+t跳回最开始处
- tags可以查看跳转记录
八、vim快捷键
-
搜索单个函数:当光标在某个单词上面的时候 按 shift + #键(或 shift + * )即可
-
搜索及替换命令
/pattern: 从光标开始处向文件尾搜索pattern ?pattern: 从光标开始处向文件首搜索pattern n: 在同一方向重复上一次搜索命令 N: 在反方向上重复上一次搜索命令 :s/p1/p2/g: 将当前行中所有p1均用p2替代 :n1,n2s/p1/p2/g: 将第n1至n2行中所有p1均用p2替代 :%s/p1/p2/g: 将文件中所有p1均用p2替换 :%s/p1/p2/gc: 将文件中所有p1均用p2一个一个选择替换
-
屏幕翻滚类命令
Ctrl+u: 向文件首翻半屏 Ctrl+d: 向文件尾翻半屏 Ctrl+f: 向文件尾翻一屏 Ctrl+b: 向文件首翻一屏 nz: 将第n行滚至屏幕顶部,不指定n时将当前行滚至屏幕顶部。
-
插入文本类命令
i: 在光标前 I: 在当前行首 a: 光标后 A: 在当前行尾 o: 在当前行之下新开一行 O: 在当前行之上新开一行 r: 替换当前字符 R: 替换当前字符及其后的字符,直至按ESC键 s: 从当前光标位置处开始,以输入的文本替代指定数目的字符 S: 删除指定数目的行,并以所输入文本代替之 ncw或nCW: 修改指定数目的字 nCC: 修改指定数目的行
-
删除命令
ndw或ndW: 删除光标处开始及其后的n-1个字 do: 删至行首 d$: 删至行尾 ndd: 删除当前行及其后n-1行 x或X: 删除一个字符,x删除光标后的,而X删除光标前的 Ctrl+u: 删除输入方式下所输入的文本
-
寄存器操作
"?nyy: 将当前行及其下n行的内容保存到寄存器?中,其中?为一个字母,n为一个数字 "?nyw: 将当前行及其下n个字保存到寄存器?中,其中?为一个字母,n为一个数字 "?nyl: 将当前行及其下n个字符保存到寄存器?中,其中?为一个字母,n为一个数字 "?p: 取出寄存器?中的内容并将其放到光标位置处。这里?可以是一个字母,也可以是一个数字 ndd: 将当前行及其下共n行文本删除,并将所删内容放到1号删除寄存器中
-
剪切和复制、粘贴
[n]x: 剪切光标右边n个字符,相当于d[n]l。 [n]X: 剪切光标左边n个字符,相当于d[n]h。 y: 复制在可视模式下选中的文本。 yy or Y: 复制整行文本。 y[n]w: 复制一(n)个词。 y[n]l: 复制光标右边1(n)个字符。 y[n]h: 复制光标左边1(n)个字符。 y$: 从光标当前位置复制到行尾。 y0: 从光标当前位置复制到行首。 :m,ny<cr> 复制m行到n行的内容。 y1G或ygg: 复制光标以上的所有行。 yG: 复制光标以下的所有行。 yaw和yas:复制一个词和复制一个句子,即使光标不在词首和句首也没关系。 d: 删除(剪切)在可视模式下选中的文本。 d$ or D: 删除(剪切)当前位置到行尾的内容。 d[n]w: 删除(剪切)1(n)个单词 d[n]l: 删除(剪切)光标右边1(n)个字符。 d[n]h: 删除(剪切)光标左边1(n)个字符。 d0: 删除(剪切)当前位置到行首的内容 [n] dd: 删除(剪切)1(n)行。 :m,nd<cr> 剪切m行到n行的内容。 d1G或dgg: 剪切光标以上的所有行。 dG: 剪切光标以下的所有行。 daw和das:剪切一个词和剪切一个句子,即使光标不在词首和句首也没关系。 d/f<cr>:这是一个比较高级的组合命令,它将删除当前位置 到下一个f之间的内容。 p: 在光标之后粘贴。 P: 在光标之前粘贴。
-
怎样把vim中的内容选择复制到其他程序中
在使用VIM时,有时需要把VIM中的内容复制到其他程序中,例如复制到firefox浏览器中查询。
方法如下:
在normal mode下,按住shift键,使用鼠标选择,右击鼠标键,菜单中就出现了copy选择。 -
vim回退、前进操作,列操作(插入、删除、替换)、复制粘贴
- 撤销上一步操作: u
- 前进操作: crtl + r
vim中列(块)删除:
- 第一步:按下组合键“CTRL+v” 进入“可视 块”模式,选取这一列操作多少行
- 第二步:按下d 即可删除被选中的整块
vim中列(块)插入:
- 第一步:按下组合键“CTRL+v” 进入“可视 块”模式,选取这一列操作多少行
- 第二步:按下shift+i(或者大写的字母"i")
- 第三步:输入要插入的内容
- 第四步:按ESC,之后就会看到插入的效果。
vim中列(块)替换:
- 第一步:按下组合键“CTRL+v” 进入“可视 块”模式,选取这一列操作多少行
- 第二步:按下r 即可进入修改模式,然后输入待修改的值,如“string1”
九、cscope&catgs 使用
- 生成
.out
文件cscope -Rbkq
- 查找
Ctrl+]将跳到光标所在变量或函数的定义处 Ctrl+T返回 Ctrl+\+s ---- 查找C语言符号,即查找函数名、宏、枚举值等出现的地方 Ctrl+\+g ---- 查找函数、宏、枚举等定义的位置,类似ctags所提供的功能 Ctrl+\+d ---- 查找本函数调用的函数 Ctrl+\+c ---- 查找调用本函数的函数 Ctrl+\+t: ---- 查找指定的字符串 Ctrl+\+e ---- 查找egrep模式,相当于egrep功能,但查找速度快多了 Ctrl+\+f ---- 查找并打开文件,类似vim的find功能 Ctrl+\+i ---- 查找包含本文件的文
- 返回
ctrl+]跳转到函数、变量等的定义处 ctrl+o往回跳转(一步一步) ctrl+t跳回最开始处 tags可以查看跳转记录
- 打开nerdtree
,ne
10. vim复制粘贴到系统剪贴板
10.1 检查是否支持此功能
要完成vim
中的内容复制到系统剪切板,需要vim
支持 +clipboard
,检查的方法:
uos@uos-PC:~/Desktop$ vim --version | grep clipboard
-clipboard +jumplist +persistent_undo +vartabs
+eval +mouse_gpm +syntax +xterm_clipboard
查看clipboard
和xterm_clipboard
前面的符号
- 加号(+),表示支持
- 减号(-),表示不支持
可以看到现在的vim
是不支持的,意思是不支持从vim中复制到系统剪切板中。
10.2 安装插件vim-gtk
此时可以选择安装插件vim-gtk
sudo apt install -y vim-gtk
安装完成以后,同样的在终端中输入 vim --version | grep clipboard
uos@uos-PC:~/Desktop$ vim --version | grep clipboard
+clipboard +jumplist +persistent_undo +vartabs
+eval +mouse_gpm +syntax +xterm_clipboard
可以看到clipboard
和xterm_clipboard
前面减号变成了加号。现在就可以把vim中的内容复制到系统剪切板中了,具体怎么操作,请您继续向下看。
10.3 vim复制到系统剪切板
在vim同一个文件下操作,复制使用的是 nyy,粘贴使用的是 p(在vim中有很多的寄存器,这样操作是把内容复制到无名寄存器(unnamed register): “”,其他的寄存在vim命令行模式下输入 :help registers 命令可以查看)
vim系统剪切板:
"+y
复制到系统剪切板中(解释一下:这里的+
号不是表示"
和y
同时按下,按键的顺序应该是shift '
、shift =
、y
)"+p
把系统粘贴板里的内容粘贴到vim(解释一下:这里的+
号不是表示"
和p
同时按下,按键的顺序应该是shift '
、shift =
、p
)
10.4 快捷键设置
如果每次从vim中复制到系统剪切板都这么麻烦的话太影响工作效率了,此时可以选择配置快捷键,但是上面我的vim配置文件中已经有了此快捷键的设置,所以后期使用的时候直接Ctrl c
即可复制vim中的内容到系统剪切板中。
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"键盘命令
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
nmap <leader>w :w!<cr>
nmap <leader>f :find<cr>
" 映射全选+复制 ctrl+a
map <C-A> ggVGY
map! <C-A> <Esc>ggVGY
map <F12> gg=G
" 选中状态下 Ctrl+c 复制
vmap <C-c> "+y
📚 设置高亮显示当前行
在vimrc
里加入:
"开启光亮光标行
set cursorline
hi CursorLine cterm=NONE ctermbg=darkred ctermfg=white guibg=darkred guifg=white
"开启高亮光标列
set cursorcolumn
hi CursorColumn cterm=NONE ctermbg=darkred ctermfg=white guibg=darkred guifg=white`
其中Cursorline
和CursorColumn
分别表示光标所在的行和列,根据一般用户的习惯,高亮行就可以了
📕 Vim识别三种不同的终端
term
,黑白终端;cterm
,彩色终端;gui
,Gvim
窗口。
📖 term
term
,可以定义其字体显示为:bold
、underline
、reverse
、italic
或standout
。用逗号来组合使用这些属性:
highlight Keyword term=reverse,bold
📖 cterm
cterm
,可以用ctermfg
设置前景色;用ctermbg
设置背景色。
定义蓝底红字并使用下划线来显示注释:
highlight Comment cterm=underline ctermfg=red ctermbg=blue
📖gui
gui
,可以使用选项gui=attribute
,来定义图形窗口下语法元素的显示属性。
推荐使 用的颜色包括:black
, brown
, grey
, blue
, green
, cyan
, magenta
, yellow
, white
。
📚 自动补全功能
1. vim自带补全功能
使用 Ctrl + N
或 Ctrl + P
进行补全:在命令模式下输入部分命令,然后按下 Ctrl + N
或 Ctrl + P
可以分别进行向下或向上的补全操作。这种方式可以用于命令、文件名、标签名等的补全。
📚 vim打开多个文件、同时显示多个文件、在文件之间切换 打开多个文件
1.使用分屏功能
在不关闭当前正在编辑的文件的情况下,在Vim中创建一个新的文件,可以使用Vim的分屏功能。以下是一种方法:
- 在Vim打开的文件中,按下
Ctrl + W
,然后松开。 - 接着按下
v
或sp
,将当前窗口垂直分割成两个窗口。或者按下s
或sp
,将当前窗口水平分割成两个窗口。 - 新分割出的窗口中会显示一个空白文件。可以使用
:e <文件名>
命令来打开一个新文件,例如:e newfile.cpp
。 - 在新的窗口中编辑新文件。
2. 同时显示多个文件
:split
简写 :sp
:vsplit
简写 :vsp
显示缓存 :ls
3. 多文档编辑
:n
编辑下一个文档。
:2n
编辑下两个文档。
:N
编辑上一个文档。注意,该方法只能用于同时打开多个文档。
:e
文档名 这是在进入vim后,不离开 vim 的情形下打开其他文档。
:e#
或 Ctrl+ˆ
编辑上一个文档,用于两个文档相互交换编辑时使用。?# 代表的是编辑前一次编辑的文档
:files
或 :buffers
或:ls
可以列出目前 缓冲区 中的所有文档。加号+
表示 缓冲区已经被修改过了。#
代表上一次编辑的文档,%
是目前正在编辑中的文档
:b
文档名或编号 移至该文档。
:f
或 Ctrl+g
显示当前正在编辑的文档名称。
:f 檔名
改变编辑中的文档名。(file)
4. 更新NERDTree
点击 NERDTree
中的 r
映射来刷新 nerdtree
,它会显示你创建的文件/目录正确。
5. 同时创建or打开多个文件
vim file1 file2 file3 …
在vim窗口中打开一个新文件
:open file
在新窗口中打开文件
:split file
切换到下一个文件
:bn
切换到上一个文件
:bp
6. vim 文件刷新
在 vim 打开一个文件,在另一个地方修改了文件
:e
重新加载文件:e!
强制丢掉本地修改,从磁盘加载文件
问题
$ source ~/.vimrc
Command '' not found, but can be installed with:
sudo apt install mailutils-mh # version 1:3.7-2.1, or
sudo apt install meshio-tools # version 4.0.4-1
sudo apt install mmh # version 0.4-2
sudo apt install nmh # version 1.7.1-6
sudo apt install termtris # version 1.3-1
显示相关
set: command not found
autocmd: command not found
set ruler : command not found
bash: /home/*/.vimrc: line 19: syntax error near unexpected token `('
bash: /home/*/.vimrc: line 19: `set novisualbell " 不要闪烁(不明白)'
sudo apt install -y mailutils-mh meshio-tools nmh