Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 869
node ninja Avatar asked Mar 14 '12 15:03

node ninja


People also ask

How to jump to definition in vim?

To go to definition, instead of Ctrl + ] , do Ctrl + \ + g .

How do I delete a function in Vim?

If your cursor is on the line with the function name, try d } . It will delete everything to the next block (i.e. your function body). Within the function body itself, d a p will delete the 'paragraph'. You can delete a curly brace block with d a } .


2 Answers

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.

like image 28
romainl Avatar answered Oct 10 '22 08:10

romainl


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.

like image 90
Michael Berkowski Avatar answered Oct 10 '22 10:10

Michael Berkowski