Navigating and Changing Directories

If you’ve used Vim a lot, you may have accidentally discovered that you can view a directory and move through it using keystrokes similar to those used with files.

Let’s consider a directory containing many .c files, ex-050325 (this happens to be the directory containing the compilable source for the original vi editor). Edit ex-050325 with:

$vim ex-050325

Figure 15-3 is a partial screenshot of something similar to what you might see.

Vim “editing” the ex-050325 directory
Figure 15-3. Vim “editing” the ex-050325 directory

Vim displays three types of information: introductory comments (preceded by equals signs), directories (displayed with trailing slashes), and files. Each directory or file is on its own line.

There are many ways to use this feature, but with little effort you can be immediately and intuitively productive with standard Vim motion commands (e.g., w to move to the next word, j or the down arrow to jump down one line) and by clicking the mouse over entries. Some particular features of directory mode include:

  • When the cursor is positioned over a directory name, move to that directory by pressing the ENTER key.

  • If the cursor is over a filename, pressing ENTER edits that file.

Tip

If you want to keep the directory window around for further work in that directory, edit the file under the cursor by typing o, and Vim will split the window, editing the file in the newly created window. ...

Get Learning the vi and Vim Editors, 7th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.