Fandom

Vim Tips Wiki

Jumps to a local/global definition by same key

1,624pages on
this wiki
Add New Page
Talk0 Share
Tip 1455 Printable Monobook Previous Next

created 2007 · complexity basic · author Shotaro Aoyama · version n/a


When you want to jump to a definition of a variable, what do you do? Use C-] or gd?

C-] finds only global variables (and functions; ctags extracts only global objects).

On the other hand, gd detects only local variables.

I think it's a bit complicated to choose between them. So I wrote this function:

function! GoDefinition()
  let l:pos = getpos('.')
  normal! gd
  if getpos('.') == l:pos
    execute 'tag' expand('<cword>')
  endif
endfunction
nnoremap <C-]> :<C-U>call GoDefinition()<CR>

This function first does gd to try to find a local definition of a variable under the cursor, and if it failed then probably the variable is a global variable, so it tries :tag.

This way, you can jump to the definition of both local and global variables with the same key.

CommentsEdit

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.