Home‎ > ‎unix/linux‎ > ‎

vim

(copying in case it gets moved)

# Matching # Matching
. any character except new line    
\s whitespace character \S non-whitespace character
\d digit \D non-digit
\x hex digit \X non-hex digit
\o octal digit \O non-octal digit
\h head of word character (a,b,c...z,A,B,C...Z and _) \H non-head of word character
\p printable character \P like \p, but excluding digits
\w word character \W non-word character
\a alphabetic character \A non-alphabetic character
\l lowercase character \L non-lowercase character
\u uppercase character \U non-uppercase character

Quantifier Description
* matches 0 or more of the preceding characters, ranges or metacharacters .* matches everything including empty line
\+ matches 1 or more of the preceding characters...
\= matches 0 or 1 more of the preceding characters...
\{n,m} matches from n to m of the preceding characters...
\{n} matches exactly n times of the preceding characters...
\{,m} matches at most m (from 0 to m) of the preceding characters...
\{n,} matches at least n of of the preceding characters...
\({data}\) takes {data} and puts it into register \1

Examples:

Simple Find and Replace: 

Search for text cool and replace it with groovy. Do this as many times as necessary for each line.
 was      is
 I am cool
cool cool cool
 I am groovy
groovy groovy groovy

:%s/cool/groovy/g


Add a carriage return before the word text. Only do this once per line.
was      is
 text is written here
 this is more text
 
 text is written here
 this is more
 text

:%s/text/[Ctrl+v][Return]text/

find any number matching a pattern

Search for all dates (ie: 5-20-2000), and delete: 
was      is
 101
cool
1-1-1
34-2341-12
done
 101
cool
done


:%s/\d\+-\d\+-\d\+//g

find and replace with regex "word 123" with "word    123" and use grouping brackets

was      is
# something
number 5
fiveby5
 # something
number     5
fiveby5
:%s/\(\w\) \(\d\)/\1\t\2/

Find some non-printed stuff

Delete all empty lines: 
was      is
# something

more text here
 # something
more text here

:g/^$/d

Delete all empty lines that contain only white spaces
(same as above with spaces)  
:g/^\S$/d

Find, copy, and paste

find some text and store it in register $1, then paste it later

was      is
interface GigabitEthernet1/0/1
 description switch
GigabitEthernet1/0/1, description switch

:%s/interface \(\S\+\)\n / \1, / 


Delete lines containing match

any line with the word match should be deleted
:g/match/d




Scripts

if you create a text file (in this example called "xl.vim", and in this example putting it in your home directory), and in that text file put a bunch of search and replace statements that you want to do (like such): 
:%s/^!#.*\n//g
:%s/\t//g
:%s/<t>/  /g
:%s/<p>/\r/g

then in your current file that you want to run this script against, call the script by the following command: 
:source ~/xl.vim
  


Keyboard Cheat Sheet:

Quick list of what each key does:


also note:
[Ctrl-R]: Redo changes which were undone (undo the undos)

Configuration Settings: 

VIMRC: 

Config files and good standards for them. 

References:

How-Tos:

Links: