Vi: Difference between revisions

2,546 bytes added ,  4 years ago
 
(20 intermediate revisions by the same user not shown)
Line 1:
[[Category:Linux]]
__TOC__
<br />
 
= Basics =
*Modes:
i insert
Crtl + c or Esc exit insert mode
ZZ or :x or :wr save & exit
 
*EX Commands:
i = insert
set number Displays line numbers
crtl + c = exit insert mode
set nonumber Removes line numbers
ZZ or :x = save & exit
set autoindent Automatic indentation in insert mode
set noautoindent Removes the automatic indentation feature in insert mode
set tabstop=3 Sets the number of spaces by which the tab indents during editing
syntax on Turns on highlighting of syntax
syntax off Turns off highlighting of syntax
set shiftwidth=4 The size of the indent, measured in spaces
set list List all Special characters except for white space.
 
*The .vimrc file
The EX commands (those that you key in after typing : in the Vi editor), which you want to execute whenever you start the Vi editor, can be saved in the .vimrc file in your home directory.
 
* Pages:
Control + F Next Page
Control + B Previous Page
 
== Searching ==
 
/string Search for "string"
Enter Highlight the first keyword match
N Scroll through next keyword matches
Shift + G Go to bottom of the file
Shift + N Search from Bottom to top for keyword matches
 
= Intermediate =
 
* Abbreviations can be set, which Vi will expand into full text whenever they are typed in edit mode
:ab abbr full text
:ab aman Amandeep Singh
 
The abbreviation can be disabled by:
:unab abbr
:unab aman
 
*Encryption in Vi
To encrypt any file in the Vi editor, type the following in command mode and press the Enter key,then Enter a password twice.:
:X
 
To decrypt the encrypted file, open the file and type below command and hit the Enter key twice; then save and exit the Vi editor by typing :wq, the file will be saved in the decrypted format:
:X
 
* Inserting the contents of an existing file
: r filename
 
* Opening Vi directly at a particular line
vim filename +5
 
* Opening Vi at the end of the file
 
vim + filename
 
* Writing the buffer to a new file
:w newname
 
You can now quit the original file by typing :q.
 
* Indenting the source code
gg=G
 
Where gg indicates the beginning of the file, = is for indenting and G indicates the end of the file.
 
* Repeating the last change
. (period) key to repeat the last change.
 
* Undoing and redoing
u undo the last change.
Ctrl-R repeat a change that has been undone.
 
 
* Jumping to matching braces
% can jump to curly brace or square bracket
 
* Preventing auto-indent
:set pastetoggle=<F2>
 
You can then press <F2> in the insert mode when you are ready to paste.
After pasting, you can press <F2> again to go to the auto-indent mode.
 
= Advanced =
Line 12 ⟶ 93:
*Going to any line
:50
 
* Show Line Numbers:
:set nu
 
*In the command mode to get the same result:
Line 19 ⟶ 103:
 
*Incrementing or decrementing a number
 
Ctrl-a
Ctrl-x
 
*Changing the case of letters
 
~ To toggle the case of the character below which the cursor is positioned
gUU To change the case of the current line to upper case
Line 39 ⟶ 121:
;Executing a filter command
 
# ‘!’ symbol is used within the command line of Vi editor to execute an external program.
# The line number 1 and the $ symbol in the above command can be replaced with any portion of the file with the numbers of the starting line and ending line of the particular portion.
 
*To sort the file from the first line to the last line
Line 57 ⟶ 139:
:10,20 ! tr a-z A-Z
 
*Writing a portion of file, lines from 20 to 50 to a new file beingnamed editednewfile.txt:
:20,50 w > newfile.txt
 
As a result, the lines from 20 to 50 are written in a new file named newfile.txt.
The above facility of processing parts of the text and substituting the original content with the output makes the Vi editor very powerful.
 
;Inserting the output of a command executed within the Vi editor
Line 70 ⟶ 149:
*Less known is the fact that you can insert the output of the command given within the Vi editor by placing a ‘.’ (period) before the exclamation mark.
:.! date
:r! date
 
*Cursor movements
 
H The cursor is positioned at the first line of the screen
M The cursor is positioned at the middle line of the screen
L The cursor is positioned at the last line of the screen
 
*Scrolling through a file
Ctrl-f Scroll down by one screen
Ctrl-b Scroll up by one screen
Ctrl-u Scroll up by half a screen
Ctlr-d Scroll down by half a screen
zz Scroll the screen so that the current line appears at the middle of the screen, very useful for viewing the block of code associated with the current line.
 
*Editing a file opened without sudo
Line 89 ⟶ 169:
 
*Powerful delete commands
di( Deletes all characters within the parentheses
di” Deletes all characters within the quotes
 
*Recovering a file, after a crash, from the swap file of the file being edited:
$vi -r filename
 
 
 
<br />
;References
<references/>
<br />
<br />
<br />
 
{{DISQUS}}