实现Vim tags自动更新

这里要用到两个插件:
projtags.vim : Set tags file for per project
AutoTag : Updates entries in a tags file automatically when saving

这两个插件单独工作都不能满足我的要求,但是他们合起来使用却是非常方便。
projtags.vim用于给项目配置tags文件,如配置项目路径为 /path-to-proj ,那么,当文件在 /path-to-proj及其子目录时,tags选项此时被局部地设置为了/path-to-proj/tags ,这样,即使是在子目录,tags选项的配置也能工作。
现在还有一个问题是tags自动更新的问题了。总不能每添加一个新文件就手动运行一下命令吧?而且,ctags的-a选项(append)并不会自动删除已经不存在的tag,此时,autotag.vim华丽登场~~,它能搞定这一切。

不过直接把这两个插件扔plugin目录是不会达到上述效果的,得稍做一些修改。
AutoTag.vim 作者的安装指导是要你直接在.vimrc里面source它。
这里我把它修改一下,然后放到~/.vim/plugin目录,给出补丁吧:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[admin@huangye plugin ]$ diff -urNB ~/document/vim/plugins/autotag.vim autotag.vim
--- /home/admin/document/vim/plugins/autotag.vim    2012-01-06 16:50:37.912729822 +0800
+++ autotag.vim 2012-01-08 10:43:21.423301599 +0800
@@ -87,7 +87,7 @@
       while file:
          file = os.path.dirname (file )
          #self.__diag ('drive = "%s", file = "%s"' % (drive, file ) )
-         tagsFile = os.path.join(drive, file, self.tags_file)
+         tagsFile = self.tags_file
          #self.__diag ('tagsFile "%s"' % tagsFile )
          if os.path.isfile (tagsFile ):
             st = os.stat (tagsFile )
@@ -184,7 +184,7 @@
    let g:autotagDisabled= 0
 endif
 if !exists ("g:autotagVerbosityLevel" )
-   let g:autotagVerbosityLevel=0
+   let g:autotagVerbosityLevel=2
 endif
 if !exists ("g:autotagExcludeSuffixes" )
    let g:autotagExcludeSuffixes="tml.xml.text.txt"

projtags.vim的话,在

1
execute 'autocmd BufEnter ' . filepattern . ' :setlocal tags+=' . tagfile

前面加上一句:

1
let g :autotagTagsFile=tagfile

,然后把其内容直接加到.vimrc里面即可。因为这里要让projtags.vim优先运行以告知autotag.vim tags文件的位置。
还有要注意的一点是,在给projtags.vim配置项目路径时,要写绝对路径,不能写 $HOME或 ~ ,不然autotag.vim不能更新tags.

1
:scriptnames

命令会列出你加载的插件列表,按加载顺序排列。不用总结哪个目录的插件先加载了,直接运行这条命令就好,一目了然。


====

file:///home/alan/Desktop/vim-tags-auto-update.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Ubuntu 系统中使用 vim 编辑器时,可以通过设置自动补齐来提高编辑效率。以下是实现自动补齐的步骤: 1. 安装 vim 插件管理器 Vundle: ``` git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim ``` 2. 在 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() " let Vundle manage Vundle, required Plugin 'VundleVim/Vundle.vim' " Add all your plugins here (note older versions of Vundle used Bundle instead of Plugin) Plugin 'Valloric/YouCompleteMe' Plugin 'SirVer/ultisnips' Plugin 'honza/vim-snippets' " All of your Plugins must be added before the following line call vundle#end() " required filetype plugin indent on " required " YouCompleteMe settings let g:ycm_autoclose_preview_window_after_completion = 1 let g:ycm_key_invoke_completion = '<c-space>' let g:ycm_always_populate_location_list = 1 let g:ycm_collect_identifiers_from_tags_files = 1 let g:ycm_complete_in_comments = 1 let g:ycm_complete_in_strings = 1 let g:ycm_seed_identifiers_with_syntax = 1 " UltiSnips settings let g:UltiSnipsExpandTrigger="<tab>" let g:UltiSnipsJumpForwardTrigger="<tab>" let g:UltiSnipsJumpBackwardTrigger="<s-tab>" ``` 以上配置文件中包含了三个插件,其中 YouCompleteMe 提供了自动补齐功能,ultisnips 和 vim-snippets 则提供了代码片段和模板的支持。 3. 安装 YouCompleteMe 插件: ``` cd ~/.vim/bundle/YouCompleteMe python3 install.py --clangd-completer ``` 其中,--clangd-completer 参数表示使用 clangd 作为代码补全引擎。如果你使用的是其他编程语言,可以参考官方文档选择合适的补全引擎。 4. 重新打开 vim,输入代码时即可使用自动补齐功能。可以通过 <Tab> 键来触发补全提示,通过 <Enter> 或 <Tab> 键来选择补全项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值