Moving around the filesystem in vim

Posted by Jen Tong on February 17, 2017

I’m a bit of a vim junkie. Many years ago I was forced to use it in my CS101 class because it was the only text editor available. The first month was pretty rough, but that was probably because we were writing Java without an IDE. Anyway, once I got the hang of it, I was hooked.

I never studied how to be effective with vim, but instead I’ve added a trick here or there over the years. Even 16 years later, I’m still adding tricks to my vim quiver.

My latest new trick is navigating around the file system inside a vim buffer. It feels a bit awkward at the first, but once you get the hang of it, it’s a lot faster than using the OS GUI to find files.

Moving between files

I used open a new vim process from bash, make edits, :wq, find the next file, and repeat. It turns out there’s been a better way the whole time – the edit command: :e.

You give it a file name, and vim switches the buffer that file in the current directory. If the file does not exist, vim will create a new file when you write it.

:e dinosaurs-are-awesome.txt

You can also give it a path to edit a file somewhere else.

:e /secret-stuff/embarassing-fanfics/

And use escape sequences to cope with filenames that contain spaces.

:e ~/Downloads/ProposalDeck\ finalVersion\ Copy\ 2.doc

If you made some edits in your current buffer, but want to ditch those changes, add a ! just like you do in a :q!, a.k.a. the only vi command that emacs users know.


Oh, and if you’re not using tab completion here, you’re missing out. Tab completion works for filenames and directories. Consider it an added incentive to start your commonly edited files and git repos with distinct letters.

Moving between directories

If you know where you want to go, use the :cd command. If you kind of know what you want, use :cd and abuse tab completion.

:cd /tmp

If you have only a vague idea of what you want, you can navigate around directories with the :e command.

:e .

Which yields a screen like this:

Pressing enter opens whatever is under the cursor.

You can move around with arrow keys, but that’d be a sin against vim. Instead, use your vi file navigation-fu. Most of it works, including moving to line number (:42), regex searches (/[hHarry]), and even macros (qa↑↑↓↓←→←→q).

Setting a different default directory

By default vim will start it’s directory context in your home directory. This is a reasonable default, but if you want something different, just add a :cd command to your .vimrc.

:cd ~/dinosaur-fanfics/


So yeah, so that’s how you can move around in vim. It’s also a core feature that I somehow avoided for 16 years.

vim users: Are you still picking things up? If so, tell me about something you picked up recently :)

Non-vim users: 1) why are you still reading? 2) How do you tolerate other text editing interfaces?