vim b or tag or e

Here is a vim snippet that defines an ':F filename' command that goes through all of current open files, a ctags file and a local path, in one invocation:

function BufferOrTagOrPath(fname)
                " try to open an existing buffer
                execute 'buffer ' . a:fname
                        " try to open a tag
                        execute 'tag ' . a:fname
                        " go to last changelist position
                        execute 'normal! g;'
                        if filereadable(a:fname)
                                execute 'e ' . a:fname
                                echoerr "can't find " . a:fname

command -nargs=1 -complete=tag F :call BufferOrTagOrPath()


I use ctags in vim, so that ':tag file.c' immediately opens such a file found anywhere in a source tree.

However, :tag also jumps to the start of the file. So in my workflow I often prefer :b to just jump back to that buffer. That is causing the cumbersome situation that I type ':b file.c' but the buffer isn't loaded -- or maybe more than one of them is loaded and :b fails. Then I try again with ':tag file.c'. Stupid.

Also, when first opening, I use ':tag file.c', but the second time around I want to use ':b file.c', so I go back in the history and edit 'tag' to 'b'. Meh. Also, when typing ':b fi' and hit arrow-up to autocomplete from history, that doesn't work if I previously called ':tag file.c'.

All of this is super annoying, so I want one single command that does both. Then I don't need to edit history or try two commands all the time. The above is my "do what I mean" script for this task.

Now I can simply do ':F file.c' all the time.

If I have already opened a buffer with a matching name, I will go there.

If not, try a tags file.

If no tags file exists, try to open a file with that path.

login 2019-02-20 23:57