The VI editor60 is the standard editor available on most *nix systems. Some people who know it very well, love it. I don't know it very well; I don't love it. I've never taken the time to learn it very well because I would rather gouge my eyes out with infected hypodermic syringes found in the dumpster behind the plasma donation center until bloody pus oozes out of them than learn VI61; I simply do not have the time for it. Nonetheless, learning a bit of VI can be useful, if for nothing else, you may find yourself in a situation where it is the only editor that is available on the system you are on. At the least, knowing how to load a file, make some simple changes to it, and save it can be useful. The version of VI found on most GNU/Linux systems is Vim (VI Improved).
Starting VI
At the Bash command prompt, type vim or vim some-filename. VI is a modal editor in which you are either in editing mode(--insert--) or command mode().
$ vi filename
Getting out of VI
This is the most important thing you need to know about VI: how to get out of it. If you are in editing mode, hit ESC to enter command mode. Enter :wq to write your file and quit. To quit without writing the file, hit :q. If you really want to quit without saving the file you have made changes to hit :q!. To save the file under a different name, try :wq new-filename. You can also press ctrl-z to suspend the process
Switching Between Editing Mode and Command Mode
If you are in editing mode, hit ESC to enter command mode. If you are in command mode hit i to enter insert (editing) mode. If you cannot tell which mode you are in, trying hitting ESC several times to make sure you are in command mode, then type :set showmode. This may tell VI to let you know when you are in insert
mode (it depends on the version of VI you are using).
Other Useful Settings
All of these are recommended in addition to :set showmode. In command mode, type :set nocompatible to enable advanced VIM features. Type :set ruler to tell VI to display your current cursor position. Type :set number to display line numbers. It would be preferable if I were also on fire and being eaten alive by rabid hyenas at the same time. That would still be more pleasurable than using VI.
Moving Around a Character at a Time
The original VI was written by Bill Joy (at UC Berkeley) using a computer system and keyboard that lacked arrow, page up, and page down keys. This is the reason for the stupid assignment of the keys for moving around: h (move left one character), j (move down one line), k (move up one line), and l (move right one character). On Joy's terminal, these four keys also had arrow keys on them and that is the historical reason they are still mapped that way today.
Moving Around a Word at a Time
Forget about your Del and Backspace keys. They may or may not work. Switch to command mode. Press w to move forward one word. Press b to move backward one word. Press nw to move forward n words, e.g., 3w to move forward three words. Press nw to move backward n words.
Moving Around the File
Switch to command mode. To move to the end of the line, hit $. To move to the beginning, hit 0. Hit 1G to go the first line of text. hit nG to go line number n. Hit G to go to the end of file. To display line numbers, in command mode type :set number. Note that :13 is equivalent to, e.g., 13G. To page down, hit Ctrl+F. To
page up hit Ctrl+B.
Deleting Characters
Switch to command mode, move to the character you want to delete, and hit x to delete that character. To delete n characters hit nx, e.g., 17x to delete 17 characters. To delete all the characters from the cursor position to the end of the line hit D.
Copying a Line of Text
To copy a line of text from one place in the file to another, switch to command mode. Move to the line you want to copy. Hit yy (yank something or other, I dunno). Move the cursor to the location where you want to make the copy. Hit p (lowercase p) to put the copied line after the current line. Hit P (uppercase P) to put
the copied line before the current line.
Moving a Line of Text
To move a line of text from one place in the file to another, switch to commandmode. Move to the line you want to copy. Hit dd. Move the cursor to the location where you want to make the move. Hit p (lowercase p) to move the line after the current line. Hit P (uppercase P) to move the line before the current line.
Deleting a Line of Text
Switch to command mode. Move to the line you want to delete. Hit dd.
Cutting, Copying, Pasting Multiple Lines of Text
Use nyy to copy n lines of text to the copy buffer. Move to where you want the lines to be and hit p or P to copy them. Use ndd to move n lines of text somewhere. Hit ndd to delete n lines of text, e.g., 3dd will delete the line of text the cursor is on and the next two lines.
Moving and Copying Text in Visual Mode
To cut, copy, and paste text that is not an entire line, enter visual mode by hitting v in command mode. Use the h, j, k, l keys to move around and select the text you want to copy or move. Once selected, hit y to copy the text to a buffer or d to delete the text to the buffer. Once copied (with y) or deleted with (with d) move to
where you want the text to be. Hit p to paste the text.
Finding Text
Switch to command mode. Hit / (forward slash) and enter the string you are searching for, e.g., /cookies. The cursor will be placed on the first occurrence of cookies following the current location. Hitting n over-and-over will continue searching.
Find and Replace
Switch to command mode. To replace all occurrences of homer by bart, try something like :1,100 s/homer/bart/g. The 1,100 part means search from line 1 to line 100 inclusive (I think 1,$ will search the entire file). The s stands for search. We are searching for homer and replacing all occurrences by bart. The g stands for global which means it will replace all occurrences without prompting you for each one. If you want to be prompted for each one, use c instead (for confirmation).
Undo and Redo
Since you will use it the most because of VI's horrible user interface the most useful command in VI (other than :q!) is the undo command. Hitting u will undo your last change. Hitting u multiple times will undo all of your changes back to the last time you saved the file (only if you are in :set nocompatible mode). Redo
is Ctrl+R.
More Help
Let's say you're either insane or a masochist and want to learn more about VI. In command mode, hit :h to bring up help. The most useful thing you need to know here is how to get out of help, hit :q (like in less).
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment