vim
a widely used text editor among Unix users
A really short intro to vim
To run vim from the terminal, just type in “vim” (or sometimes “vi” works).
$ vim hello.cpp
Once vim is open, you can start editing the file immediately.
Basic use
<Esc> is the escape key or use <ctrl>[ sometimes written as C-[
vimtutor : starts vim editing a copy of a tutorial file – very good.
i : insert mode. Next keys typed are inserted into the file.
<Esc> : Escape from insert mode so you can navigate and use edit commands (stop selecting)
h j k l : move cursor ( h: ← j: ↓ k: ↑ l: → )
A : Append at end of line
o : Insert at new line below
u : undo last command, again and again
x : delete character under cursor
dw : delete everything right from the cursor to the start of next word (and put it into the default register)
dd : delete line (and put it into the default register)
p : paste the default register
/myname : search forward for myname
:wq : write and quit
:x : write and quit
:w filename : write a copy of the file you are editing as filename
:q! : quit without saving even if changes were made!
:help : display help
<Tab> : use tab completion to scroll through commands that start with what you typed
COPY PASTE (for CUTting lines use dd as described above)
v : visual mode – use to select text with cursor movement or mouse
y : use to yank (copy) what was selected
<Esc> : esc gets you back to the main mode
^ w e $ : bigger movements: beginning of line, word, end of word, end of line
Modes:
normal, insert and visual, there are others too
<Esc> takes you back to normal
Enter a number before a command to repeat it, examples:
10w : skip forward 10 words
10dd : delete 10 lines
Commands are case sensitive:
c : starts a change command
C : change to end of line (same as c$)
ce : change to end of word (a complete change command)
Other useful hints
www.vim.org : Visit frequently
comp.editors : Vim dominated newsgroup
-
g* g# : find word under cursor (forwards/backwards)
% : match brackets {}
<C-N> <C-P> : word completion in insert mode
<C-X><C-L> : Line complete SUPER USEFUL
<C-R><C-W> : Pull cword onto search/command line
:set ignorecase : you nearly always want this
:set smartcase : case-sensitive if search contains an uppercase character
:syntax on : colour syntax in Perl,HTML,PHP etc
:h slash<C-D> : type control-D and get a list all help topics containing slash
What is vim?
Historically, there are two widely used text editors among Unix users: vi
and emacs
.
vim
is the most recent version of vivim
stands for vi improvedvim
andemacs
tend to have long arguments about which is better.- These arguments are pointless and tedious.
- They are both good; they just have different design tradeoffs.
If you want to learn about emacs instead of vim, visit the Emacs Page on this website. Since this page is about vim, we won’t mention emacs further.
Quick Reference Guides
- Vim wiki http://vim.wikia.com/wiki/Category:Getting_started
- Vim tutorial http://vim.wikia.com/wiki/Tutorial
How To
- Line-numbers on the side:
:set nu
- To make this persist between invocations of vim, see Vim: Customization
-
What do
:w
,:wq
, and:wq!
mean? :w
means “write”. Hit “Escape” and type in the character sequence to write your edits.:wq
means “write and quit”. Hit “Escape” and type in the character sequence to write and quit from the Vim environment.:q!
means “quit without saving”.
And a bonus:
:u
means “undo”.
Finding and Replacing
- To go to a specific line number:
:[LINE_NUMBER]
- For instance, if I was trying to go to line 181 in a file, I would enter
:181
- For instance, if I was trying to go to line 181 in a file, I would enter
- To search for a character string:
/[STRING]
- Note that you may need to use the escape character
\
before any special characters
- Note that you may need to use the escape character
- To search/replace (e.g. change
old
tonew
):- To replace the first encountered instance of a string:
:%s/old/new/
- To replace all instances of a string:
:%s/old/new/g
- To replace the first encountered instance of a string: