Thursday, July 28, 2016

Useful VI commands

===================================================================================
Regular Expressions
===================================================================================
. (dot)    Any single character except newline
*            zero or more occurrences of any character
[...]        Any single character specified in the set
[^...]       Any single character not specified in the set
^            Beginning of the line
$            End of line
\<           Beginning of word
\>           End of word
=======================================================================================
1. Add something at the beginning of each line 

:%s/^/@

2. Add something at the end of each line 

:%s/$/.sql

3. Delete blank lines
:g/^$/d
:g/^ *$/d

4.remove all leading white space(s) from all lines
:1,$ s/^\s*//g

5.Remove the ^M characters at the end of all lines (Dos to Unix Conversion)

:%s/^V^M//g  ====The ^v is a CONTROL-V character and ^m is a CONTROL-M. When you type this, it will look like this: 
:%s/^M//g:%s/\r//g : Delete DOS returns ^M " 
Is your Text File jumbled onto one line? use following 
:%s/\r/\r/g : Turn DOS returns ^M into real returns

6. Duplicate every Line

:g/^/t.

7. Delete Duplicate Lines

:%s/^\(.*\)\n\1$/\1/        : delete duplicate lines
:%s/^\(.*\)\(\n\1\)\+$/\1/  : delete multiple duplicate lines [N]

8. Delete blank lines

:v/\S/d                     : Delete empty/blank lines
:g/^\s*$/d                  : delete all blank lines

9. Read lines from external command
:r!ls -ltr

10. Being able to determine the line number of the current line or the total number of 
lines in the file

:.= returns line number of current line at bottom of screen
:=  returns the total number of lines at bottom of screen
^g  provides the current line number, along with the total number of lines,in the file at the 
 bottom of the screen

No comments: