tags:

views:

210

answers:

4

Hi all !

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 ?

+3  A: 

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\+')
meeselet
seems like {ident} doesn't match the keyword under the cursor. :(it just has to be replaced by the tag you want.got any idea to get the keyword under the cursor ?
kamou
yes, {ident} is for syntax highlighting in vim's documentation
meeselet
+3  A: 

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>

Nathan Fellman
but how can I call tag or stag with the keyword under the cursor ? {ident} doesn't seem to work..
kamou
yes, but I need it as a command for my key bindingsI tried:nmap <unique> <silent> <Leader>tg :exe 'vsplit \| tag <C-r> <C-w>'<CR>but it didn't work... I just got my vi splitted
kamou
thaks, expand("<cword>") was just what I needed !
kamou
this is the line I added in my rc file for those who could be interested:nmap <unique> <silent> <Leader>tv :exe 'vertical stag <c-r>=expand("<cword>")<CR>'<CR>
kamou
A: 

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.

Antony Hatchkins
A: 

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.

Dave Kirby