These commands change to input mode, at the cursor
i
insert before current cursor location
a
insert after current cursor location
I
at first non-blank character of the line
A
at end of line
o
open new line below
O
Open new line above
Upper and lower case commands are related
Easier to memorize
Input mode moves existing characters over
Replace Commands
Replace mode overwrites existing characters
These commands change to Replace mode, at the current cursor location:
r
replace character under cursor, once
R
start Replacing characters under cursor
Undo, Redo, and Do Again
In case you make a mistake:
u - undo
^r - redo
U - undoes line to original state
. - do most recent change, again
Commands for Movement
Cursor Movement: Line, Character, Word
Character and line:
hl - left and right one character
jk - up and down one line
Learn these in muscles, not in regular memory!
Words:
w - word forward (whitespace or punctuation delimited)
b - word backward
e - end of current or next word
W - WORD forward (whitespace delimited)
B - WORD backward
Cursor Movement: To a Location
Line Locations:
0 - beginning of line
$ - end of line
File Locations:
gg - Top
nG - line n
G - Bottom
n% - n percent of file
Screen Movement
^f - Screen Forward
^b - Screen Forward
Counts
Most movement commands allow a count
Example: 3w moves 3 words forward
Searching
/string - search forward for string
?string - searches backward for string
n - after a search, finds next occurrence (in same direction)
N - after a search, finds previous occurrence (in opposite direction)
Delete and Change Text
Delete Text
x - Delete character
dw - Delete word
dd - Delete line
D - Delete to end of line
Changing Text
cw - change word
cc - Change entire line
C - Change rest of line
Combining Operator and Motion
dw - Delete word
de - Delete to end of word
db - Delete to back to beginning of word
d$ - Delete to end of line
d0 - Delete to beginning of line
dG - What does this do? (Careful - remember u for undo)
d/foo - deletes up to foo
ce - change to end of word
cb - change to beginning of word
c$ - change to end of line
c0 - change to beginning of line
c/foo - changes up to foo
Combining Counts with Operator and Motion
3x - delete 3 characters
d3w - Delete 3 words
3dw - Delete 3 words
3d5w - Delete 15 words
c3w - change 3 words
c3l - change next 3 lines
Cut and Paste
Paste Deleted Text
Items deleted (with x and d) go into (default) buffer
p puts buffer into file, after cursor
P puts buffer into file, before cursor
ddp - what does this do?
xp - what does this do?
Later see other buffers
Careful: ^x, ^c, ^v don't do cut and paste, by default
See later what they do
Can change default with an option
Copy (Yank) and Paste
y copies items into (default) buffer
Example: yw MOVE p - copies and pastes a word
Example: y3w MOVE p - copies and pastes a word
Y copies a line
Example: Y MOVE p - copies and pastes a line
yy also copies a line
Command Line Mode, Help, Set
Command Line Mode (aka ex mode)
Pressing : opens command line and starts command line mode
Enter command and press enter
Common Commands
:q and :wq
:pwd and :cd
:ha - :hardcopy - print
on rucs, must set printer, but how to set for unix lab?
Abbreviations
Most commands have abbreviations:
:ha = :hardcopy
Can use TAB to expand and cycle through abbreviations
:help
:help - splits window and shows help
Use :q to exit help
Use ^] and ^o to navigate on colored links
:help - splits window and shows help
:help command - splits window and shows help for command
Can use tab for completion
:help abbreviated :h
Set
Set - sets options
:set number
:set nonumber
:set ruler - shows line and column numbers in status line
Can use tab to expand options
:set - shows all set options
:set all - shows values of all option that can be set
Configuration: Vimrc
.vimrc
.vimrc contains commands executed on starting vim
put .vimrc in Home Directory
Find Home Directory with :echo $HOME
Can also be called _vimrc
Check current vimrc with :echo $MYVIMRC
Most Important Commands for Vimrc
:syntax enable " enable syntax coloring
:filetype indent on " enable specific indentation
:filetype plugin on " enable specific plugins
:set ruler " line and col nums in status line
:set ai " automatic indentation
:set ignorecase " ignore case in search
:set smartcase " use case if specified
:set backspace=2 " make backspace better
: is optional in vimrc
Vim for Programmers
Query Settings
:options
:changes
:set
:set makeprg
:verbose set makeprg
:set all
:map
:abbrev
:scriptnames
:function
:command
:marks
More Vimrc Commands
:noremap ; : " Non recursive map of ; to :
:noremap q; q: " Opens command window
:inoremap jk <esc> " Use jk instead of escape!??!
:set history=50 " number of commands to remember
:set nobackup " backup files are useful, but they create clutter
:set expandtab " expand tabs - replace tabs with spaces
:set tabstop=4 " display any tabs as 4 chars
:set shiftwidth=4 " How much to indent on shift, also backspace 4 chars
:set smarttab " Enters shiftwidth spaces at line start when tab entered
:set softtabstop=0 " ??
:set shiftround " indents are rounded to multiples of shiftwidth
Tabbed Editing
:tabe filename - opens a new tab
:gt - move to next tab
:gT - move to previous tab
works in unix
vim -p first.adb second.adb - opens 2 files in 2 tabs
Split Windows
:split filename
:vsplit filename
Movement between windows: ^W then hjkl
Exit with :q
Command History Window
* q:
* To save a keystroke, use :map q; q:
* Completion with ctrl-X ctrl-P
* Bring line down with ctrl-C
* Exit with :quit OR : OR ctrl-C ctrl-C
* Set history for number of lines saved
Search History Window
* q/
* To save a keystroke, use :map q; q:
* Completion with ctrl-X ctrl-P
* Bring line down with ctrl-C
* Exit with :quit OR : OR ctrl-C ctrl-C
Plugins
Install plugins to configure vim
Files in plugin directories are loaded on startup
Loads from system and user plugin directories
To install in user plugin, simply drop .vim file in respective folders there
Plugins pathogen and vundle provide automatic installation of plugins
User Plugins
Your Plugin directory: $HOME\vimfiles\plugin
Alternate location: If this does not work on rucs, try $HOME/.vim
Look for local plugin locations in :set runtimepath
Try: :split $HOME\vimfiles\plugin
Find plugins on vim.org
Some useful plugins:
supertab - Enables keyword completion
nerdtree - Directory browser in split window (or VTreeExplorer
taglist
command-t - like textmake snippits
jad - Edit a compressed file (current?)
calendar - Open a calendar
Filetype Plugins
Loaded based on the type of file that is being edited
Stored in folder ~/vimfiles/ftplugin
System provides some; can add more
User Filetype Plugins
Your Plugin directory: $HOME\vimfiles\ftplugin
Try: :split $HOME\vimfiles\ftplugin
Put commands in language.vim (eg java.vim, ada.vim)
Can put newlines in definitions (with \ at end of line, I think)
Example User Filetype Plugin: java.vim
ab pc public class <CR>{<CR><CR>}<UP><up><up><END><left>
ab psvm public static void main(String[] args)<CR>{<CR><CR>}<UP><TAB><TAB>
ab sop System.out.println();<LEFT><LEFT>
ab fori for (int i = 0; i < X; i++)<CR>{<CR><CR>}<UP><TAB><TAB>
set makeprg=javac\ %
Example User Filetype Plugin: ada.vim
ab fori for i in 1 .. 1 loop end loop;
ab ati ada.text_io; use ada.text_io;
ab afti ada.float_text_io; use ada.float_text_io;
ab aiti ada.integer_text_io; use ada.integer_text_io;
ab proc procedure foo is beginend foo;
autocmd BufNewFile,BufRead *.ad* set makeprg=gnatmake\ %
Put ada.vim in ftplugin
Auto command needed because of default ada configuration does no allow setting makeprg in the user ftplugin/ada.vim
Autocmd
Specify commands to occur on various events
Quickfix window
:make - compile with makeprg
:cc - go to current error
:cn - go to next error
:cp - go to prev error
:cope - open quickfix window
Make program for Ada, C, Java
* Java: set makeprg=javac\ %
* Ada: set makeprg=gnatmake\ %
* C: set makeprg=gcc\ -o\ %:r\ %
% is a special register with the file name
:r gives removes the suffix
Templates
Can create template files to open automatically
Create templates/java, for example
Editing
Shift and Indent
<< - Shift current line left
>> - Shift current line right
== - indent current line
Visual Commands
Highlight text
Then operate on it
Three highlighting commands: v, V, ^v
Three kinds of highlighted areas
Character - v
Line - V
Block - ^v
Commands on Visual Areas
d and x - delete
c - change area
C - change area lines
rx - replace each character in area with character x
R - replace all lines of area
< > = - shift lines left, right, or indent
Commands on Visual Blocks
I - Insert at beginning of block on all lines
A - Insert at end of block on all lines [use $ to mark end of line]
c - Change block on all lines
C - Change from beginning of block to end of line on all lines
:s - Substitute
Other Visual Commands
gv - rehighlight area
o - go to beginning/end of area
O - go to beginning/end of line of block
v, V, ^v - change visual mode
Text Objects
Can change and delete text objects
Example: di( - delete an inner () block
Examples: ca( - change a () block
Example line to operate on: procedure foo(p: param1) is.
Other objects: [ { < ' "
More Text Objects
Tag objects: cit - change inner tag
Example line to operate on: a list item .
Words - w W
Paragraph and sentence: p s (what is a paragraph?)
Screen Movement
ctrl-y - Screen down one line, cursor moves with screen
ctrl-e - Screen up one line, cursor moves with screen
ctrl-u - Screen backward one screen
ctrl-d - Screen forward one screen
Cursor Movement
HML - High, Middle, Low on screen
zt - Move cursor line to top of screen
zz - Move cursor line to middle of screen
zb - Move cursor line to bottom of screen
Mark Movement
ma - mark current location with mark a
'a - go to mark a
can use any letter
'' - return to previous location (two single quotes)
Jump Movement
ctrl-o - go to older location on jump list
ctrl-i - go to newer location on jump list
Searching
/pattern - forward search for pattern
?pattern - backward search for pattern
n - repeat previous search
N - repeat previous search, in reverse
* - searches for next occurrence of word under cursor
# - searches for previous occurrence of word under cursor
Search Options
smart case: ignore case unless used
Example: Word word word Word word (try /word and /Word from column 1)
:set hlsearch - turn on search highlight
wrapscan - search around EOF
Advanced Searching
/pat/e - go to end of located string
/pat/b+1 - go to second character of located string
/pat/e-1 - go to next to last character of located string
/pat/e+1 - go to character after located string
/pat/-2 - go to 2 lines above located string
/pat/+2 - go to 2 lines below located string
Substitute Command - Substitute one string for another
:s/old/new - default: operate on current line
:s/old/new/g - all occurrences on line
:s/old/new/c - with confirm
:%s/old/new/gc - all occurrences in file, with confirm