Nasser Heidari


Vim Jump-Start

Filed under: Linux — Nasser Heidari @ 20:11

Thanks goes to roqet

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)

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)

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))

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


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

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)

additions to the .vimrc configuration file

set nocompatible
source $VIMRUNTIME/mswin.vim
" adds recent filelist; downloaded from:
source $VIMRUNTIME/MRU.vim
behave mswin

" OS-specific settings
if has("win32")
  " windows preferred font...
  set guifont=HE_TERMINAL:h10
  " linux preferred font...
  set guifont=-dec-terminal-medium-r-normal-*-*-140-*-*-c-*-iso8859-1

" 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

" 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

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: