Training/Linux - vi editor

From HPC
Jump to: navigation , search

Introduction

Linux has a large number of editors available to it which will match their preferences. The vi editor is installed on almost every Linux. Linux will very often install vim (vi improved) which is similar. Every command line user should know vi(m), because it is an easy tool to solve problems.

The vi editor is not intuitive, but once you get to know it, vi becomes a very powerful application.

Starting Vi

Below using the command line are some examples:



[username@viper ~]$ vi        (just start vi)
[username@viper ~]$ vi newfile.c    (start vi with a new file called newfile.c)
[username@viper ~]$ vi existing.c   (start vi with an existing file called existing.c)
[username@viper ~]$ vi existing1.c existing2.c  (start vi with a multiple files listed)

Below is how vi appears on the screen of a terminal connected to Viper:


Vi1.jpg

Commands

Vi : command mode and insert mode

The vi editor starts in command mode. In command mode, you can type commands. Some commands will bring you into insert mode. In insert mode, you can type text. The escape key will return you to command mode.

Vi : start typing (a A i I o O)

The difference between a A i I o and O is the location where you can start typing. a will append after the current character and A will append at the end of the line. i will insert before the current character and I will insert at the beginning of the line. o will put you in a new line after the current line and O will put you in a new line before the current line.

Vi : replace and delete a character (r x X)

When in command mode (it doesn't hurt to hit the escape key more than once) you can use the x key to delete the current character. The big X key (or shift x) will delete the character left of the cursor. Also when in command mode, you can use the r key to replace one single character. The r key will bring you in insert mode for just one key press, and will return you immediately to command mode.

Vi : undo and repeat (u .)

When in command mode, you can undo your mistakes with u. You can do your mistakes twice with . (in other words, the . will repeat your last command).

Vi : cut, copy and paste a line (dd yy p P)

When in command mode, dd will cut the current line. yy will copy the current line. You can paste the last copied or cut line after (p) or before (P) the current line.

Vi : cut, copy and paste lines (3dd 2yy)

When in command mode, before typing dd or yy, you can type a number to repeat the command a number of times. Thus, 5dd will cut 5 lines and 4yy will copy (yank) 4 lines. That last one will be noted by vi in the bottom left corner as "4 line yanked".

Vi : start and end of a line (0 or ^ and $)

When in command mode, the 0 and the caret ^ will bring you to the start of the current line, whereas the $ will put the cursor at the end of the current line. You can add 0 and $ to the d command, d0 will delete every character between the current character and the start of the line. Likewise d$ will delete everything from the current character till the end of the line. Similarly y0 and y$ will yank till start and end of the current line.

Vi : join two lines (J) and more

When in command mode, pressing J will append the next line to the current line. With yyp you duplicate a line and with ddp you switch two lines.

Vi : words (w b)

When in command mode, w will jump to the next word and b will move to the previous word. w and b can also be combined with d and y to copy and cut words (dw db yw yb).

Vi : save (or not) and exit (:w :q :q! )

Pressing the colon : will allow you to give instructions to vi (technically speaking, typing the colon will open the ex editor). :w will write (save) the file, :q will quit an unchanged file without saving, and :q! will quit vi discarding any changes. :wq will save and quit and is the same as typing ZZ in command mode.

Vi : Searching (/ ?)

When in command mode typing / will allow you to search in vi for strings (can be a regular expression). Typing /foo will do a forward search for the string foo and typing ?bar will do a backward search for bar.

Vi : replace all ( :1,$ s/foo/bar/g )

To replace all occurrences of the string foo with bar, first switch to ex mode with : . Then tell vi which lines to use, for example 1,$ will do the replace all from the first to the last line. You can write 1,5 to only process the first five lines. The s/foo/bar/g will replace all occurrences of foo with bar.

Vi : reading files (:r :r !cmd)

When in command mode, :r foo will read the file named foo, :r !foo will execute the command foo. The result will be put at the current location. Thus :r !ls will put a listing of the current directory in your text file.

Vi : text buffers

There are 36 buffers in vi to store text. You can use them with the " character.

Vi : abbreviations

With :ab you can put abbreviations in vi. Use :una to undo the abbreviation.

Further Information

The vi Editor Tutorial


Navigation