Essential and practical vi – Part 1


Basically I wanted to make a post on vi buffers (editing multiple files at once) but discovered I couldn’t do it without more vi related stuff. Therefore a three part more elaborated post.

Working in the Linux terminal every day, for me it is a no-brainer to use vi, more specifically vim. It’s installed by default on every Linux system and it is very, very powerful. The only thing that could hold you back is, as it is jokingly called, the vi ‘learning wall’ (in contrast to ‘learning curve’).

First the basics in a nutshell! In vi you can use 3 modes:

  1. Command mode. You start out in this mode and has many functions like super fast copy and pasting or deleting of text. Return to it from the other modes with the ESC key
  2. Insert mode. The mode to actually insert tekst. You can enter this mode in different ways, most often via the i, a, I or A keys in command mode
  3. Last line mode. When you press the : key from command mode, you’ll end up in last line mode. As with the command mode you can use this in many different ways. Simply quitting and saving are mostly used here

Basics like navigating through the file when you do not have arrow keys (h, j, k and l), inserting (i) and adding (a) text and quitting (:q or :q!) or saving (:w or :x) are outside the scope of this post. For these take a look at a nice cheat sheet from a post 2 years ago.

Here are things I’m using every day, so it should be a very practical post. In no particular order:

An error (in a log, PHP error, whatever) often comes with a line number where the error originates. To open the file and immediately jump to the correct line (in this example line number 114):

# vi +114 /path/to/file

This is the same with the read only variant of vi: view. I use view when I’m pretty certain I’m not going to edit the file:

# view +114 /path/to/file

Let’s say I’m in view mode and decide I DO want to edit the line, for instance comment the current line out, I can do so by adding or inserting  like normal (a or i). It will give a warning, but that’s OK. When you’re done you can write and exit with the ! to write the file anyway:


This only works when you have enough privileges to it to begin with. When you’re in a file (view or vi, it doesn’t matter) and you have to edit it with elevated permissions, you can also do this from the last line mode:

:x! sudo tee %

Both can also be run with w, where you only write the file instead of write and quit. 

When you’re in the file, you can set the line numbers anyway. This can also be set by default in your .vimrc file, but that’s for a later post. Display linenumber in your file:

:set nu

Disable them again:

:set nonu

Also a bit basic, but navigation wise I use these shortcuts on a daily basis, in command mode:

  • G jump to the end of the file
  • gg to jump to the first line of the file
  • :89 jump to line 89
  • { } jump paragraphs up and down
  • $ jump to end of the line
  • ^ jump to the start of the line

Deleting stuff! I delete a lot of stuff apparently because I think I know and use every delete option available.

Single characters, typos, an extra letter for example, are simply deleted in command mode with the x. In this way you don’t even have to enter insert mode. I often also have to replace just a single character: move your cursor to the character to be replaced, type r and then the new character.

Power deleting a bunch of stuff at the same time is just as easy from the command more. For the current word:


Next five words:


Current line:


Next 8 lines:


Easy as pie (or vi, if you want). Also know that deleting the next 8 lines for example, places them in your paste buffer. Comes in handy when you want to move a couple of lines to another location. It’s just for the very last thing you deleted though, so don’t delete an extra blank line for instance.

Deleting the rest of the line (from my cursor):


And deleting the rest of my file (from my cursor):


Talking about paste buffers: let’s do some copying around in the file. For this we’re using the y for yank. As with deleting words, you can almost imagine the commands for yanking. Yanking the current word:


Next 10 words:


Current line:


Next 6 lines:


When the text is in your buffer, you can paste it with p after the cursor and P before the cursor.

I briefly mentioned inserting tekst with i (insert), I (insert at start of the line), a (add) and A (add at the end of the line). What I’m probably using more often though to enter text is o (insert a new line after my cursor) and O (insert a new line before my cursor). Just so that you know.

Undoing when you made a mess is a push of a button in command mode: just the letter u. When you made several typos on the same line undo them all with U. Redoing is also possible with the dot (.). When you want to be sure not to save anything, quite the file without saving with :q!

Got to love vi! I want to cover more intermediate stuff like searching, find and replace, marks, commands, read in and write ins, etc. I will do so in the next post.

Update: Complete series here, here and here!

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.