Learn Vim at your own pace with my self-study Core Vim Course.

Learn more

Learn Vim at your own pace with my self-study Core Vim Course.

The :edit command

#14

Run time:

This episode focuses on the :edit command. I demonstrate how to open files whose location is relative to the current working directory, then I show how to create a mapping which makes it easier to open files in the same directory as the one in the active window.

Shownotes

Depending on what arguments you supply, the :edit command may do one of 3 things:

  • pass it the name of a file, and it will open that file in the current window
  • pass it the name of a directory, and it will open a file explorer for that directory
  • when called with no arguments, the :edit command will revert to the latest saved version of the current file. To discard unwanted changes, you will have to force this command with a trailing ‘!’ bang.

Pressing the <tab> key triggers auto-complete for directories and files that match the characters you have typed so far.

If you want to open several files from the same directory, specifying the full path can start to feel like a lot of extra work. You could set the working directory to match that of the file being edited in the current window by issuing the command:

:cd %:h

However, this can make it harder to locate some files, because you have to climb the directory tree before drilling down again. I prefer to create a set of shortcuts for opening files located in the same directory as the current file:

let mapleader=','
map <leader>ew :e <C-R>=expand("%:p:h") . "/" <CR>
map <leader>es :sp <C-R>=expand("%:p:h") . "/" <CR>
map <leader>ev :vsp <C-R>=expand("%:p:h") . "/" <CR>
map <leader>et :tabe <C-R>=expand("%:p:h") . "/" <CR>

Now, I can run ,ew and it expands to :e path/to/directory/of/current/file/. This makes it really easy to open several files from the same directory.

The ‘ew’ command stands for open in window. The other variants stand for open in split (‘es’), open in vertical split (‘ev’) and open in tab (‘et’) respectively.

Update

Thanks to Gary Bernhardt, here is a less horrible way of creating the same mappings:

cnoremap %% <C-R>=fnameescape(expand('%:h')).'/'<cr>
map <leader>ew :e %%
map <leader>es :sp %%
map <leader>ev :vsp %%
map <leader>et :tabe %%

Additionally, this allows you to expand the directory of the current file anywhere at the command line by pressing %%. A top tip from Max Cantor!

Further reading

Comments

Level-up your Vim

Training

Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify

Publications

Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from thoughtbot..