Vi: Difference between revisions

From Network Security Wiki
Content added Content deleted
Line 37: Line 37:
;Executing a filter command
;Executing a filter command


# ‘!’ symbol is used within the command line of Vi editor to execute an external program.
# ‘!’ 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.
# 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
*To sort the file from the first line to the last line
Line 55: Line 55:
:10,20 ! tr a-z A-Z
:10,20 ! tr a-z A-Z


*Writing a portion of a file being edited
*Writing a portion of file, lines from 20 to 50 to a new file named newfile.txt:
:20,50 w > newfile.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
;Inserting the output of a command executed within the Vi editor
Line 71: Line 68:
*Cursor movements
*Cursor movements


H The cursor is positioned at the first line of the screen
H The cursor is positioned at the first line of the screen
M The cursor is positioned at the middle 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
L The cursor is positioned at the last line of the screen


*Scrolling through a file
*Scrolling through a file
Ctrl-f Scroll down by one screen
Ctrl-f Scroll down by one screen
Ctrl-b Scroll up by one screen
Ctrl-b Scroll up by one screen
Ctrl-u Scroll up by half a screen
Ctrl-u Scroll up by half a screen
Ctlr-d Scroll down 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.
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
*Editing a file opened without sudo
Line 87: Line 84:


*Powerful delete commands
*Powerful delete commands
di( Deletes all characters within the parentheses
di( Deletes all characters within the parentheses
di” Deletes all characters within the quotes
di” Deletes all characters within the quotes


*Recovering a file, after a crash, from the swap file of the file being edited:
*Recovering a file, after a crash, from the swap file of the file being edited:

Revision as of 19:35, 20 August 2017

Basics

i = insert
crtl + c = exit insert mode
ZZ or :x = save & exit


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
  • 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