I'm a longtime happy user of vim-go, but one thing bugs me: When jumping to a definition with :GoDef, I often want to look around and then jump back to the original location. While it's possible to use CTRL-O for that, this often requires multiple jumps.
When programming in C, I use ctags and Vim allows going to a tag definition with the CTRL-] mapping, and then jump back with CTRL-T. This keeps a tag stack that can be viewed with :tags, so it is easy to see the flow of the code.
It would be awesome if this were possible in vim-go: Allow going to a definition with CTRL-] and adding the location to the tag stack so that all the rest just works.
Hi @gavrie This looks like a nice improvement we could do. I'm looking into it. Thanks for the feedback :)
From my initial research, modifying the tag stack is a tricky business. You might be better off making your own jump stack and remapping C-T, C-], and creating your own :Gotags to list them. For a reference on how this might be done, check out https://github.com/vim-scripts/VIntSearch/blob/master/autoload/VIntSearch.vim#L258
@alaska That sounds just great!
Yeah, my initial research didn't lead me to any important direction. There are no public api's to create tag stack unfortunately. I'm closing this as I don't have the time to do any kind of hacks to support it. Thanks @gavrie for opening it though.
I'll take a look and submit a PR if I can get anything done in a reasonable time.
+1 for the request
This is now in master. Though I'm still going to work on this for the guru branch and the final commands might change. Fyi.
Very cool! Thanks @alaska and @fatih. This will make the user experience much better.
Awesome feature, but I wanted to point out that just in case anyone ends up here because their <C-t> maps stop working with error message godef: tag stack empty, you can disable this feature with let g:go_def_mapping_enabled=0
I have NERDTree mapped to <C-t> and this line overrode that map.
As always, there is a relevant xkcd.
Nice pic from xkcd @jakebasile :) We tried to emulate as possible as current Vim default commands. By default C-t and C-] are used for jumping between the tag stack so we enabled them by default. Of course as you also mentioned, it can be disabled.
But reading the source code, seems like the default keybinding its bound to CTRL-T not CTRL-t, fixing it now, thanks for bringing this up @jakebasile :+1:
Fixed with https://github.com/fatih/vim-go/commit/16cc4d62c6c14f5d59c7b0e5182fc724d601b97d /cc @jakebasile
It's actually bound to both by default. I disagree with this change VERY strongly, as I learned and have muscle memory for CTRL-t, and I'm sure many others do, too. Plus, it's awkward to type shift for one command and not for another (CTRL-]), which is probably why it's mapped to both.
Vim documentation can be inconsistent with capitalization on these commands. Just look here: http://vim.wikia.com/wiki/Browsing_programs_with_tags The docs say You can press Ctrl-t but also Help: :pop, Ctrl-T, <C-RightMouse>, g<RightMouse>
This should remain an issue where the user has to change it manually.
@alaska I'm trying to be consistent with the tags documentation. You can check it via :help tags. Nowhere does it write it's using CTRL-t. It's all upper case. Our main goal is never create mappings that is not defined in Vim, so this totally makes sense.
But I've also added plug mappings, so you are free to use CTRL-t if you wish by adding the following to your vimrc:
au FileType go nmap <c-t> <Plug>(go-def-pop)
Now CTRL-t will work as previously. I've did it myself, so can you too.
Do you hit ctrl-shift-u or ctrl-shift-d to move up and down in the document? I'm betting you don't. I'm betting you use ctrl-u and ctrl-d like the rest of us, yet they are not mentioned in the documentation as such:
:help ctrl-u gives:
CTRL-U Scroll window Upwards in the buffer. The number of
lines comes from the 'scroll' option (default: half a
screen). If [count] given, first set the 'scroll'
option to [count]. The cursor is moved the same
number of lines up in the file (if possible; when
lines wrap and when hitting the end of the file there
may be a difference). When the cursor is on the first
line of the buffer nothing happens and a beep is
produced. See also 'startofline' option.
{difference from vi: Vim scrolls 'scroll' screen
lines, instead of file lines; makes a difference when
lines wrap}
My point is that vim documentation doesn't make this distinction because it originally didn't need to. Terminals couldn't distinguish between ctrl-a and ctrl-shift-a. Making this a shift-only mapping departs from the default behavior, whether it appears that way from the docs or not.
Valid point. Reverted it back
Of course, I don't think your mapping change would have made a difference in regular vim as, once again, it doesn't really care. But I don't know about neovim, so it's best to play it safe. :+1:
fwiw, I have no problem just turning it off as I don't use the tag stack functionality. Out of curiosity, is there a way to have this GoDef feature enabled but mapped to something else? If base Vim has the tag stack keys mapped to something else, can you query for and use those?
Base Vim already does use those keys, which is kind of the issue here.
If you're asking whether or not you can detect alternative mappings made by .vimrc or some other plugin this is, in theory, possible by doing something like the following, and parsing the variable:
:redir => mappingsText | :silent :map | :redir END
But it's a lot of trouble.
Yeah, that's what I was asking. It's no big deal, as I can just turn it off and manually map GoDef if I still want the tag stack in the future.
Most helpful comment
Awesome feature, but I wanted to point out that just in case anyone ends up here because their
<C-t>maps stop working with error messagegodef: tag stack empty, you can disable this feature withlet g:go_def_mapping_enabled=0I have NERDTree mapped to
<C-t>and this line overrode that map.As always, there is a relevant xkcd.