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)
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
- i inserts text immediately before current cursor position
- a appends text immediately after current cursor position
- o opens line below current one for input
- I inserts to beginning of current line
- A appends text to end of current line
- O opens line above current one for input
How to undo and repeat commands?
- u undoes the last action you did
- . (period) repeats the last command you did
I just finished my document, how do I save?
- :w writes the file and save the changes
- :w name same, but the changes go into a file with the new name
- :x,y w name writes only the lines x to y to the file (for excerpting)
- :wq writes and quits at the same time
- ZZ writes and quits at the same time
at the point of the cursor
Is there a way I can cut and paste?
saved for later use
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?
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:
- Please note that the .exrc file contains commands that Vi reads when you first run it. The default directory that Vi looks in for the .exrc file is your home directory. However, if you run Vi from another directory containing a .exrc file, Vi will use the .exrc in that directory and bypass the home directory .exrc.
- You must precede control characters (like ^[ or ^M) with CONTROL-V. For example to enter ^M, type CONTROL-V CONTROL-M.
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!