Intro to the Vi text editor

            Vi is a popular text editor that lets the user edit files within the terminal on Linux and Unix based machines. To start editing a file in the terminal, type the command vi <filename> in the terminal.

            [eansley@cycle3 ~/Documents]$ vi Example.txt

This will pull up an editor within the terminal. The default editor is command mode. There is also an insert mode and a visual mode. Command mode and visual mode interpret keyboard actions as commands, while insert mode takes are keystrokes and inserts them as text.

Command Mode

            To move around in command mode, users can either use the arrow keys or they can use the keys ÒhÓ, ÒjÓ, ÒkÓ, and ÒlÓ as left, down, up, and right, respectively. There are also many commands that make moving around in this editor easier. The command ÒggÓ brings the user to the beginning of the file, and ÒGÓ brings the cursor to the end of the file. Any specific line within the file can be reached with the line number alone with G. So, typing Ò7GÓ would place the cursor on the end of the 7th line of the file. The beginning of the line can be reached by typing Ò0Ó, while the end of the line can be reached with the Ò$Ó key. To jump to the next word, use ÒwÓ or ÒWÓ, the latter of which ignores punctuation.  To get to the beginning or the end of a word, the commands ÒbÓ and ÒeÓ are useful, while ÒBÓ and ÒEÓ do have the same effect while ignoring punctuation. Instead of moving ahead one word at a time, Vi also lets you type a number before the command that specifies how far to move ahead. For example, Ò3wÓ would move the cursor ahead three words. This works for any movement command, so Ò3lÓ would move 3 spaces to the left, and 3b would move to the beginning of the word that was three words before the cursor. To search for characters within the file, users should type ÒfÓ and then the character. The command Ò3foÓ would find the third occurrence of ÔoÕ from the placement of the cursor. To find the next and previous occurrences of a word that is under the cursor, the user can use the Ò*Ó and Ò#Ó commands respectively. A general search can be done with the Ò/Ó command followed by the text that is being searched for. The command Ò/textÓ would search the file for the word ÔtextÕ. The next and previous occurrences of the word can be found using ÒnÓ and ÒNÓ. Vi also allows for parenthesis matching. This command is executed with the Ò%Ó character while the cursor is placed on the parenthesis the user wishes to match. Deleting text can also be accomplished in command mode by pressing ÒxÓ to remove the character directly under the cursor, and ÒXÓ to remove the character that is immediately to the left of the cursor. Deleting more than one character can be done with the ÒdÓ command. This command can be combined with other movement commands such as ÒwÓ. The command ÒdwÓ would therefore delete the first word to the right of the cursor. The ÒdÓ command not only deletes the text, but also copies the content, so the user can paste it somewhere else using the ÒpÓ command. To replace any single character under the cursor without switching to insert mode, users should type ÒrÓ and then the new character. To repeat the previous command, they should use the Ò.Ó symbol. For instance, deleting the first five words in a line can be done with the commands Òd2wÓ to delete the first one, then Ò. . . .Ó to delete the next four. Within the command mode, a user can also undo an action with the ÒuÓ command.

Insert Mode

            To switch into insert mode, type ÒiÓ; to exit, hit the esc key to go back to command mode. While in insert mode, text can be typed like any regular text editor. The cursor moves by the movement keys on the keyboard, which could be tedious in long files. Therefore, it is usually best to place the cursor in command mode, insert text in insert mode, then exit back to command mode to replace the cursor.

Combining Command Mode and Insert Mode

            Combining modes lets the user use many shortcuts. For example, instead of typing 30 dashes, the user could type Ò30Ó in command mode, switch to insert mode but typing ÒiÓ, typing a Ò-Ó, then exiting insert mode but pressing the esc key (30i- ESC). This would insert the dash symbol thirty times at the place of the cursor. Creating new lines using the ÒoÓ or ÒOÓ commands in command mode will place a new line below or above the line the cursor is on, and then switch into insert mode.

Visual Mode

            The third mode, visual mode, lets the user select (and highlight) text within the terminal using the Vi movement keys (h, j, k, l) before deciding what to do with them. The commands in visual mode are the same as in command mode.

Saving and Quitting

            Most importantly, the command Ò:wÓ will save the file, and Ò:qÓ will quit the file and bring the user back out to the terminal. These commands can also be combined into one command as Ò:wqÓ which saves and then exits to the terminal. To quit without saving, the command Òq!Ó should be used.

 

            Vi is a powerful in-terminal editor. To learn more about any of these commands, or to learn new commands, vi has a built in help file which can be found by the Ò:helpÓ command.


 

Resources

http://www.openvim.com/tutorial.html

http://blog.interlinked.org/tutorials/vim_tutorial.html

http://heather.cs.ucdavis.edu/~matloff/UnixAndC/Editors/ViIntro.html

Author: Emily Ansley

CSC 210W