Difference between revisions of "Training/Linux - vi editor"
m |
m (→Navigation) |
||
(5 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== Introduction == | == Introduction == | ||
− | Linux has a large number of editors available to it | + | Linux has a large number of editors available to it that 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. | The vi editor is not intuitive, but once you get to know it, '''vi''' becomes a very powerful application. | ||
Line 32: | Line 32: | ||
=== Vi : start typing (a A i I o O) === | === 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. | + | 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 it before the current character and I will insert it 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) === | === 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 | + | 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 keypress, and will return you immediately to '''command mode'''. |
=== Vi : undo and repeat (u .) === | === Vi : undo and repeat (u .) === | ||
− | When in '''command mode''', you can undo your mistakes with u. You can do your mistakes twice | + | When in '''command mode''', you can undo your mistakes with u. You can do your mistakes twice. (in other words, the. will repeat your last command). |
=== Vi : cut, copy and paste a line (dd yy p P) === | === Vi : cut, copy and paste a line (dd yy p P) === | ||
Line 48: | Line 48: | ||
=== Vi : cut, copy and paste lines (3dd 2yy) === | === 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 | + | 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 lines yanked". |
=== Vi : start and end of a line (0 or ^ and $) === | === 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. | + | 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 the start and end of the current line. |
=== Vi : join two lines (J) and more === | === Vi : join two lines (J) and more === | ||
Line 60: | Line 60: | ||
=== Vi : words (w b) === | === 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). | + | 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! ) === | === Vi : save (or not) and exit (:w :q :q! ) === | ||
Line 89: | Line 89: | ||
[https://www.tutorialspoint.com/unix/unix-vi-editor.htm The vi Editor Tutorial] | [https://www.tutorialspoint.com/unix/unix-vi-editor.htm The vi Editor Tutorial] | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | [[Main Page]] / [[FurtherTopics/FurtherTopics #Programming| Back to Further Topics]] |
Latest revision as of 11:36, 16 November 2022
Contents
- 1 Introduction
- 2 Commands
- 2.1 Vi : command mode and insert mode
- 2.2 Vi : start typing (a A i I o O)
- 2.3 Vi : replace and delete a character (r x X)
- 2.4 Vi : undo and repeat (u .)
- 2.5 Vi : cut, copy and paste a line (dd yy p P)
- 2.6 Vi : cut, copy and paste lines (3dd 2yy)
- 2.7 Vi : start and end of a line (0 or ^ and $)
- 2.8 Vi : join two lines (J) and more
- 2.9 Vi : words (w b)
- 2.10 Vi : save (or not) and exit (:w :q :q! )
- 2.11 Vi : Searching (/ ?)
- 2.12 Vi : replace all ( :1,$ s/foo/bar/g )
- 2.13 Vi : reading files (:r :r !cmd)
- 2.14 Vi : text buffers
- 2.15 Vi : abbreviations
- 3 Further Information
Introduction
Linux has a large number of editors available to it that 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:
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 it before the current character and I will insert it 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 keypress, 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. (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 lines 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 the 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