Vi: Difference between revisions

From Network Security Wiki
Content added Content deleted
Line 39: Line 39:
: r filename
: r filename


* Opening Vi directly at a particular line
Running an external command within Vi
vim filename +5
You can execute any command within the Vi editor by typing the following command within the command mode of Vi:
:!commandname


Insert the output of a Linux command
* Opening Vi at the end of the file
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
:w newname


You can now quit the original file by typing :q.
You can now quit the original file by typing :q.


Indenting the source code
* 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
gg=G


…where gg indicates the beginning of the file, = is for indenting and G indicates the end of the file.
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
Repeating the last change
We can use the . (period) key to repeat the last change.
u undo the last change.
Ctrl-R repeat a change that has been undone.
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
* Jumping to matching braces
% can jump to curly brace or square bracket
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.


Preventing auto-indent
* Preventing auto-indent
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:
:set pastetoggle=<F2>
: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.
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 =
= Advanced =

Revision as of 22:23, 20 August 2017

Basics

  • Modes:
i                   insert
Crtl + c or Esc     exit insert mode
ZZ or :x or :wr     save & exit
  • EX Commands:
set number 		Displays line numbers
set nonumber 		Removes line numbers
set autoindent  	Automatic indentation in insert mode
set noautoindent	Removes the automatic indentation feature in insert mode
se 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
  • 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.

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

  • Going to any line
:50
  • In the command mode to get the same result:
50G

Typing just G in the command mode takes the cursor to the last line of the file and typing 1G takes the cursor to the beginning of the file.

  • 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
guu       To change the case of the current line to lower case
g~~       To toggle the case of the current line
g~$       To toggle the case of all characters from the cursor position to the end of the line
  • Sorting within Vi
:sort
  • To sort lines and remove duplicate lines
:sort u
Executing a filter command
  1. ‘!’ symbol is used within the command line of Vi editor to execute an external program.
  2. 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
:1,$!sort
  • The -u option with sort will keep only the unique lines and remove the duplicate lines.
:1,$!sort -u


  • To sort the lines starting from the contents of line number 5 to 15, give the following command:
:5,15! Sort
Using tr within Vi
  • The following command will convert all the letters in the line numbers 10 to 20 to upper case:
:10,20 ! tr a-z A-Z
  • Writing a portion of file, lines from 20 to 50 to a new file named newfile.txt:
:20,50 w > newfile.txt
Inserting the output of a command executed within the Vi editor
  • Most users are familiar with the method to execute a shell command within the Vi editor.
:! ls
  • 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

Let’s assume that you are editing a file which requires root access, instead of saving the file by taking many steps, you can save it straight away by using a combination of the tee and sudo commands:

:w !sudo tee %1
  • 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