# vim **Repository Path**: feipeng8848/vim ## Basic Information - **Project Name**: vim - **Description**: No description available - **Primary Language**: Shell - **License**: LGPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-01-18 - **Last Updated**: 2025-11-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 编译 执行`:Build`或者`:Rebuild`就可以编译当前项目 ```vim " 设置编译 " :B会调用Build函数,此函数会调用相关shell脚本执行编译操作(这部分需要自己写编译脚本) command B : call Build() command Build : call Build() command Rebuild : call Rebuild() function! Build() let current_dir = fnamemodify(getcwd(), ':t') " 项目文件夹名字是signtool " 这是一个cmake项目 if current_dir == 'signtool' " 进入vim内置的终端 execute 'terminal' " 在终端里执行cmake和make命令 call feedkeys("cmake -Bbuild" . "\r", 'n') call feedkeys("make -C build" . "\r", 'n') return endif if current_dir == 'xyz' execute 'terminal' " bxyz是一个shell函数,该函数会编译xyz项目 " 具体执行什么操作写到bxyz函数中即可 call feedkeys("./bxyz" . "\r", 'n') return endif echo "nothing to do" endfunction " Rebuild操作,同样也是依赖于shell函数 function! Rebuild() let current_dir = fnamemodify(getcwd(), ':t') if current_dir == 'signtool' execute 'terminal' call feedkeys("rm -rf build" . "\r", 'n') call feedkeys("cmake -Bbuild" . "\r", 'n') call feedkeys("make -C build" . "\r", 'n') return endif if current_dir == 'xyz' execute 'terminal' " rbxyz是一个shell函数,执行xyz项目的rebuild操作 call feedkeys("rbxyz" . "\r", 'n') return endif echo "nothing to do" endfunction ``` --- # 与tmux一起用 `:tab terminal`在新的标签中打开一个终端,然后在这里启动tmux 用tmux管理更多终端 再结合tmux自动保存回话的几个插件 在tmux中打开vim总是有高亮颜色变化问题,在vim打开tmux则避过了这个问题 --- # 插件管理 下载下面的`plug.vim`文件: https://gitee.com/blime/vim-plug/raw/master/plug.vim 这是gitee同步的仓库,原仓库是 https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim 可能无法访问 接下来把文件放到合适的位置 - 如果是Windows将文件放在 `~/vimfiles/autoload` - 如果是unix 放在 `~/.vim/autoload` 文件夹内 安装插件,只需要将插件写在 `.vimrc` 内,然后在 vim 中使用:PlugInstall 命令即可: ```bash call plug#begin('~/.vim/plugged') call plug#end() ``` 例如,安装vim-airline插件,就可以写成这样 ```bash call plug#begin('~/.vim/plugged') Plug 'vim-airline/vim-airline' call plug#end() ``` 确保插件要放在 begin 和 end 之间 重新打开 vim 使用命令 :PlugInstall ,Finishing ... Done! 表示安装完成 实际插件安装的位置在`~/.vim/plugged`,他就是给你git clone了一个项目放在这个目录里(所以说你可以 自己clone然后扔进来就可以) 删除插件 删除插件,只需要将写在 .vimrc 配置文件内的插件删除,重启 vim 并执行命令 :PlugClean 即可: ``` call plug#begin('~/.vim/plugged') call plug#end() ``` 保存在 vim 中使用 :PlugClean 这是我常用的插件: ```vim call plug#begin('~/.vim/plugged') Plug 'vim-airline/vim-airline' Plug 'vim-airline/vim-airline-themes' Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' } Plug 'gcmt/wildfire.vim' Plug 'tpope/vim-surround' "缩进线 Plug 'Yggdroot/indentLine' "括号高亮 Plug 'luochen1990/rainbow' Plug 'Yggdroot/LeaderF', { 'do': ':LeaderfInstallCExtension' } " 下划线高亮当前单词 Plug 'itchyny/vim-cursorword' Plug 'rhysd/vim-clang-format' Plug 'neoclide/coc.nvim', {'branch': 'release'} Plug 'vim-scripts/taglist.vim' " 行尾空白字符 " 默认行尾空白被标红 " 输入:FixWhitespace会删除这些空白字符 Plug 'bronson/vim-trailing-whitespace' " 切换头文件和源文件 " :A 切换 Plug 'vim-scripts/a.vim' Plug 'ludovicchabant/vim-gutentags' " Git支持 Plug 'tpope/vim-fugitive' Plug 'airblade/vim-gitgutter' Plug 'szw/vim-maximizer' Plug 'cdelledonne/vim-cmake' call plug#end() ``` --- # 模糊搜索快速打开文件 需要vim支持python,`vim --version`确认python前边带加号,然后在vim中`echo has("python3")`得到1, 或者vim中`py3 print("hello")`能准确地道输出,才能确保leaderF装完之后能正常运行。 https://github.com/Yggdroot/LeaderF 我目前主要用的三个功能: 1. 模糊查找打开文件 2. 模糊查找跳转到某个函数或者变量 3. 模糊查找跳转到某行 #### 模糊搜索打开文件 `:LeaderfFile`,然后再输入关键词模糊搜索即可 习惯vscode和sublime text的快捷键,我设置成`ctrl p`,`let g:Lf_ShortcutF = ''` #### 跳转到函数或变量 这个依赖ctags,需要先生成tags文件,更详细的内容在其他章节说明 `:LeaderfBufTag` 搜索当前buffer中所有的tag,函数、命名空间、变量等等 `:LeaderfBufTagAll` 搜索所有打开的bufer中的tag,函数、命名空间、变量等等 `:LeaderfFunction` 搜索当前buffer中的函数 `:LeaderfFunctionAll` 搜索所有打开的buffer中的函数 我定义为`F8`快捷键打开 命令后加一个叹号会进入 normal 模式,就跟tagbar一样,除了上下键选择外,Vim的各种跳转和搜索命令都 可以使用,回车就跳转过去。 对于大点的文件,用 tagbar 上下翻页找名字是件挺痛苦的事情。 而在 LeaderfFunction 的浏览模式中,按 i (Tab键也可以)进入模糊匹配模式,输入文件名搜索(按 TAB切 换回来) 作者:韦易笑 链接:https://www.zhihu.com/question/31934850/answer/379725837 来源:知乎 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 #### 跳转到行 `:LeaderfLine` 打开当前buf所有行,然后输入模糊搜索关键词就可以定位 `:LeaderfLineAll` 搜索已经打开的所有buffer中的行 #### 更详细的一些操作(从help中复制) ``` :LeaderfFile [directory] or ||f *LeaderfFile* Launch LeaderF to search files. :LeaderfBuffer or ||b *LeaderfBuffer* Launch LeaderF to search buffers. :LeaderfBufferAll *LeaderfBufferAll* Same as LeaderfBuffer, except that the unlisted buffers are shown. :LeaderfMru *LeaderfMru* Launch LeaderF to search Mru. :LeaderfMruCwd *LeaderfMruCwd* Launch LeaderF to search Mru in current working directory. :LeaderfTag *LeaderfTag* Launch LeaderF to navigate tags. 列出tag,包括函数、变量、命名空间等 :LeaderfBufTag *LeaderfBufTag* Launch LeaderF to navigate tags in current buffer. 列出当前文件的所有tag,包括函数、变量、命名空间等,类似于vscode的ctrl+shift+o :LeaderfBufTagAll *LeaderfBufTagAll* Launch LeaderF to navigate tags in all listed buffers. 列出当前已经打开的所有文件的所有tag,包括函数、变量、命名空间等 :LeaderfFunction *LeaderfFunction* Launch LeaderF to navigate functions or methods in current buffer. :LeaderfFunctionAll *LeaderfFunctionAll* Launch LeaderF to navigate functions or methods in all listed buffers. :LeaderfLine *LeaderfLine* Launch LeaderF to search a line in current buffer. :LeaderfLineAll *LeaderfLineAll* Launch LeaderF to search a line in all listed buffers. :LeaderfHistoryCmd *LeaderfHistoryCmd* Launch LeaderF to execute the command in the history. :LeaderfHistorySearch *LeaderfHistorySearch* Launch LeaderF to execute the search command in the history. :LeaderfSelf *LeaderfSelf* Launch LeaderF to execute the commands of itself. :LeaderfHelp *LeaderfHelp* Launch LeaderF to navigate the help tags. :LeaderfColorscheme *LeaderfColorscheme* Launch LeaderF to switch between colorschemes. :LeaderfFiletype *LeaderfFiletype* Launch LeaderF to navigate the filetype. :LeaderfCommand *LeaderfCommand* Launch LeaderF to execute user-defined/built-in Ex commands. :LeaderfWindow *LeaderfWindow* Launch LeaderF to search windows. :LeaderfQuickFix *LeaderfQuickFix* Launch LeaderF to navigate the quickfix. :LeaderfLocList *LeaderfLocList* Launch LeaderF to navigate the location list. :LeaderfRgInteractive *LeaderfRgInteractive* Launch LeaderF to use rg interactively. :LeaderfRgRecall *LeaderfRgRecall* Recall last search of rg. ``` 非常有用的快捷键: ![](./images/leaderf_1.png) #### 全局搜索 leaderf支持全局搜索,类似于vscode的ctrl shift f 依赖于ripgrep,需要系统中提前安装,debian系`sudo apt install ripgrep`, 红帽系`sudo dnf install ripgrep` `.vimrc`中配置成快捷键`nnoremap rg :Leaderf rg` --- # 代码补全 #### coc.nvim coc.nvim类似一个app商店,在这个商店中可以装插件(虽然coc.nvim本身也是个vim的插件) `coc-clangd`是coc.nvim的插件,用于代码补全。coc.nvim支持的插件在npm中可以搜到 https://www.npmjs.com/search?q=keywords%3Acoc.nvim 当然也可以安装其他功能的插件,比如高亮 https://github.com/neoclide/coc-highlight https://github.com/neoclide/coc.nvim coc.nvim与其他vim插件不一样,他依赖node,所以先装node ``` curl -sL install-node.vercel.app/lts | bash ``` 然后去`.vimrc`安装`coc.nvim` ``` " Use release branch (recommend) Plug 'neoclide/coc.nvim', {'branch': 'release'} ``` 执行`:PlugInstall` 在vim中输入如下命令确认安装完成 `:CocInfo` #### coc-clangd coc.nvim只能算作一个插件管理器,需要安装c语言/c++语言服务器,这里用的是`coc-clangd`,可以直接在vim里安装: ``` CocInstall coc-clangd ``` 执行之后通过`CocInfo`能看到安装细节,coc-clangd被安装到了`~/.config/coc/extensions/coc-clangd-data/` ```bash # kun @ debian in ~/.config/coc/extensions/coc-clangd-data/install/19.1.2/clangd_19.1.2 [23:40:03] $ tree -L 2 . ├── bin │ └── clangd ├── lib │ └── clang └── LICENSE.TXT ``` 在coc的配置文件中也能看到clangd路径: ```json { "clangd.path": "~/.config/coc/extensions/coc-clangd-data/install/19.1.2/clangd_19.1.2/bin/clangd" } ``` 并非所有时候都能通过`CocInstall coc-clangd`成功安装clangd,如果代码补全没法正常工作,或者vim提示 `coc.nvim the clangd language server is not installed`等,可以尝试手动安装clangd ```bash # ubuntu或者debian # 其他发行版用对应命令即可比如centos的dnf sudo apt install clangd ``` 然后在vim中手动添加clangd路径配置 ```json { "clangd.path": "/usr/bin/clangd" } ``` 在`.vimrc`中配置Tab键和Enter键作为补全: ``` inoremap coc#pum#visible() ? coc#pum#confirm() : "\" inoremap coc#pum#visible() ? coc#pum#confirm() : "\" ``` 默认通过快捷键ctrl p触发补全 #### 函数参数的嵌入提示 英文名叫:Inlay Hint 样子如下图灰色背景的`x: y: &in: `等符号: ![](./images/InlayHint.png) 这不是实际代码,看着影响观感,临时修改可以在vim里通过如下命令切换: ``` CocCommand document.toggleInlayHint ``` 所有的coc-nvim配置可以在 https://github.com/neoclide/coc.nvim/blob/7c6db7546dfa29f1167c53111a00abac4e86e383/doc/coc-config.txt 查到 临时修改仅仅针对此次使用,Inlay Hints默认是打开的,下次打开vim还会启用, 永久关闭可以在coc-nvim的配置文件里修改。 vim里执行`:CocConfig`打开coc的配置文件,添加: ```json { "inlayHint.enable":false } ``` #### 关闭clangd的自动加入头文件 自动加入的头文件有时候并不符合代码规范,反而造成困扰 ```json { "clangd.arguments": [ "--header-insertion=never" ] } ``` 经过上述操作,coc配置文件一般是这样的(该文件在 `~/.vim/coc-settings.json`): ```json { "inlayHint.enable":false, "clangd.arguments": [ "--header-insertion=never" ], "clangd.path": "/usr/bin/clangd" } ``` --- # 函数跳转(插件实现) ctags是一个用于生成程序源代码索引的工具,广泛用于程序员在文本编辑器中高效地导航和跳转代码中的符号 函数跳转以及leaderF中一些功能是依赖ctags生成的tags文件 ``` ctags -R . ``` `Ctrl+]`跳转到定义 `Ctrl+o`回到上一个位置 如果把tags文件放到工程的根目录,在根目录用vim打开代码,就可以跳转了 仅仅用ctags有两个问题,首先是需要手动生成tags文件,另外是需要放到代码根目录,脏了工程(tags文件本身也不是代码文件) 插件:vim-gutentags(推荐) 自动管理ctags的一个插件 :vim-gutentags 可以参考 https://www.cnblogs.com/pengdonglin137/articles/10202606.html 我的配置: ```vim "gutentags 自动生成tags文件 "F12跳转到函数定义 nnoremap " gutentags搜索工程目录的标志,碰到这些文件/目录名就停止向上一级目录递归 " let g:gutentags_project_root = ['.root', '.svn', '.git', '.project'] " 所生成的数据文件的名称 " let g:gutentags_ctags_tagfile = '.tags' " 将自动生成的 tags 文件全部放入 ~/.cache/tags 目录中,避免污染工程目录 " let s:vim_tags = expand('~/.cache/tags') let g:gutentags_cache_dir = s:vim_tags " 检测 ~/.cache/tags 不存在就新建 " if !isdirectory(s:vim_tags) silent! call mkdir(s:vim_tags, 'p') endif " 配置 ctags 的参数 " let g:gutentags_ctags_extra_args = ['--fields=+niazS', '--extra=+q'] let g:gutentags_ctags_extra_args += ['--c++-kinds=+pxI'] let g:gutentags_ctags_extra_args += ['--c-kinds=+px'] let g:gutentags_ctags_exclude = [ \ 'lib', \ 'build', \ 'out', \ '*.o', \ '*.a', \ '*.so*', \] ``` --- # 函数跳转(通过systemd定时任务手动实现) systemd自动执行ctags,每隔一段时间生成一次(笨办法,但也能解决问题) 在`.vimrc`中指定tags路径,让vim去`~/.tags/`路径下找tags文件,而不是去代码根目录找 这样就不用让tags文件脏了代码工程 ``` set tags=/home/kun/.tags/*/tags ``` 每一个工程生成的tags文件在`~/.tags/`中占一个文件夹,所以一般会这样: ```bash $ tree . ├── update_ctags.sh ├── project_1 │ └── tags └── project_2 └── tags ``` 在文件夹`~/.tags/`目录下写一个可以生成tags文件的脚本: update_ctags.sh内容 ```bash #!/bin/bash # 存代码的路径 prefix='****/repo' tagsPath='$HOME/.tags' # 有新增的工程直接在这加一行即可 projList=( project_1 project_2 ) for i in "${projList[@]}";do echo $i if [ ! -d $tagsPath/$i ];then echo "path .tags/$i is not exit" mkdir -p $tagsPath/$i fi cd $tagsPath/$i ctags -R --languages=c,c++ --exclude=*.o --exclude=out --exclude=*.so* --exclude=build --exclude=lib --exclude=*.a $prefix/$i chmod 666 $tagsPath/$i/tags chown kun:kun $tagsPath/$i/tags done ``` 手动执行一次该脚本就可以生成tags文件 这里我想每隔一段时间执行一次,用的是systemd的定时 创建服务单元文件 `/etc/systemd/system/update_ctags.service` ```ini [Unit] Description=Update ctags tags file [Service] ExecStart=/path/to/update_ctags.sh ``` 这里每执行一次就会执行一次update_ctags.sh,注意配置好路径,手动执行确认 创建定时器单元文件`/etc/systemd/system/update_ctags.timer` ```ini [Unit] Description=Run update_ctags.sh every 20 minutes [Timer] OnCalendar=*:0/20 Persistent=true [Install] WantedBy=timers.target ``` 这里的意思是每隔20分钟就启动一次update_ctags.service 因为update_ctags.service就能创建tags文件,所以定时执行就可以每隔一段时间重新生成tags文件 最后设置定时任务开机启动,这两条命令手动执行一次即可 ```bash sudo systemctl enable update_ctags.timer sudo systemctl start update_ctags.timer ``` --- # 代码格式化 首先借助clang-format生成格式化文件,可以参考 https://www.cnblogs.com/feipeng8848/p/18235431 然后vim中装 https://github.com/rhysd/vim-clang-format 插件 格式化代码的时候先选中然后直接`:ClangFormat`就可以了 --- # git 插件:vim-fugitive https://github.com/tpope/vim-fugitive 可以输入`:Git status`查看状态,或者直接输入`G status`效果是一样的 以下是一些简单用法 `:Git status`:查看文件的状态。 `:Git add`:将文件添加到暂存区。 `:Git commit`:提交暂存区中的文件。 `:Git push`:推送本地提交到远程仓库。 `:Git pull`:从远程仓库拉取新的提交。 `:Git branch`:查看分支列表。 `:Git checkout`:切换分支。 `:Git merge`:合并分支。 --- # cmake 插件vim-cmake --- # 外观 indentLine 缩进线 https://github.com/Yggdroot/indentLine rainbow 括号高亮 https://github.com/luochen1990/rainbow vim-airline 状态条 https://github.com/vim-airline/vim-airline 该插件和git插件等能集成到一起 ![](./images/vim-airline.png) --- # 编译vim 手动编译vim-gtk 这是chatGPT给出的答案: 要手动编译Vim(带有GTK GUI支持),您需要执行一系列步骤。以下是在Linux系统上手动编译vim-gtk的大致步骤: 1. **获取源代码**: 首先,您需要获取Vim的源代码。您可以从Vim的官方网站(https://www.vim.org/download.php)下载最新的源代码压缩包,然后将其解压缩。 2. **安装依赖**: 在编译Vim之前,您需要确保系统中安装了必要的依赖项。对于基于Debian/Ubuntu的系统,您可以运行以下命令安装一些常见的构建工具和库: ```bash sudo apt-get update sudo apt-get install build-essential libgtk-3-dev python3-dev libxt-dev ``` 请注意,可能还有其他依赖项,具体取决于您的系统配置和需求。 3. **配置编译选项**: 在终端中,切换到解压后的Vim源代码目录,并运行以下命令来配置编译选项: ```bash cd /path/to/vim/source ./configure --with-features=huge --enable-gui=gtk3 --enable-python3interp --prefix=/usr/local --with-x ``` `--with-x`可以支持粘贴板,如果是centos stream9,需要安装`libXt-devel.x86_64` 这里的`--with-features=huge`将启用所有功能,`--enable-gui=gtk3`将启用GTK GUI支持,`--prefix=/usr/local`将指定安装目录为`/usr/local`。 4. **进行编译**: 在配置选项完成后,运行以下命令开始编译: ```bash make ``` 这将编译Vim。如果您的系统拥有多个处理器核心,您可以使用`-j`选项以加快编译速度,例如`make -j4`。 5. **安装**: 编译成功后,运行以下命令进行安装: ```bash sudo make install ``` 这将把编译后的Vim安装到指定的`--prefix`目录中(在此示例中为`/usr/local`)。 6. **验证安装**: 安装完成后,您可以在终端中运行`vim`命令来验证Vim是否已经成功编译和安装。如果一切顺利,Vim的GTK GUI版本应该能够启动。 记录: 一开始忘记配置python3,使用`make distclean`后重新configure既可,注意这里不要用`make clean`,这样不彻底