Pointer Variables and Dynamic Allocation
Vim for programmers
Introduction
Modes
- Normal: typed characters are commands to editor [initial mode]
- Input: typed characters are inserted into file [enter/exit with i/escape]
- Replace: typed characters replace characters in file [enter/exit with R/escape]
- Visual: highlight text
- Command: command line commands
Important Minimal Essentials
- Esc - Exit insert mode
- :wq : Exit and save changes
- :q! : Exit and abandon changes
Command Mode
- Enter command mode with - in normal mode
- Example: :hardcopy (:ha)
- Tab - command completion
- Exit command mode with enter, esc, or ctrl-c
- Example: :set number, :set nonumber
- Example: :set spell, :set nospell
Printing
- :ha - hardcopy - on windows, brings up menu
Help
- :help - open help window
- :help topic - open help window on topic
- tab does completion of topics
- :q - exit help
Simple File Commands
- :q - exit if no changes
- :q! - exit and abandon changes
- :wq - exit and save changes
- :w - save file (but don't exit)
- :w filename - save file to filename
More File Commands
- :w filename - save file to filename (continue editing current)
- :w! filename - save file to filename, overwriting existing (continue editing current)
- What if you want to change name of file currently editing
- :saveas filename - save current file as filename and begin editing filename
Bang Commands
- Used for dangerous versions of commands
- :q - exit if no changes - will not exit if changes
- :q! - exit and abandon changes
- :w filename - save file to filename (continue editing current) - will not write if exists
- :w! filename - save file to filename, overwriting existing (continue editing current)
- :ha - hardcopy - print dialog
- :ha! - hardcopy - print without dialog
Input Mode Commands
Change to input mode
- i - input
- s - delete the current character and enter input mode
- I - input at beginning of line (at indent)
- a - append to current character
- A - append to end of line
- o - open the line below
- O - Open the line above
Exit Input Mode
- esc - exit from input mode
Change to input mode - Advanced
- gi - input at location of most recent input
- gI - input at beginning of line ignoring indent
G commands - Advanced
- Several commands have g versions
- i - input
- gi - input at location of most recent input
- I - input at beginning of line (at indent)
- gI - input at beginning of line ignoring indent
- J - Join lines, insert space between lines
- gJ - Join lines, don't insert space
More G commands - Advanced
- gv - remark visual area
- gg - go to top of file
- gd - go to declaration (ie first mention)
- ga - show ascii
- ctrl-g - show file information
Control commands in input mode - Advanced
- ctrl-t shift right (ie tab) line
- ctrl-d shift left (ie untab) line
- ctrl-y insert character from line above
- ctrl-e insert character from line below
Replace Mode Commands
Change to Replace mode
- R - Replace current characters with input characters
- esc - exit from Replace mode
Movement
Cursor Movement - Character
- hl - left and right one character
- jk - up and down one line
Cursor Movement - Word
- w - start of word forward
- b - start of word backward
- e - end of word
- ge - end of word backward
Cursor Movement - Line
- 0 - beginning of line
- $ - beginning of line
- 20| - column 20
Cursor Movement - File
- gg - top of file
- G - end of file
- 3 G - line 3
[Count] Movement
- 3 movement - move 3 times
- 3 h - move 3 characters left
- 3 k - move 3 lines right
- 3 w - move 3 words forward
- 3 b - move 3 words backward
Screen Movement
- ctrl-f - Screen forward one screen
- ctrl-b - Screen backward one screen
Screen Movement - Advanced
- 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
More Cursor Movement - Advanced
- 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 (Advanced, but useful)
- 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 - Advanced
- 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
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
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
Regular Espressions - Advanced
- ^ beginningg of line
- $ end of line
- . matches any letter
- matches multiple of previous item
- MANY more
Editing
Deleting character [can use counts]
- x - one character
- 3x - 3 characters
Undo, redo, repeat
- u - undo
- ^r - redo
- . - repeat last edit command
Command plus motion
- dw - delete one word forward
- db - delete one word backward
- 3dw - delete three words forward
Deleting lines [can use counts]
- dd - delete entire current line
- d$ - delete to end of current line
- D - to end of line
Cut and Paste
- cut with delete commands above
- p - paste after current line/character
- p - paste before current line/character
Swap
- xp - characters
- ddp - lines
Copy (Command and motion) and Paste
- yy - yank line
- Y - yank line
- yw - yank word
Copy Buffers - Advanced
- "aY - Yank into buffer a, replacing contents of a
- "ap - put buffer a, leaving contents of a
- "AY - Yank into buffer a, appending to current contents
Replace a Character
- rx - replace current character with x
Changing words [all take counts]
- cw - one word forward
- cb - one word backward
Changing lines [all take counts]
- cc - change entire current line
- c$ - change end of current line
- C - change to end of current line
Text Objects - Advanced (but really useful)
- 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 - Advanced
- 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?)
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
- Can operate on visual area (see below)
Visual Mode - Highlighting
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
Folding
Creating Folds
- Creating folds and opening and closing folds are different operations
- zf - creates fold from visual area and closes it
- :set foldmethod={manual, indent, marker, ...}
Opening and Closing Folds
- zo - open fold at cursor
- zc - close fold at cursor
History Windows
Command History Window - Advanced (but really useful)
- Enter with q:
- Search and edit as in normal window
- Execute with enter
- Exit with enter on empty line
- Exit with ctrl-c twice
Search History Window
- Enter with q/
- Exit with ctrl-c twice
Multifile Editing (Advanced, but few basics)
Editing another file
- :vi foo.adb - ends current edit and edits foo.adb
- :vi # - ends current edit and edits most recently edited file
- :ls - list of files currently in buffers
- :bu n - edits buffer n
Split Windows
- :split filename
- :vsplit filename
- Movement between windows: ^W then hjkl
- Exit with :q
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
:args
- vim first.adb second.adb - opens 2 files with one vim session
- :args - lists the open files in the argument list
- :n - goes to next file in argument list
- :prev - goes to previous file in argument list
- :argdo - perform the same operation on all files in argument list
Configuration
Set Command
- Example: :set hlsearch, :nohlsearch
- Example: :set spell, :nospell
- :set
- :set all
- :options
:options Command
- can check and set options in place
- set by pressing enter
- exit with :q
Map Command
- Map one character to another
- Favorite mapping: :map ; :
- Query with :map
Vim Startup
Startup Files
- .vimrc - commands in .vimrc are automatically read and executed on startup
- Put .vimrc in your home directory
- May need underscore vimrc on Windows
- Check current vimrc with :echo $MYVIMRC
- Check current home with :echo $HOME
- Check startup scripts :scriptnames - shows all files read on startup
Vimrc Commands - Colons not required
set nobackup " backup files are useful, but they create clutter
filetype indent on " do automatic indentation
filetype plugin on " load filetype plugins
syntax enable " turn on syntax highlighting
set ai " autoindent
set backspace=2 " backspace past newlines
set shiftwidth=4 " how much to shift on >>
set smarttab " Enters shiftwidth spaces at line start when tab entered
set ignorecase " Ignore case when searching
set smartcase " but, if case is used in the pattern, DON'T ignore it
noremap ; : : Save a keystroke
remap q; q: : Save another keystroke
Useful operators
- Shift: >>
- Shift: <<
- Indent: ==
- For visual areas: <, >, =
Plugins
- Files in plugin directories are loaded on startup
- Loads from system and user plugin directories
- To install in user plugin, simply drop .vim file there
User Plugins
- Your Plugin directory: $HOME\vimfiles\plugin
- Try: :split $HOME\vimfiles\plugin
- Find plugins on vim.org
- Useful plugins:
supertab.vim - Enables keyword completion
calendar.vim - Open a calendar
jad.vim - Edit a compressed file (current?)
Filetype Plugins
- Loaded based on the type of file that is being edited
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)
User Filetype Plugin: java.vim
set makeprg=javac\ %
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>
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
Input Mode Completion
- In input: ctrl-x plus ...
- I prefer Supertab
What happend to my paste??
- Sometimes a cut and paste does too much indenting
- :set paste
- then do the paste
- :set nopaste
References
- vim.org
- search
- :help quickref
My Language Summary
- An attempt at listing all commands
- Under construction
- mapping.html
How to learn
- vimtutor
- pick a few, learn more as needed
- gets easier!
Happy 21st Birthday Vim
History
Some baby pictures
Pictures