0

When using ctags with Vim, it's possible to open a file as:

:tag <filename>

This is possible if the tags file was generated using the --extras=+qf flag, like in this code snippet:

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

This produces a line in the tags file such as this:

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

This entry contains 4 elements: {tag name, path to file, line number, tag type}. Whenever Vim opens the tag, it goes to line number 1, despite I have configured Vim to remember the last edited position of a file and go back to it when the buffer is read.

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

Is there any way of opening a file with :tag <filename> and set the cursor to the last edited position?

Diego Pino
  • 11,278
  • 1
  • 55
  • 57

2 Answers2

0

Not an exact duplicate, but see this thread.

I'm not sure if you could do it with the :tag command. Consider making your own :Tag command that accomplishes this, maybe something like:

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

This is essentially copypasta from the aforementioned thread. Make sure you also see :mksession; it's even more powerful.

Hari Amoor
  • 442
  • 2
  • 7
0

I figured this out.

In Vim, :help tags-file-format shows information about a ctags entry format:

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} ..

Somewhere below, it states the following about {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.

In most cases this {tagaddress} is a line number or a regular expression, but it should be possible to use other Vim mechanisms for positioning the cursor. If I replace 1 for '" (last edited position), it works.

So basically I need to produce a tags file that, for the indexed filenames, replaces the default 1 for '". Ideally that should be an argument in exuberant-ctags or universal-ctags, but basically I did that by postprocessing the tags file with sed:

# Replace 1 for "' (first line for last edited line).
sed -ri "s/1;\"\s+F$/'\";\"\tF/" .tags
Diego Pino
  • 11,278
  • 1
  • 55
  • 57