File browsing in Vim

Well-heeled Vim users will no doubt see the following as being pretty obvious. For anyone else like me fumbling around with Vim to the point of giving up, I hope this at least saves you the time I spent until the small hours of the morning!

The way I used to think

Coming from working with text editors like jEdit which, like others, has a file browser in a split pane, I had been trying to reproduce this in Vim.

Vim split into 4 panes

However, since upgrading to Vim 7, this had been causing irritations, like this routine to open a file:

  • Select a file in the left pane.
  • Click in a window in the right pane to indicate that was the one to open it in.
  • Go back to the file in the left pane and Shift-p to open it.

Forgetting to select the window to open it in meant it would open in the left pane instead, which was supposed to be my file browsing pane.

Not only that but for whatever reason I was getting an odd refresh when I moved to another application and back to Vim. This refresh was annoying enough to have me spend time reinstalling netrw, attempting to compile Vim for myself (which failed) and even taking time to try out Textmate.

All in all I was getting pretty fed up and was even converting the Euro to Aussie Dollar to see if I had enough pocket money to buy Textmate. Luckily, the stubbornness of anyone who has put the time into learning Vim wouldn’t go away and I stopped to have a look at the new tabs in Vim 7. It was then that I realised I’d been thinking about this all wrong…

Vim is about modes

My mistake was to think of any window in Vim as having a specific role and ignoring vim’s modal nature. I realised that file browsing was always there in the background as I edited a file and I didn’t need to have windows specifically assigned to file browsing.

The joy of :Ex (very poor, I know!)

The magic lies in the fact that the file browsing functionality of netrw is following you around as you go. For example, if I open …/Sites/zend_framework/library/Zend.php in a window…

Vim split horizontally with files

…and then hit :Ex I will be in that files directory…

Vim split horizontally with netrw in top

However, if I open a file from another directory in another window hitting :Ex for that window will bring up it’s directory. This then allows me to have a contextual file browser available at any time without having to move my hands from the keyboard.

Admittedly this is not exactly a nirvana attaining moment of enlightenment, but it’s certainly one of those “Aha!” moments that reinforces your faith in your tools and the time spent learning them.

Further Reading

Beginner’s guide to Vi Improved (vim)

6 Responses to “File browsing in Vim”

  1. Kevin Says:

    I was as frustrated as you and found a couple of other useful ex-commands. Vexp, Hexp, and Sexp.

    I came from an emacs background and liked being able to mark multiple files while browsing a directory and rename, copy or execute any other command on them. If the same functionality is here in vim I haven’t found it yet.

  2. Jerri Says:

    Thanks for this little article. I never even knew about :Ex. Funny. I learn something new about vim every day. How deep is this rabbit hole? :)

  3. Campbell Says:

    About that “odd refresh” — see :help g:netrw_fastbrowse you probably should have it set to 1 or 2.

  4. jonathan Says:

    thanks for the Vim tip. I too spent ages (actually about three years) tryuing to recreate file browsing from a folder tree in Vim. The thing is I didn’t want a solution that required a complex set of instrucitons in the .vimrc file. You’re right the answer is – just use :Ex where ever you go.

    PS love the rounded corners!

  5. Izkata Says:

    Here’s something I just found by accident:

    After using :Ex to get into the file browser, if you select a file and just hit v, vim will split the current view vertically and open the file.

  6. Izkata Says:

    Better yet:

    :help netrw-dir

    Here’s a list of all the commands that work with the file browser, including: o Open file with a horizontal split v Open file with a vertical split c Make browsing directory the current directory - go up a directory

    ..and many more!

Leave a Reply