Geoffrey Mariette
Feb 13 · 6 min read

The perfect VI cheat sheet for ops

Photo by Federica Galli on Unsplash

Or how to impress your colleagues.

It’s been a while since I’m using vi instead of editors like nano, and I will share with you some of the commands I use daily to improve your skills and your productivity as well.

Let’s master the basics first, then we will talk a little bit of advanced commands.
First thing to know, is the differents modes that vi allows you to use.
To switch between modes, you always have to go back to what we can call “no mode” by just pressing escape key.

Once here, many commands are available, like the following :

  • i” : allows you to enter into the edit mode
  • :” : use some vi commands which will not edit directly your text
    For example, “:q(!)” quit the editor (esclamation point if you don’t want to save your modifications, I.E force) while “:w” save your changes.
  • r” : for replace, will replace the char where your pointer is
  • !” : followed by a shell command will run it directly (:! whoami will print you your current unix user)

That was just some basic examples. Now it’s time to create our first script with vi to really learn how to use the editor.

For this practice I’m just using a docker image debian:latest, and vi a script (I can share the example on github if someone matter).

First thing to do to enter in edit mode is press “i”

Note the INSERT mode in the bottom left corner

You’re now able to enter some text. Let’s do basic with an echo command.

Don’t forget to press escape once your command is written, you will by doing that escape the insert mode and get back to the main vi mode.
Now let’s say you want to replace the first char of your echo “m” by a “M”.
In a traditional editor, you will delete the m and write M instead. You can do that is vi too by entering again in insert mode. But you can also :
- delete the m char by pressing “x”, enter again in insert mode via “i”, then write “M”, then press escape to get back to vi standard mode… Not efficient isn’t it ?
A better vi way will be to directly replace the caracter you want. To do that, put your cursor on the m, press “r” (like replace) then press “M”. m char will be replaced by M !

Almost forgot to say, don’t use your cursor to get back to my word ! Instead, use the finder method by pressing / and write my. Here you are, on your word !

m char replaced by M using r command

We now would like to persuade ourselves that we aren’t afraid of vi by copying our line and paste it line 4. By the way, how do we know about the line numbers ? :set number (or :set nu — :set nonu(mber) to remove line numbers).
To copy a line, put your cursor on it, and press “yy” to copy, then “p” to paste it right after.

line copied with yy and pasted with p

Impressive isn’t it ? But we can do better. Let’s change the script word of the line 4. Put your cursor on the first char of script. Then press “cw” (change word) to delete it and immediatly enter in insert mode (if you only want to delete it, use “dw” (delete word)).

manipulate words with cw — dw

But let’s be realistic for a sec… This doesn’t looks like a masterpiece… So we can undo changes with “u”. You can undo changes multiples times by pressing u as much as you want (ctrl+r to redo).

Let’s add some content into the script to have more lines to manipulate. Remember the command “yy” to copy one line. Guess what… you can copy two (or X) lines by just adding the number of the lines you want to copy between the two y (“y2y” will copy 2 lines). This logic can be applied to many vi commands (for example with “dd” (to delete one line — “d3d” to delete 3 lines).

copy/paste multiple lines

I hope you like indentation like me, so your first thought when seeing this screen was “it’s fuc**** ugly”, which I 100% true.

To add some indentation quickly, we can use one of my favorite functionality of vi, called visual bloc. Press ctrl+v to enter in Visual bloc mode (your cursor as to be, in our case, for exemple, at the beginning of the line 4 or 9). This mode allows you to select stuffs with your keyboard arrows.

All lines which need indentation are selected

Once you have selected the lines from 4 to 9, you can insert chars into them by pressing this tricky keyboard combinaison :

  • maj + i : you will enter in insert mode
  • tab : to insert indent (note you will only see for the moment one line indented)
  • escape escape : this will apply your changes to all lines
Press escape escape to apply your changes to all lines

Magic isn’t it ! We can also update many lines if we want. Maybe our script is finaly a masterpiece…
Select all script word with the visual bloc mode, then :

  • press c : you will enter in change mode
  • enter the word you want, here masterpiece
  • escape escape to apply your changes
press c to change word

This is cool. But vi allows us to replace char like in a traditional editor where you use the classic ctrl+h. Here it is just a little bit more… unix

Press : then enter a sed expression to replace script by masterpiece.
:%s/script/masterpiece/g then press enter, and it’s done !

I think that’s pretty much it, you will have some basic and more advanced command to improve your feeling with this editor.

Dropping just some extra content for the ones still here.

  • :line_number => jump to the line of your choice
  • gg => go to first line
  • maj+G => go to last line
  • maj+G+o => insert directly at the last line
  • yw => copy word (y2w to copy two words)
  • :%d => delete all content
  • ^ => jump at the beginning of your current line
  • $ => jump at the end of your current line
  • :%s/^/\t/g => add tab at the beginning of every line
  • set ff=unix => set fileformat as unix, useful when script are coming from a Windows system
  • sy(ntax) off/on => turn off/on syntaxic coloration
  • set (no)wrap => wrap or not long lines

Geoffrey Mariette

Written by

Working in IT for 10 years, just want to share with others.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade