It seems to me that a lot of people dislike Vi. I thought a short blog on the essential commands might be helpful. If you just want to know how to either “save your changes and exit” or “exit and abandon your changes” just jump to here.
I’ve collated a list of what I think are the minimum commands to get you started with Vi. This post is about the basics.
Is it Vi or Vim?
If you’re using Linux or possibly Git – you may find that when you call “vi”, you actually get “vim” instead. Vim is “Vi Improved”. They’re very similar, but not identical. Keep this in the back of your mind if something doesn’t work like the internet said it would. Running “vi -h” will tell you for sure.
How do you start vi?
Starting Vi from a command is easy, type “vi” and enter, or “vi filename”. If you’re using Git, you might find yourself in a Vi session in order to record some critical information.
The secret to Vi
It’s useful to know that Vi actually has two “command-states”. The first is reached by pressing the “esc” key. It tells Vi that you want to enter a command. Hitting “esc” is like telling Vi: stop editing, I’m about to enter a new command (or I’ve finished). Optionally, you may enter a number before a command (to execute the command multiple times). Generally, edit commands tend to be no more than one or two keystrokes. You press “esc”, then an edit command, and then you start typing your text changes. Think of this mode as the “edit mode” – the commands you are entering immediately affect the text in the document. Pressing “escape” is like clicking on the menu bar if you had a mouse, entering a keystroke after that is like clicking on a menu item.
The other “command-state” is reached by pressing esc first, then it’s followed by either a colon (“:”) or sometimes slash (“/”) too. Consider this like the “global command state” –commands entered after the colon (or sometimes “/”) will typically apply to whole file (or some range of it). Unlike the edit mode above, these commands are often more than a couple of keystrokes long. You can review and edit these commands before executing them. They are only executed when you press the enter key. If you go wildly wrong, you can cancel what you’re typing by pressing esc – because “esc” is your friend. Examples are: saving and exiting, saving without quitting, searching and replacing text, and retrieving the contents of a different file into the one you’re working on.
Anytime you get confused about what command state you’re in (whether you’re still inserting text, or overwriting, or searching) just hit esc and enter the command you want. Use it like a reset key, esc is your friend and it is used to start all commands and it also signals you have finished if you are editing text.
Vi begins in command mode – since it can’t know what you want to do until you enter a command.
Finally, the commands are case sensitive, so entering “j” moved the cursor to the left (same as left arrow), but entering “J” will join the current line to the next (deleting the carriage return at the end of the current line).
Let’s start with the end. Getting out.
How to save and exit, and also how to leave without saving any changes – are two very important commands. There are a multitude of ways to leave vi, I’m telling you about the ones that I prefer. You can Google how to exit Vi, and pick whatever works for you, just make sure you understand whether you are saving before leaving.
(Remember, because these are commands, you start by pressing escape.)
esc:wq – to exit making changes, write my changes and then quit.
If I want to exit immediately without saving, I use:
esc:q! – stop making changes (“escape” ends the current operation, “:” means the command affect the “whole file”), and quit! (the “!” means don’t warn me about unsaved changes).
Next, Navigation – if the arrow keys aren’t mapped correctly then you’ll need to know the old school way of moving the cursor. The keys – h, j , k, and l – will move the cursor “left, up down, and right” respectively. You can also enter a number before the navigation keys and the cursor will move that number of times.
Inserting text? Press: esci
Press esc again when you’re finished editing, ready to enter a new command.
Press, shiftR to replace text (that is upper case R). This immediately hits one of those quirks, if you enter lower case “r”, Vi would let you change a single character and then go back to command mode. If you entered 5r then after you’d typed 5 characters, having overwritten 5 character in your file, Vi would go back to command mode. If you use, “R” then Vi will stay in “Replace” mode until you hit esc again.
Want to delete text? There are two commands to know. To erase a single character press escx. A single line is deleted with: escdd. Put a number in front of these commands and you’ll delete that number of characters or lines.
You simply hit escape for command mode and then type “/” followed by the string you want. Search forward by hitting “/” again. Even if you use a command like, delete or insert, Vi will continue to search for the last text you looked for until you enter a new search term. You can search backwards using “?”. There’s a lot more to search, but that’s enough to get you started.
“Gotcha” warning. You can undo your last change with esc:u. The gotcha is that if you’re actually using Vim (which is common on Linux and with Git) then redo is “Ctrl-r”. If you’re using true Vi, then redo is “:redo”.
Vi can undo the last 10 actions. And it will let you redo do them too.
Starting vi: vi filename
Exit and save: esc:wq
Quit without saving: escq!
Navigation: arrows keys, but also h, j, k and l – a number in front of the arrow will repeat the key that number of times. Eg 4k will move down for lines, 4 down arrow will do the same.
Insert text: esci
Replace text: escR
Erase a single (or a number of characters): escx or esc3x to delete three characters
Erase a line, or a number of lines: escdd or escdenter to delete a single line or esc6dd to delete six lines.
Join consecutive lines together: escJand esc8J The first examples joins the current line to the next. The second example joins the current line and the seven that follow it into one long line.
Search: esc/patternenter – searches forward for the text “pattern”
This will search forward for the next occurrence the “pattern”. Entering j ust ”/” will search for the next occurrence of the same text (so repeats the current search looking forward). Similarly, you can use: esc?patternenter to search backwards for the “pattern” text. Regardless of which direction you started searching in you can use “/” and “?” to search in either direction through the file after you’ve defined a search pattern.
One useful tip. You might find you’ve opened a file that you don’t have permission to change – maybe you’re not the file owner or don’t have permission to write files in the directory you’re in. Write the file to a different place, exit and fix the permissions before copying the temporary file to the correct place, for example:
Or write it to your home directory and fix the permissions. Of course if it’s a sensitive file you should be sure to immediately delete the temporary file afterwards. Be aware this fix would leave you as the owner of the new file.
That’s enough to get you started, and hopefully made some sense to you. We’ve only just begun to scratch the surface here, there’s a lot more (especially commands) to what Vi can do. It’s even possible to use Vi to drive how your command line editing works – but I’ll spare you that for another time.
Slàinte mhath, Nick.