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?

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
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
SHA256:E2LnW3A+iPavhFaCpBVIP3A6Nl0LrbMT3usFD7na1Co.
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 example.confirm.ch 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).

28 Comments

  • spanish to english

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

  • aaa

    GOOd JOB

  • run 3

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

  • ObstFlieg

    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

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

  • Property Preservation

    If your looking for Online Illinois license plate sticker renewals then you have need to come to the right place.We offer the fastest Illinois license plate sticker renewals in the state.

  • www.visualfilmworks.com

    I am genuinely thankful to the holder of this web page who has shared this wonderful paragraph at at this place

  • hair removal

    thanks for this! such a great help!

  • geometry dash

    I think the post has information very well.

  • mamalactea.com

    Its very minimal, efficient, and it looks great!

  • Dr. Andrea Natale

    A debt of gratitude is in order for the blog entry amigo! Keep them coming…

  • Ross Levinsohn profile

    I liked your article and I hope you will have many entries or more

  • click

    I keep wondering how to open a vim files. Thanks!

  • Jason

    Fabulous post, you have denoted out some fantastic points, I likewise think this s a very wonderful website. I will visit again for more quality contents and also, recommend this site to all. Thanks

  • Local Citations

    I really don’t know how to use Vim haha my poor brain.

  • guitar lessons

    I really enjoyed reading your article. I found it in an informative and interesting work, so I think it is very helpful and knowledgeable.

  • kitchen remodeling

    God thank you, I find that page! This inspiring article! continue to send

  • Moving Help

    Outstanding tutorial. Thanks you!

  • Wikipedia Page Writing

    Oh great! I was thinking of this yesterday and I don’t think so that somebody will already publish this issue, blog.confirm.ch do you want to create a wiki page for your blog

  • https://surveyornewcastle.com.au/

    Pretty good post. I’m very grateful to the author, this really changed my thought. I’m looking forward to acknowledge this website as one of the best for new information.

  • Renungan Harian Katolik

    Pretty good post. I’m very grateful to the author, this really changed my thought. I’m looking forward to acknowledge this website as one of the best for new information.

  • https://plasterershobart.com.au/

    Thanks for sharing this. I found out about a lot of great things. I like the way you make your blog posts.