Vi: Difference between revisions

1,200 bytes removed ,  4 years ago
 
(12 intermediate revisions by the same user not shown)
Line 1:
[[Category:Linux]]
__TOC__
<br />
 
= Basics =
Line 11 ⟶ 14:
set autoindent Automatic indentation in insert mode
set noautoindent Removes the automatic indentation feature in insert mode
seset 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 =
Line 39 ⟶ 55:
: r filename
 
* Opening Vi directly at a particular line
Running an external command within Vi
$vim filename +n5
You can execute any command within the Vi editor by typing the following command within the command mode of Vi:
:!commandname
 
Insert* Opening Vi at the outputend of a Linuxthe commandfile
You can insert the output of a command by giving the following command:
:r! :commandname
 
$vim + filename
The command is executed and the output is inserted from the position where the cursor is located.
 
* Writing the buffer to a new file
Opening Vi directly at a particular line
You can open the Vim editor with the cursor placed directly at the line you want, by giving the following command:
$vim filename +n
 
The number ‘n’ in the command is the line number.
 
Opening Vi at the end of the file
Adding the + sign and a space before the file name, opens the file and places the cursor at the last line.
This is useful if you have to work on large files.
$vim + filename
 
Writing the buffer to a new file
:w can be used to save the complete buffer, that is, the file being edited currently, under a new file name.
This can be used when you have made a lot of changes in the file, but then realise that you do not want to overwrite the original file.
To save the contents of the buffer, use the following command:
:w newname
 
You can now quit the original file by typing :q.
 
* Indenting the source code
In case you have typed dozens of lines of code without bothering about indentation, the quickest way to indent the code is to do the following;
Press the Esc key to go to command mode and then type the following:
gg=G
 
…whereWhere gg indicates the beginning of the file, = is for indenting and G indicates the end of the file.
 
* Repeating the last change
We can use the . (period) key to repeat the last change.
This helps to reduce the typing required when carrying out repetitive tasks.
 
* Undoing and redoing
Type u to undo the last change.
Type Ctrl-R to repeat a change that has been undone.
figure-2-a-code-snippet-with-indent
 
Jumping to matching braces
We can jump to the matching parenthesis (the curly brace or square bracket) by pressing the % key.
This is very useful when editing source code.
 
* Jumping to matching braces
Preventing auto-indent
% can jump to curly brace or square bracket
When text is pasted in Vim from an already indented source file, Vim will further apply its auto-indenting feature (if it is enabled) to the pasted text, which will have a cascading effect on the text.
 
To prevent auto-indenting, use the following command:
* 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.
After pasting, you can press <F2> again to go to the auto-indent mode.
 
= Advanced =
Line 99 ⟶ 93:
*Going to any line
:50
 
* Show Line Numbers:
:set nu
 
*In the command mode to get the same result:
Line 177 ⟶ 174:
*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}}