Question

I'm trying to map my own shortcuts in vi. I want to be able to open a splitted window containing the declaration of the function I'm calling the shortcut on. something like this in my vimrc:

nmap <unique> <silent> <Leader>tg :exe 'vsplit'\| equivalent of <C-]>

but what's the equivalent of C-] in command mode ?

Was it helpful?

Solution

To open vim at the line containing the tag foobar type this at the shell commandline:

vim -t foobar

To jump to the same tag on the vim commandline type:

:tag foobar

If you want to split the window and jump to the tag in the new window type this in vim's commandline:

:stag foobar

If you want a keystroke that specifies "the word under the cursor", then according to this question, you can use the CTRL+R CTRL+W to get that:

:tag CTRL+R CTRL+W

You can also use :

:nmap <leader>w :tag <c-r>=expand("<cword>")<c-r>

Now typing <leader>+w (which is \ in my setup, so I'd press \w) will be the same as typing :tag <word under the cursor>

OTHER TIPS

See :help CTRL-]:

CTRL-] Jump to the definition of the keyword under the cursor. Same as ":tag {ident}", where {ident} is the keyword under or after cursor.

Edit

Not sure if there is a built-in for this, but the following seems to match the keyword "under or after" the cursor:

matchstr(getline('.'), '\%'.col('.').'c\s*\zs\k\+')

you could also write (to map it for example to Ctrl-Q):

:nmap <C-Q> <C-]>

you could also consider :nn[oremap] in such cases to avoid nested/recursive mappings.

You can jump to the definition of the word under the cursor with exe "tag " . expand("<cword>"). The expand function replaces special tokens with their meaning - this includes a filename under the cursor, the current file name, etc. See Vim's help for the expand() function for the full set.

Alternatively you could use the :normal command, which lets you execute normal-mode key sequences from an :ex command.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top