A few days ago I wrote about computational survivalists, people who prepare to be able to work on computers with only software that is available everywhere. Of course nothing is available everywhere, and so each person interprets “everywhere” to mean computers they anticipate using.
If you need to edit a text file on a Windows computer, you can count on Notepad being there. And if you know how to use Windows, you know how to use Notepad. You may not know advanced features (does Notepad even have advanced features?) but you can get around.
On a Unix-like computer, you can count on
vi being there. It was written about four decades ago and ships with every Unix-like system. This post will explain the bare minimum to use
vi in a pinch.
Arrow keys should work as expected. If for whatever reason your keyboard has no arrow keys or the keys don’t work, you can use the four home row keys h, j, k, and l as arrow keys. Use trial and error until you figure out which way each one moves.
vi has two modes, command mode and insert mode, and it opens in command mode by default. Typing an h, for example, does not insert an h into your text but instead moves the cursor to the left.
To edit a file, you can enter insert mode by typing
i. Now the characters you type will be inserted into your file. What if you want to delete a character? You can delete too, but it’s still called “insert” mode. The arrow keys still navigate, but typing an h, for example, will insert an h into your file. To go back to command mode to navigate more, type the Escape key.
If you’re not already in command mode, type
ESC to enter command mode.
If you haven’t made any changes, you can type
:q and Enter from command mode to exit. If you have made changes, you can use
:wq to save the file and exit, and
:q! to exit without saving.
Alternatives to Escape
If your keyboard doesn’t have an escape key, as on some Macs, you can use Control-[ as an escape key. (There’s a story behind why this works.)
For daily tips on using Unix, follow @UnixToolTip on Twitter.