Using vi/vim Editorsby: G.E. Ozz Nixon Jr.
Published: June 2009
Copyright 2009 by Friends of FPC
Linux is an awesome world, years of shortcuts, pipes, macros all combined into a simple shell environment. Well, until you actually want to do some serious editing and you are on a tightly secured machines that only has vi or vim. I occasionally find myself in this environment, and I have always been able to make due with the basic keyboard keystrokes, that was until I found myself on a proprietary Mac wireless keyboard. No insert, no delete, no home, end or page up and page down... well, before I blew a blood vessel, I had to hit the man pages on the Ubuntu web site.
Very Basic Stuff
The vi editor starts in a read-only mode, where you can see the file content, navigate with your arrows (if your keyboard has them). However, we are going to assume you have never used the vi editor before.
$ vi myfile.001
This opens up vi on a [new file] as the status bar shows on your terminal. To abort, press [ESC], :q!
[ESC] (Escape Key), aborts your current action and tells the editor you are about to give it a new command. In this case, ":" to put the cursor on the status bar and wait for a command. "q" is quit, and following it with "!" means do not prompt me for anything (like, 'you have not saved recent changes'). Pressing enter will return you back to the shell prompt. Doing an ls, you will see that vi did not create 'myfile.001', as you did a "q!".
Now do the same vi command to open 'myfile.001' and this time, you will do [ESC], :wq [ENTER]. This time, if you do an ls, you will see that you actually created a file! "w"rite to disk, then "q"uit. Most vi commands can be "stacked" like this, so you do not have to do [ESC], :w [ENTER], [ESC], :q [ENTER], simply merge the commands in the order you wish, :wq.
When you find yourself on a machine with some or all of the navigation buttons missing, you will be glad if you memorized these (or most of them).
h Move Left j Move Down k Move Up l Move Right
Just these four keys will help you, and a simple rule of thumb is they are h,j,k and l - four in a row on your keyboard. Of course, getting to the end of a long line will eventually get frustrating, so the next set of keystrokes help you 'jump' around faster.
$ End of Line 0 Start of Line ^ First Character
The dollar sign jumps to the last position of a line, zero jumps to the first position, while carrot jumps to the first non-whitespace in the line (This is useful when working with indented or formatted text). These keystrokes jump around whole lines, even if the line is word-wrapped. However, you will find yourself sometimes wanting to jump from word to word, especially when you are working with word-wrapped lines of text.
w Next Word Start W Next Word after Blank e Next Word End E Next Word before Blank b Previous Word Start B Previous Word before Blank
The way I remember these 6 is the word "web", (Word) (End) (Begin), after a few times of seriously editing source files, or web pages, these become second nature. Next are ways to jump around whole sentences and paragraphs:
( Current Sentence Start ) Current Sentence End
Current Paragraph StartCurrent Paragraph End 1G Move to start of File G Move to end of File nG Move to nth line of File :n Move to nth line of File H Move to start of Screen M Move to middle of Screen L Move to last of Screen % Move to matching ( ),
, [ ] fc Move forward to matching c Fc Move back to matching c
These are popular related words: