使用:tag打开文件并跳至上次编辑的位置

问题描述 投票:0回答:2

当在Vim中使用ctags时,可以通过以下方式打开文件:

:tag <filename>

如果此tags文件是使用--extras=+qf标志生成的,则有可能,如下面的代码片段所示:

$ find . -name "*.c" | xargs ctags-universal --extras=+qf -L -

这将在标签文件中产生如下一行:

JPEGImageDecoder.cpp Source/WebCore/platform/image-decoders/jpeg/JPEGImageDecoder.cpp  1;"   F

此条目包含4个元素:{标签名称,文件路径,行号,标签类型}。每当Vim打开标签时,尽管我已将Vim配置为记住文件的最后编辑位置,并在读取缓冲区时返回该位置,但它会转到行号1。

if has("autocmd")
  au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal! g`\"" | endif
endif

是否可以用:tag <filename>打开文件并将光标设置到最后编辑的位置?

vim ctags exuberant-ctags
2个回答
0
投票

不是精确的重复项,但请参见this thread

我不确定您是否可以使用:tag命令来执行此操作。考虑制作自己的:Tag命令来完成此操作,可能类似于:

autocmd BufWinLeave * mkview
command -nargs=? Tag :tag <args> | loadview

这本质上是来自前述线程的copypasta。确保您还看到了:mksession;它甚至更强大。


0
投票

我知道了。

在Vim中,:help tags-file-format显示有关ctags输入格式的信息:

The lines in the tags file must have one of these three formats:

1.  {tagname}           {TAB} {tagfile} {TAB} {tagaddress}
2.  {tagfile}:{tagname} {TAB} {tagfile} {TAB} {tagaddress}
3.  {tagname}           {TAB} {tagfile} {TAB} {tagaddress} {term} {field} ..

在下面的某处,它陈述了有关{tagaddress}的以下内容:

{tagaddress}    The Ex command that positions the cursor on the tag.  It can
                be any Ex command, although restrictions apply (see
                tag-security).  Posix only allows line numbers and search
                commands, which are mostly used.

[在大多数情况下,此{tagaddress}是行号或正则表达式,但是应该可以使用其他Vim机制来定位光标。如果我将1替换为'"(上次编辑的位置),它将起作用。

因此,基本上,我需要生成一个标签文件,对于已索引的文件名,该文件将替换1的默认'"。理想情况下,它应该是exuberant-ctagsuniversal-ctags中的参数,但基本上我是通过使用sed对标签文件进行后处理来做到这一点的:

# Replace 1 for "' (first line for last edited line).
sed -ri "s/1;\"\s+F$/'\";\"\tF/" .tags
© www.soinside.com 2019 - 2024. All rights reserved.