Vim Jump-Start
Prelude
I created this document as a quick reference. There are many other references out there, and vim gives you a lot of help itself, but I find this useful, so I have made it available to anyone who is interested. If you are new to Vim, but a bit daunted by what you need to learn, I suggest you take a quick look here (small movies on some of the cooler aspects of Vim).
Notes
vi
(and vim
) runs in 2 modes: insert and command, ESC
changes to command mode.
This command list was made while using vim
(vi
improved), all commands may not work in other versions of vi
.
In this document, c
stands for ctrl
and m
for alt
.
%
usually represents the whole buffer
If you're having trouble starting vim in a console, try vim -X
See the "additions to the vimrc
configuration file" section to enable ms-windows-like editor commands.
basic commands
quit : :q
(closes the editor, use :bd
to just close the buffer)
write/save : :w
quit with write : :wq
quit without save : :q!
close buffer : :bd
open file : :e
(filename)
new file : :new
page up : c-u
or page-up
page down : c-d
or page-down
up : k
or up-arrow
down : j
or down-arrow
left : h
or left-arrow
right : l
or right-arrow
go to file start : gg
go to file end : G
go to line : nG
(where n
= a number)
go to line end : $
go to line start : 0
jump forward word : w
jump forward word : b
begin typing/insert : i
insert at end of line : A
join lines : J
(nJ
to join more than 2 lines - n
= a number)
indent a block : v
(then select the block) >
(or <
to go left)
undo last command : u
redo : c-r
repeat last command : :<up arrow>
repeat last change : .
select : v
(then select text, and choose y
to copy or x
to cut) for example to select from current position to the end of the file: vG
, or the beginning: vgg
copy line : cc
delete line : dd
delete character : x
delete word : dw
delete till end of line : d$
delete several lines : :dn
eg :d3
delete a specific line : :nd
(where n
= a number, not to be confused with above)
paste (after character/line) : p
paste (before character/line) : P
search forwards : /
(followed by word to search) (/
alone to search next)
search backwards : ?
(followed by word to search) (?
alone to search next)
search files : :vimgrep /love/ *.txt
follow this with the command :cwindow
to see a list of all the files found
change case : n~
(where n
= a number)
replace/substitute : :%s/old/new/g
(%
says to do this for each line, %
can also be a set number). To replace a character with a newline or carriage return: :%s/ /<CTRL-v><Enter>/g
(*nix) or :%s/ /<CTRL-q><Enter>/g
(windows).
replace single character : r<character>
match parenthesis : %
(after placing the cursor on the first parenthesis)
code complete : ctrl-x ctrl-o
word wrap : :set wrap
no word wrap : :set nowrap
line numbering : :set number
no line numbering : :set nonumber
execute an external command : :!
(followed by command)
read in results of a command : :r!command
or !!command
(eg :r!date
)
insert an external file : :r (filename)
open a shell : :sh
create a page break : while in the insert mode, CTRL-l (that's an "L"); "^L" will appear in your text and will cause the printer to start a new page.
execute perl : :!perl -wc %
(for syntax check, drop the "c
" for normal run)
compile java : :!javac -verbose %
run java : :!java %<
(the <
removes the extension)
compile c#: :!csc %
spell check : :!ispell %
(or :!aspell check %
)
new buffer : :new
next buffer : :bn
last buffer : :bl
change buffers : :(n)b
close buffer : :bd
(close specific buffer = :bd 3
(can have more by putting a space between the numbers))
wordcount : g then ctrl-g
change between split windows : c-w
make current window the only one visible : :only
(or) c-w o
change/set options : :options
(these can also be set in the .vimrc/.gvimrc
files)
create/run a macro : qa
to begin recording into register 'a
', followed by q
to stop recording, then @a
to repeat the commands you recorded. You can record into 26 other registers (for each letter of the alphabet).
create a fold : zfap
(position the cursor at the start of the block of code first)
open a fold : zo
close a fold : zc
open all the folds : zr
close all the folds : zm
Make current line uppercase : gUU
Make current line lowercase : guu
Sort contents : :sort
New Tab : :tabe
Browse File system : :Explore
htmlize a syntax-highlighted file : :runtime! syntax/2html.vim
this will convert a file that has been syntax highlighted into html (handy for use on websites and such)
Remove blank lines : :g/^$/d
additions to the .vimrc configuration file
set nocompatible source $VIMRUNTIME/mswin.vim " adds recent filelist; downloaded from: " http://www.vim.org/scripts/script.php?script_id=521 source $VIMRUNTIME/mru.vim behave mswin " OS-specific settings if has("win32") " windows preferred font... set guifont=HE_TERMINAL:h10 else " linux preferred font... set guifont=-dec-terminal-medium-r-normal-*-*-140-*-*-c-*-iso8859-1 endif " slow down the default cursor blink set guicursor=a:blinkon600-blinkoff400 " or if you don't like blinking cursors, switch it off "set guicursor=a:blinkon0 " remove gui toolbar set guioptions-=T " set tab width to 4 and insert 4 spaces for each tab set tabstop=4 set shiftwidth=4 set expandtab " the awk syntax highlighting works well with my daily logs autocmd BufRead dba_activity.log set syntax=awk autocmd BufRead itinspiration.log set syntax=awk " keymapping; when editing, just use the comma command, eg: ,s (no : first) " (results go into another buffer): " launch SQL*Plus map ,q :new:r!sqlplus roqetman/password@roqdb @# " check Perl syntax map ,p :new :r!perl -wc # " compile Java map ,j :new :r!javac # " compile C# map ,c :new :r!csc # " Map ctrl-f and F3 for windows-type find ":map / :imap :promptfind :nmap :promptfind :map / " launch spell checker map ,s :!aspell check % " change comment colors for command line highlight Comment gui=NONE ctermfg=Green
Additions to syntax highlighting
These are my preferred additions to the $VIM/filetype.vim file:
" Delphi forms au BufNewFile,BufRead *.dfm setf pascal " ChangeLog addendum au BufNewFile,BufRead *.changelog setf changelog " useful if you use vim to read your SQL*Plus buffer (afiedt.buf) au BufNewFile,BufRead *.buf setf plsql
A way to force syntax highlighting
If you add this to a file, when it's opened in vim, it will override the highlighting by extension (to awk
in this example):
/* vim: set filetype=awk: */
A quick Go syntax highlighting hack
There's the official way to do it as detailed here, but this is the method I used to quickly get syntax highlighting to work with the Go language.
Under the syntax
dir, create a file called go.vim
containing the content found here, then edit filetype.vim
and add in this entry:
" Go au BufNewFile,BufRead *.go setf go
Adding Powershell syntax highlighting
As per this site, download the colorization files from here and add the following to your filetype.vim
file:
" Powershell au BufNewFile,BufRead *.ps1 setf ps1
Create your own color scheme
Here's one I created called emaxi.
You need to copy it into your vim colors
directory.
For Macvim, first create this directory: mkdir ~.vim/colors
Then copy your color scheme into this new folder, restart macvim, and you're done!
Unicode
By default, vim is set to ASCII. I add the following to my _vimrc
/.vimrc
file so it can display Kanji and other characters:
" enable unicode support set encoding=utf-8
JSON
I add this to my _vimrc
/.vimrc
file so vim handles JSON text correctly:
" autocommands for JSON files augroup json_autocmd autocmd! autocmd FileType json set autoindent autocmd FileType json set formatoptions=tcq2l autocmd FileType json set textwidth=78 shiftwidth=2 autocmd FileType json set softtabstop=2 tabstop=8 autocmd FileType json set expandtab autocmd FileType json set foldmethod=syntax augroup END
Vim for Writing
On macOS and iOS, I use IA Writer to write my articles and stories on. Sadly, it doesn't exist on Windows, so I found a way to modify vim when a file of .md
(markdown) is opened. First, I downloaded and copied the Pencil colorscheme, then I added the following to my _vimrc
file to wrap, do spellcheck and make for a clean writing environment (the last line is to set a spell-check add file (note the file-type name (info came from here))):
" add syntax highlighting for markdown autocmd BufNewFile,BufReadPost *.md set filetype=markdown augroup Markdown autocmd Filetype markdown set spell autocmd Filetype markdown set wrap autocmd Filetype markdown set linebreak " this one came from here: http://vim.1045645.n5.nabble.com/Save-default-font-on-Gvim-on-Windows-7-td5718120.html autocmd Filetype markdown set guifont=Consolas:h11:cDEFAULT " this colorscheme came from here: https://github.com/reedes/vim-colors-pencil autocmd Filetype markdown colorscheme pencil autocmd Filetype markdown set background=light autocmd Filetype markdown set foldcolumn=4 augroup END set spellfile=C:\roq\backup\Surface\en.utf-8.add
Vim on Mac
The version of vim I use on OS X is Macvim.
Vim on Slackware
On Slackware, the command vi
runs elvis
, a vi
text editor clone by default. If like me, you prefer vim
to be the default, then run this as root: ln -sf /usr/bin/vim /usr/bin/vi
Vim on Windows
Vim works well on windows too (in fact it's my primary editor when I'm on that OS). You use Vim the same way on any OS, but I'll put my specific modifications here as I find them...
Tortoise CVS setting changes in order to use Vim as the diff editor : Diff Application: C:\Program Files\Vim\vim72\gvim.exe
, Two-way diff parameters: -d "%1" "%2"
There is an issue running vimdiff
on Windows (all 64bit versions), to fix it, I added the following to my _vimrc
file:
" fixed win7 issue with vimdiff - fix found here: http://superuser.com/questions/697847/cant-run-vimdiff-7-4-on-windows-7 function MyDiff() let opt = '-a --binary ' if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif let arg1 = v:fname_in if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif let arg2 = v:fname_new if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif let arg3 = v:fname_out if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif if $VIMRUNTIME =~ ' ' if &sh =~ '\' . arg3 if exists('l:shxq_sav') let &shellxquote=l:shxq_sav endif endfunction
An Editor Forever Evolving
Vim may have been based on old vi
technology, but it's continuing to evolve, take a look at some of the latest additions: Some nice video examples, code-completion, a built-in spell-checker (ctrl-x ctrl-o
), and built in grep
(:vimgrep /love/ *.txt
followed by :cwindow
). Also, you can configure it and add plugins to it.