How do I customize which patterns Vim considers a definition for [d and ]d jump commands?
Answer
:set define=^\\s*def
Explanation
Vim's [d, ]d, [D, and ]D commands search for the "definition" of the keyword under the cursor. What counts as a definition is controlled by the 'define' option — a regex pattern that must match the start of a definition line. By default this is set to ^\s*#\s*define (C preprocessor #define directives), but you can customize it per filetype to jump to function or class definitions in any language.
How it works
[d— display the first line matching'define'that contains the keyword]d— display the next such match below the cursor[D— list all matching definition lines in a new window[<C-d>— jump to the first matching definition- The
'define'pattern is matched against entire lines; set it to the prefix that marks a definition in your language
Example
For Python, set 'define' to match function and class definitions:
autocmd FileType python setlocal define=^\\s*\\(def\\|class\\)
With the cursor on my_func, pressing [d will show:
found in current file
42: def my_func(x, y):
For Go, you might use:
autocmd FileType go setlocal define=^func
Tips
- Check the current value with
:set define? - Use
setlocalinFileTypeautocmds to avoid leaking settings across filetypes - These commands search both the current buffer and files in
'include'paths - Pairs naturally with
'include'and'includeexpr'for cross-file definition lookup - In Neovim with LSP, these commands are often superseded by
gd, but[dworks without any language server