I always have at least two directories in my projects:

  • One with information about the projects: info.txt, log.txt
  • One with code

And every project has a name.

How to group and jump efficiently between those directories?

How to save those multiple folders in my session?

Other editors do this task natively, like sublimetext:

Sublime Text

But I don't know how to quickly switch from log.txt in one directory to package.json in another one.

Having both directories together wouldn't make sense, since one directory goes into a git repository and the other one don't

How do you do it?


2 Answers 2


One handy keymapping I found years ago and have had in my vimrc ever since is:

cnoremap %% <C-R>=fnameescape(expand("%:p:h")."/")<CR>

It's a command-line mode mapping that expands %% to the directory of the current file, complete with any necessary escapes for spaces or other funny characters.

Combine this with padawin's buffer suggestions and you've got fairly quick access to any directory you have an open file in via :cd %% or :e %%new_file_to_open.

If you're persisting your open buffers in a session file, then this automatically comes for free.

  • Great trick! however, you need to keep those buffers open and keep changing directories manually. Like sublime and vscode I'd like to keep multiple directories open and save them in a named session. I found this, but looks like it's still work in progress: github.com/mihaifm/vimpanel
    – lalo
    Commented May 24, 2019 at 17:06
  • You didn't mention it explicitly, but this should also enable you to :e %%../../other/package/file_to_edit.cpp! Commented Nov 30, 2023 at 19:45

If you open vim with your two files (vim path/to/log.txt path/to/package.json), each file is open in a buffer. You can switch from buffers to buffers in different ways:

  • :bnext and :bprevious (to go to the.... next and previous buffers)
  • :buffer n where n is a number, each buffer has a number, you can see them with :buffers (For these I have two mappings: <space>a for previous and <space>s for next, to easily go from buffer to buffer)

I invite you to read:

:help buffers to be more familiar with the concept of buffers, windows and tabs.

Regarding the directories, I'd suggest to start your vim instance from the root directory of your project and work from there.

  • 1
    Note that the :buffer command can also work with filenames, as long as the name isn't purely digits.
    – 8bittree
    Commented May 23, 2019 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.