TALLCHAD.COM© Photos Sketchbook Movies Misc

VI cheatsheet

remove whitespace etc at the start of all lines
:%s/^[ \t]*//g


ctrl-S   REPLACE current line, and start editing


v   Enter visual mode (for visually marking blocks etc)

Blocks
    mx Mark Beginning of Block (x may be any letter)
    y'x Mark End of Block and ``Copy'' (into x buffer)
    d'x delete from here to mark  (into x buffer)

Indent a section
mark the start (mm)
go to the bottom
type >'m

Multiple Files
    :n newfile opens the newfile
    ctrl-^ Switch to the alternate file
You hav to have saved the file or it won't swap

Refresh                                      ^L
file info and line info                 ^g
go to previous command             :[up arrow]

delete blank lines        :g/^$/d

delete lines containing [X]        :g/[X]/d
delete lines NOTcontaining [X]        :g!/[X]/d

search one or more wildcards:
.*
don't forget the dot!
 

  

:%s/,$/,,/g
 replace commas at the end of a line withthree commas. (search)
:%s/<\(\w*\)/<\U\1/g Uppercase all words that are preceded by a < (i.e. opening HTML tag names): (search)
:%s/.*/\L&/g Lowercase the entire file (search)
$
end of line
J
joins this line to the next one
<ctrl> v [enter]
makes control character for [enter]
<ctrl> q [control character]
**FOR gvim** makes control character for [control char]

Collapsing Brackets (FOLDS)

cant use undo with these

zfa{

Fold current { bracket

zo / zc

Open /close fold

[z ]z

Goto start / end of current fold

zj / zk

Goto next / prev fold

zd



Sorting a section

Command Explaination
1.   Move the cursor to the first line to be sorted.
2. ma Mark the first line as mark a.
3.   Move to the bottom of the text to be sorted.
4. !'asort The ! command tells Vim to run the text through UNIX command. The 'a tell the editor that the text to be worked on starts at the current line and ends at mark a. The command that the text is to go through is sort.

External links

Search & replace Quick useful commands

How do I insert text?

How do I delete something?

How to undo and repeat commands?

I just finished my document, how do I save?

How can I include text in my document?

How can I edit my text?

Is there a way I can cut and paste?

The yanked lines can be put as often as one likes, which is a good way to repeat things. Also, deleted lines can be put as well, so dd is the same as yy dd. Also, there is only one default buffer. To move two things, or to yank and put with other work in between, you can save to named buffers. For example "ayy will yank a line in buffer a. Then buffer a can be pasted using "ap.

How do I change the case of letters?

Use the tilde (~) to toggle the case (lower/upper) of letters. It will ignore non alphabetical characters.

How do I switch between files when editing multiple files?

When editing several files, you can switch to file #x using the command: :e#x

Is there a way I can switch two characters?

You can reverse 2 characters in a word by typing xp (x to delete and p to paste).

Can deleting be used with a search operation?

d/pattern ESC deletes up to the pattern, but leaves the pattern. Then neat thing about this principle is it works with most other Vi commands such as y.

What are some examples of search and replace patterns?

As usual, type u to undo and & to repeat a substitution.

Is there a way to execute a unix command from inside Vi?

Type :!cmd where cmd is a unix command.

I'm tired of having to redefine all of my options every time I go into Vi, is there any way around this?

You can setup a file named .exrc in your home directory that has the following syntax:
set ai
set wrapmargin=1
map @* I/*^[A*/^[
map @f !}fmt^M
map @d :r !date +"\%a \%b \%d \%y"^M
map @t :%s/^I/ /g^M

I'll explain this line by line:

1: sets the autoindent mode
2: sets a wrapmargin so text will auto-wrap at end of line
3: implements line commenting (C languagne). To comment a line, hit @*
4: implements paragraph formatting. To reformat a paragraph, hit @f
5: implements a current date function. To insert the current date, hit @d
6: converts tabs to 4 spaces. All you need to do is hit @t

Notes:

How do I reformat paragraphs in Vi?

Rather than write a set of macros to do this for you, it's usually easier just t o use an external program that does it for you. If you have fmt(1), then {!}fmt will reformat the paragraph under the cursor. This can be fitted into a macro easily enough.

I hope this answers many of your questions. If you have more questions, you can check the USENET Vi FAQ.
Good luck, and have fun!