vi or vim (vi improved) is a powerful screen based text editor.
Entering the editor: #vi [filename]
Exiting the editor:
- #:q - quit. If there are unsaved changes vi will not quit and a warning message will be displayed.
- #:q! - force quit without saving changes
- #:wq [filename] - quit and save
Editor modes:
- Command mode - default when vi starts
- Insert mode
- enter insert mode: a, i, o, s
- exit insert mode: Esc, ^[
- enter insert mode: a, i, o, s
Command mode:
- :ab string strings
- Abbreviation. If a word is typed in VI corresponding to string1, the editor automatically inserts the corresponding words. For example, the abbreviation ":ab usa United States of America" would insert the words, "United States of America" whenever the word "usa" is typed in.
- :map keys new_seq
- Mapping. This lets you map a key or a sequence of keys to another key or a sequence of keys.
- :q
- Quit VI. If there have been changes made, the editor will issue a warning message.
- :q!
- Quit VI without saving changes.
- :s/pattern/to_pattern/options
- Substitute. This substitutes the specified pattern with the string in the to_pattern. Without options, it only substitutes the first occurence of the pattern. If a 'g' is specified, then all occurences are substituted. For example, the command ":1,$s/Dwayne/Dwight/g" substitutes all occurences of "Dwayne" to "Dwight".
- :set [all]
- Sets some customizing options to VI and EX. The ":set all" command gives all the possible options. (See the section on customizing VI for some options.)
- :una string
- Removes the abbreviation previously defined by ":ab".
- :unm keys
- Removes the remove mapping defined by ":map".
- :vi filename
- Starts editing a new file. If changes have not been saved, the editor will give you a warning.
- :w
- Write out the current file.
- :w filename
- Write the buffer to the filename specified.
- :w >> filename
- Append the contents of the buffer to the filename.
- :wq
- Write the buffer and quit.
Cutting and Pasting/Deleting text
- "
- Specify a buffer to be used any of the commands using buffers. Follow the " with a letter or a number, which corresponds to a buffer.
- D
- Delete to the end of the line from the current cursor position.
- P
- Paste the specified buffer before the current cursor position or line. If no buffer is specified (with the " command.) then 'P' uses the general buffer.
- X
- Delete the character before the cursor.
- Y
- Yank (copy) the current line into the specified buffer. If no buffer is specified, then the general buffer is used.
- d
- Delete until where. [dd] deletes the current line. A count [nd or dn] deletes that many lines after the current line, including the current line. Whatever is deleted is placed into the buffer specified with the " command. If no buffer is specified, then the general buffer is used.
- p
- Paste the specified buffer after the current cursor position or line. If no buffer is specified (with the " command.) then 'p' uses the general buffer.
- x
- Delete character under the cursor. A count tells how many characters to delete. The characters will be deleted after the cursor.
- y
- Yank (copy) until , putting the result into a buffer. [yy] yanks the current line. a count [ny or yn] yanks that many lines after the current line, including the current line. The buffer can be specified with the " command. If no buffer is specified, then the general buffer is used.
- v
- Visual selection. select the text from the current position moving the cursor. selected text will be highlighted.
- d or [Delete] or "ad or "a[delete] - deletes the selection, copies selection to buffer
- y - yanks the selection
- "ay - yanks the selection into buffer "a"
- gg - go to start of file
- G - go to end of file
- :n - go to line n
- n| - go to column n
- Examples:
- "ay5 - yanks (copies) 5+1 lines into "a" buffer. Do not use numeric buffer names as they will be interpreted as number of lines to yank.
- "Ay5 - yanks (copies) 5+1 lines into "a" buffer in append mode. Append mode is given by a caputal buffer letter.
- "ap - paste the content of the "a" buffer after the cursor position.
- "bd3 - deletes 3+1 lines and copies the deleted content into "b" buffer.
Insert mode:
Everything that is written on the keyboard will be inserted into the file. [Shift]+[Insert] works here. [Esc] leaves insert mode.