Vim, the one true editor.

Useful Regexes

Delete lines with one character or none
:g/^.\?$/d

Remove spaces at end of each line
:% s/\s\+$//g

Lines with one word and postcode at end of line, append to previous line
:g/^\w\+\s\d\{4}$/normal kJ

Put a space at the beginning of each line for lines containing no digits \D
:% s/\(\D\+$\)/ * \1/g

Move lines that start with a space and move to previous line
:g/^\s\*/normal kJ

Remove the following Codes
:g/\sBC\s\|\sDC\s\|\sGPO\s\|\sMC\s\|\sPO\s/d

Show long lines greater than 60 characters
/\%&gr;60v.\+

Delete lines of text
:g/Where a name/d

Check for lines with leading digits
/^\d\+

One Window Only

ctrl+w o

Send files to vim from command line

alias g="vim --remote-silent"

Text Width and Wrapping

Use the option textwidth to set the line length of inserted text :set tw=30
Turn wrapping off :set nowrap

Printing

Print from Vim :%w !lp

Yank and Paste Using Clipboard

Yanking text to the clipboard "*y
Pasting from clipboard "*p

Yank and Paste Using Register

Yanking into register x, one word "x yiw
Paste from register x "xp

Searching

Ignore case :set ic
Highlight search, insensitive search :set hls is
Remove highlighting :set nohlsearch

Replacing

To do a find and replace :s/back/black/gc on the current line
To do a find and replace on the whole file :%s/back/black/gc g=global c=confirm
To find the next instance of a word, have the cursor on the word then type * while in command mode
Go to line number 60, from command mode. 60G or use 60gg
Set a mark m character
Go to mark ' character
Go to matching bracket %

Append numbers to the start of each line in a tab delimited file

Inialise variable :let cnt = 0
Global command:g/^/let cnt = cnt + 1|execute"s/^/".cnt."\t"
Or you could do something like this.
From line 3 to end of file :3,$ g/^/let cnt = cnt + 1|execute"s/^/".cnt."\t"

Line Numbers

Turn on line numbers :set nu
Turn off line numbers :set nonu

Key Mapping

show what the key mappings are in use :map

Spelling

To check the spelling of a file first save the file :w! then use ispell :! ispell %
An easier method is to use the :set spell command. Then press z followed by = for each missed spelled word.

Shell Commands

Type a command in vim then pass the line to the shell and return the results into vim
Eg. Type date in Vim. Then in ex mode type :. !sh

Syntax Highlighting

For Oracle, sqlplus only: DEFINE _EDITOR='gVim -c "set filetype=sql"'
Turn off syntax highlighting :highlight on
:set nohls
:set nohls make the colors for syntax highlighting darker :set bg=dark

Ignore Case When Searching

:set ingnorecase
:set noingnorecase

Editing Files Remotely

:e ftp://password@example.com/folder/file edit a file
:E ftp://password@example.com display netrw file browser

Macros

Record Macro qa q=record macro a=record macro to lecodeer a
Run Macro @a @=run macro a=macro in a

Explorer View

Explorer view :E shows the files located at Vim's current working directory. You can use the J and K keys to navigate files and directories. To open a file press enter key. Explorer view opens a window to view the filesystem. Pressing i will toggle the display.

Deleting Words

:1,$g/blah/d = delete begining to end blah
:1,.g/blah/d = delete to current position
:1,$g/blah/i = delete begining to end blah

Removing Vertical Tab Character (ASCII char 13)

:set list = Show invisible characters
:%s/^V^K// = ASCII Char 13 is represented as ^K in VIM

Indenting Code

2>> The 2 represents the number of lines to indent
Before
blah
blah
After
    blah
    blah

Change line to uppercase

asfdjaslkjflkasfa
:s/\(.*\)/\U\1/
change U to u for first character uppercase

Change line to lowercase

ASFDJASLKJFLKASFA
:s/\(.*\)/\L\1/ change L to l for first character lowercase

Clear a register

clear the "a" register qaq
view contents of register "a" :reg a

Open and Close Ex Command Window

Insert Mode
Open q:
Close ctrl+w q
Command Mode
Open ctrl+f
Close ctrl+w q

Swap Windows

:h ctrl-w_ctrl-x and/or :h ctrl-w_ctrl-r

Change Case of Line

If the leading V means visual mode
VU change line to uppercase
Vu change line to lowercase

Change File Format

unix \n
dos \r\n
mac \r

This command will change the fileformat of the file in the current buffer, this may be necessary if you are seeing many ^M or ^@ symbols. You may then need to remove /r characters.
:w ++ff=unix
:w ++ff=dos
:w ++ff=mac

Set fileformat of open buffer

Here we are changing a DOS file to unix fileformat. I like all my files to be in Unix format.
:e! ++ff=unix You will then see the \r shows up as ^M.
:% s/\r//g Repalce \r with nothing. This will remove the ^M at the end of each line.

Encoding

set encoding - changes the output encoding shown in the terminal.
set fileencoding - changes the output encoding of the file that is written.

Cleaning up XML to get a list of tags

When editing this code there is one element per line as seen when viewing raw XML in a MS web browser.
:%s/>.*//g Remove everything after first tag.
:%s/\///g Remove trailing forwad slashes.
:%normal xxx Remove first three characters from each line.

Download and mirror website with Wget

$wget -m robots=off http://www.kaizentek.com.au

View HTTP Header with Curl

curl -I http://example.com/

Write to Buffer in Command Mode

:let @a='xxxxx'

Append to Register

Use a capital letter. "Ay

FTP and Edit Files within Vim

Open VIM then type the following in command mode. :e ftp://<username>@<URL>

G Command to Yank Lines and Line Below

G command matches lines that start with word function, and the .,+ is a range which selects the current line and line below then the lines are yanked to register a (capital A means append to a register).
:g/^function/.,+ y A