Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@latentflip
Last active April 14, 2021 03:29
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save latentflip/57bf8f9edde531ee979e to your computer and use it in GitHub Desktop.
Save latentflip/57bf8f9edde531ee979e to your computer and use it in GitHub Desktop.
vim, gf, and node

Vim, gf and node.

So, I just learned that gf exists. If your cursor is over a path in vim, and you type gf, it'll open that file/dir in a new buffer. You can also open in a new window/tab as detailed here.

In node, it'd be great if you could jump to a required file, huh? Trouble is, typically you don't put the .js on your require('./path/to/a/js/file'). No matter, vim has your back, just add set suffixesadd+=.js to your .vimrc and vim will try adding .js and see if it can find that file instead.

If you do a lot of spelunking in node_modules, it'd be great if you could jump to the directory of a required npm module too, right? A la, require('my-awesome-module'). Well, you can add set path+=$PWD/node_modules to your .vimrc too, and vim will add node_modules to the path, and jump to it's directory in node_modules (caveat: you must have opened vim from your project root for this too work).

For your cmd+c convenience:

set suffixesadd+=.js
set path+=$PWD/node_modules
@mrknmc
Copy link

mrknmc commented Dec 10, 2014

This is pretty sweet, thanks!

@svnlto
Copy link

svnlto commented Dec 10, 2014

@latentflip
Copy link
Author

Further to this, using ctrl-^ or ctrl-o you can jump back to where you were before you did gf.

@coderek
Copy link

coderek commented Sep 27, 2018

" works for nested files
set path+=node_modules;~

@thiagodebastos
Copy link

Awesome!

@voischev
Copy link

voischev commented Apr 8, 2021

for me set path+=**

@bumaociyuan
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment