vimtricks.wiki Concise Vim tricks, one at a time.

How do I jump to the local or global declaration of the variable under the cursor without ctags?

Answer

gd and gD

Explanation

Vim's gd and gD commands jump to the declaration of the identifier under the cursor by searching upward through the file — no ctags setup required. gd searches from the top of the current function, while gD searches from the beginning of the file. These are fast, zero-config alternatives to tag jumps for exploring unfamiliar code.

How it works

  • gd — "go to local definition": searches backward from the cursor to the top of the current function scope (the first blank line above), then forward for the first occurrence. Ideal for local variables.
  • gD — "go to global definition": starts the search from the very top of the file, making it useful for global variables, constants, and top-level declarations.

Both commands highlight all matching occurrences as they search, making it easy to spot where an identifier is defined vs used.

Example

Given a C file where count is declared at the top:

int count = 0;

void increment() {
    int step = 1;
    count += step;  " cursor here on 'step'
}
  • gd on step → jumps to int step = 1; (local scope search)
  • gD on count → jumps to int count = 0; (global file search)

Tips

  • Unlike <C-]>, gd/gD work without a tags file — they use simple text matching
  • After jumping, use <C-o> to jump back to your original position
  • For more reliable jumps in large projects, combine with :set tags and ctags for <C-]>
  • 1gd skips the current cursor position if it is already on a declaration

Next

How do I get just the filename without its path or extension to use in a command?