node ninja
node ninja

Reputation: 32986

How to jump to Javascript function definition in vim?

I'm using vim with the tagbar, jsctags and taglist-plus plugins. They all seem to work correctly, but I cannot jump to a function/variable declaration. I tried ctrl+] but it doesn't work. Is it possible to setup vim so that you can jump to a function or variable declaration?

Upvotes: 13

Views: 5567

Answers (2)

romainl
romainl

Reputation: 196546

TagBar and TagList don't generate the actual tags file used by Vim to jump to definitions.

If you want this ability to jump you have to generate this file manually from the terminal:

$ ctags -R .

if you use ctags or:

$ jsctags .

if you use jsctags or from Vim itself.

Upvotes: 3

Michael Berkowski
Michael Berkowski

Reputation: 270617

Without needing jsctags, I have the following in my ~/.ctags for handling JavaScript correctly:

--regex-JavaScript=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*new[ \t]+Object\(/\1/o,object/                                                                                             
--regex-JavaScript=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*\{/\1/o,object/
--regex-JavaScript=/([A-Za-z0-9._$()]+)[ \t]*[:=][ \t]*function[ \t]*\(/\1/f,function/
--regex-JavaScript=/function[ \t]+([A-Za-z0-9._$]+)[ \t]*\([^\]\)]*\)/\1/f,function/
--regex-JavaScript=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*new[ \t]+Array\(/\1/a,array/
--regex-JavaScript=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*\[/\1/a,array/
--regex-JavaScript=/([^= ]+)[ \t]*=[ \t]*[^""]'[^'']*/\1/s,string/
--regex-JavaScript=/([^= ]+)[ \t]*=[ \t]*[^'']"[^""]*/\1/s,string/

Using the above, a simple ctags -R generates the appropriate tagfile to match JavaScript function (and variable and object) definitions.

Upvotes: 6

Related Questions