Vim keeps a stack of tags (displayable with :tags) that the user jumps to and from, similar to the jump list (displayable with :jumps). The jump list can be cleared with the :clearjumps command. This is convenient because I can jump to the “root” of where I'm working, clear the jump list, and if I ever need to return there I can simply hold CTRL-O to “unwind” all the way to where I started.

In theory, one could do the same thing with tags: jump to the initial point of interest, clear the tag stack, navigate “more deeply” into the program by following tags with CTRL-], then hold CTRL-T to pop all the way back up to the tag that was initially jumped to. The problem is that there doesn't appear to be a corresponding :cleartags command for step two, and it’s quite easy to accidentally jump past the first tag and end up somewhere I have no reason to be (usually the first line of whichever file happened to be the first argument to Vim). So my question is:

  1. Am I correct in thinking that there's nothing like a :cleartags command?
  2. If so, is there a workaround? Could it be implemented as a Vimscript function? (A quick search through the builtin functions didn’t yield anything that could be used to manipulate or even access the tag stack.)

I’m using Vim 8.0 with patches 1–427 and most optional features compiled in.

  • 2
    Why not use an upper-case mark? This will work in other navigational situations too. – Antony May 19 '17 at 23:31

You are right, no, there is no :cleartags. See :h tagstack, but you could disable it.

The excellent plugin gutentags is the best way to handle ctags, in my opinion. There are many other tag plugins.

Ctrl+T takes you back to the last tag.

Hitting it twice, will get you back to the start.

Since you are jumping, Ctrl+O will work as expected, although it is using the jumplist an not the tagstack.

There also are the ex commands tjump and stjump

  • “Hitting it twice, will get you back to the start.” One of the reasons I’m interested in a :cleartags command is to avoid this — I often accidentally pop one past where I need to be, and then have to view the tag stack, figure out where I am, then use :tag to get where I need to be. I think you’re correct that there is no real :cleartags, which essentially answers my question. The closest thing seems to be popping all the way to the bottom of the tag stack, and using :tjump, which will empty the tag stack and push the newly jumped-to tag. – Slade Apr 19 '17 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.