Tag Archives: Vim - Page 2

My little regular expression “cheat sheet”

Regular expressions kan solve a whole lot of problems but remembering how to build them can be a real pain. This is my own “Cheat sheet” (tested in Vim v7.2)

. = match any wildcard character (must be one and one only)
c.t will match ‘cat’ but not ‘caaaat’ or ‘ct’

+= match 1 or more wildcard characters
ca+t will match ‘cat’, ‘caaat’ but not ‘ct’

* = match 0 or more of the preceeding character
ca*t will match ‘cat’, ‘caaaat’ and ‘ct’

? = match 0 or 1 of the preceding character
ca?t will match ‘cat’ and ‘ct’ but not ‘caaaat’

[] = match a range
[a-z] match all characters from a to z
[blke] match all of the letters b, l, k and e
[^blke] matches all characters EXCEPT b, l, k, and e

| = or character
cat|dog match either the full word ‘cat’ OR ‘dog’

^ = matches beginning of the string
^dog matches the first appearance of ‘dog’ in ‘dog dog dog’. Match is made on each line

$ = matches end of the string
dog$ matches all lines that end with ‘dog’. NOTE Whitespace is also a character!

\ = normal escape character
In Vim the following needs to be escaped to work: +, ?, | and {
A complete list can be found in Vim help ‘:help magic’

{n} = match preceding characters n times
[1-9]{6}-[1-9]{4} matches 771122-5748

{n,m} = match preceding characters between n and m times
‘ca{2,3}t’ will find ‘caat’ and ‘caaat’ but NOT ‘cat’ or ‘caaaat’

My VIM command collection

In my work I often need to edit files in a terminal environment. My choice of editor in these environments is VIM. Here are a bunch of commands in command mode that I use.

Start vim

vim <filename>

Start vim with the output of another command/program

<command/program> | vim -

Show vim help

:help

Save file

 :write #or 'w' for short

Quit vim

 :quit #or 'q' for short

Save AND quit

:wq

Force save (you can also force edit and quit by appending a exclamation ‘!’ mark)

:w!

Show line numbers

:set number

Remove line numbers

:set nonumber

Enable syntax highlighting

:syntax on

Disable syntax highlighting

:syntax off

Change colorscheme

:colorsheme <colorscheme> #E.g. desert

Enable auto indent

:set autoindent

Remove auto indent

:set noautoindent

Set tab spaces

:set ts=4 #Sets tab to 4 spaces

Always show status bar (with filename)

:set ls=2

Show special characters

:set list #Use :set nolist to turn off

Delete all lines between X and Y

:X,Yd #X and Y are linenumbers

Load new file into vim. Can also be used to refresh current file – just use ‘e’ without path or filename

:edit /tmp/myfile #or 'e' for short

Run external command. Vim let you run the command, shows the result and switches back to your Vim window again when you press Enter

:! command #E.g. ':! ls' will show the files of your current location

Go to a specific line numer

:<linenumber>

Substitute something with something else in the whole file

:%s/old/new/g

Search for a string in file (next search result = n)

/<search string>

Use regexp

/<regexp>

Undo last action

u

Split window vertically

ctrl + wv

Split window horizontally

ctrl + ws

Create new vertical window

:vnew

Move between windows

ctrl + ww

Rotate windows

ctrl + wr

Diff two outputs from standard input (from command line)

vim -d <(command1) <(command2)

Diff two files inside vim

:e file1 #Open first file
:diffthis #Add window to 'diff pool'
ctrl + wv #Open new window
ctrl + ww #Move to new window
:e file2 #Open second file 
:diffthis #Add window to 'diff pool'
:diff #Execute diff
# Extras
:set diffopt+=iwhite #To ignore whitespaces

If you want to diff two windows (and not open files) just add them to 'diff pool' and run the :diff command

COMMAND MODE OPTIONS
Delete 5 lines
Stand on the first line you want to delete and press 5dd
Delete a whole line
Stand on the line and press: dd
Direct search of word
Stand in the word and press: *
Find matching bracket
Stand on a bracket and press: % (press % again to go back to previous bracket)
Expand all nodes in diff mode
Press: zR
Fold all nodes in diff mode
Press: za