vi and Vim Introduction |
Russell Bateman
March 2014
last update:
I wrote this for a friend contemplating getting into a real man's editor.
Here's a good cheat sheet for Vim which is a superset of vi. In Vim (http://www.vim.org/ and http://www.vim.org/download.php#mac), there's nothing that thwarts pure vi. However, nowdays, vi on anything like Linux and Macintosh is really console Vim.
http://www.viemu.com/vi-vim-cheat-sheet.gif
Of course, the problem with cheat sheets is understanding them. To help orient you, let's take the two examples from this morning. Reading the following and looking at the illustration will quickly suck the mist away from the cheat sheet.
h (left arrow) j (down arrow) k (up arrow) l (right arrow)
These keys were chosen because the underlying rule in vi is "don't make the editor's hands stray from the keyboard proper in accomplishing anything."
What we did this morning was based on row and column—delete from current character to character marked originally:
d`x delete[range] to x
Alternatively, and perhaps more commonly, we could have used our make to handle a range of lines using the apostrophe (the other tick):
d'x delete [lines] to x
One last example to show you a peek into the sheer power of vi as a text processor. We're going to do some movement and replace tokens with other tokens. This short tutorial will take you about 5 minutes assuming you also spend the time to glance back at the cheat sheet as you do the exercises and contemplate what you're learning. If not, it will take you more like 3 minutes.
Let's replace some Java identifier with "dog".
:q!
This last command wasn't even vi. It was ed. vi is based on ed which is the original UNIX line-oriented editor. The opposite of this command is either ZZ or :w to exit vi with update. To save without exiting, just do :w.
If you've followed along on the cheat sheet as you've done this little exercise, you should now better understand the cheat sheet and you've been given a tiny peek into vi's powerful text processing capability.