Mastering Vim: Opening files

I think every *NIX admin knows this little gem – the Vi or Vim (Vi Improved) text editor.
While the origin vi was written by Billy Joy, vim was cloned, improved and released in 1991 by Bram Moolenaar.

In the blog series Mastering Vim, I’d like to show you some tips & tricks.

Said that, I’m not that Vim pro-user you might expect now. I’m just somewhere in between the noob and the pro.

If you’ve any tricks (or corrections) related to the post topic, feel free to leave a comment and enlighten me 😉
If you don’t know Vim yet, I’d recommend Vim Adventures (a browser game to learn Vim) to you, as I’m not covering the basic in the upcoming blog posts.

My history with Vim

I’m using vi & vim for years now and I think I master some of it. However, even though after coding a lot of Python, forced by enterprise customers with no access to IDE’s, I catch myself surprised by it from time to time.

You might know this scenario: While you’re using vim, you unintendedly press a keystroke in the command mode and end up somewhere in the dark of the vim universe – and of course you’ve absolutely no idea what just happend and how you get there.
That is how vim works and when you start to learn new things 😉

I once heard this joke about vim:

How do you generate a random string?
Simply put a web developer in front of vim and tell him/her to save & exit.

I was exactly that web developer a (few) years ago, before I started using vim on a daily basis.
Eventually, I became more aware of the vim features and today I know how to save a file, exit that beast and probably a few more things.

This first blog post is all about opening files in vim.

Opening files from the command line

Opening a single file

Opening files in vim from the command line is quite simple. The most straight-forward command is obviousely:

vim /path/to/file

Opening a single file on a specific line

But did you know that you can also directly jump to a specific line directly after opening the file?
You ask yourself why on earth you want to do that? Well, you might need it in programming when an exception is thrown on a specific line. Or do you know these SSH (error) messages?

Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
Please contact your system administrator.
Add correct host key in /Users/dbarton/.ssh/known_hosts to get rid of this message.
Offending RSA key in /Users/dbarton/.ssh/known_hosts:29
RSA host key for has changed and you have requested strict checking.
Host key verification failed.

The SSH host key changed, which means you’ve to delete the old key. The OpenSSH client is so friendly and prints out the file & line on which the old host key is stored:

Offending RSA key in /Users/dbarton/.ssh/known_hosts:29

With vim, you can easily open that file and jump to the corresponding line:

vim /Users/dbarton/.ssh/known_hosts +29

Opening multiple files

Of course you can also open multiple files with Vim.
This is handled in a separate blog post about working with multiple files.

Opening files in vim

Opening a new file for editing

If you’re already in vim, then there’s no need to exit it just to open a new file.
Simply switch to the command mode (ESC) and type:

:e /path/to/file

That will open the file for editing. You can also use the tab-key for autocompletion of the path.
Please note that the current file must be saved, or you’ve to use :e! to discard the unsaved changes.

Reading in a file into the current buffer (file)

Have you found yourself in the situation, where you had to merge the content of two files?
There’s always the shell way by using cat foo >>bar.
But what about when you don’t want to append the content on the end of the file, or if you’re already in vim?

Of course we can also to that! Instead of :e, we simply use :r:

:r /path/to/file

Can’t remember those commands?
Just try to remember, :e means edit, while :r  means read.

The file browser

Did you know Vim also has a file browser called Netrw?
You can access it by defining a directory instead of a file, for example:

# On the command line
vim /path/to/directory/

# In Vim command mode
:e /path/to/directory/

Of course there are the obvious keys you might need, for example:

  • The cursor (or hjkl) keys to browse through the list
  • The return key to enter a directory or open the file

But did you know about these:

  •  will browse up one dir
  • i will change the browser style
  • I will hide the header
  • D will delete a file (confirmation required)
  • R will rename a file
  • s will change the sorting (name, time, size, exten)
  • r will revert the sorting

I really like the collapsing tree browsing style (I think it’s #4).
You can access it by pressing i 3 times (by default).


  • spanish to english Reply

    I was exactly that web developer a (few) years ago, before I started using vim on a daily basis.

  • bandale Reply

    good work

  • aaa Reply

    GOOd JOB

  • run 3 Reply

    There is a lot of good information, I hope it has a better approach so that the interesting things can spread. Useful information.

  • ObstFlieg Reply

    I have a really basic question: I open vim inside a folder like vim ~/.dotfiles. As you describe I can browse the folder structure really neatly and navigate to a file. Once inside the file I would like to know if there’s a quick way to move “up” again to the tree structure with vim?.

    Greetings from Germany 🙂

  • theessayservice Reply

    If the program is not greatly improved and that include developers effort in improvements, we will look at any and all alternatives.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.