Notes on Using Vim: The Basics
-----------------------------------------------------------------------------
### Switching between Insert and Normal Modes
* use i to enter insert mode
* use esc to exit insert mode and enter normal mode
-----------------------------------------------------------------------------
### Status line
-----------------------------------------------------------------------------
### Moving one character or line at a time
* hjkl - left, down, up, right one space or line
* Practice this until it becomes second nature
-----------------------------------------------------------------------------
### Vim is case sensitive
* Example: j and J are different commands
* Later we will see how to make searching NOT case sensitive
-----------------------------------------------------------------------------
### Moving by words
* w - move forward one word
* b - move backward one word
* e - move to end of current word
-----------------------------------------------------------------------------
### Count operator
* count operator does operator count times: 3k moves up 3 lines
-----------------------------------------------------------------------------
### Larger movements in a line
* 0 - move to beginning of line
* $ - move to end of line
-----------------------------------------------------------------------------
### Larger movements in a file
* gg - move to beginning of file (or :1 or 1G)
* G - move to end of file
* CTRL-f - move one screen forward
* CTRL-b - move one screen back
* % - find next item and move to its match, (example (ne{braces}sted) end)
-----------------------------------------------------------------------------
### Undo, Redo, repeat
* u - undo
* CTRL-r - redo
* . - repeat most recent change
-----------------------------------------------------------------------------
### Other ways to start insert mode and INSERT characters
* o - open new line BELOW
* O - open new line ABOVE
* i - insert BEFORE cursor location
* s - insert AT cursor location (replacing current character)
* a - insert AFTER cursor location
* A - insert at end of current line
* I - insert at beginning of current line
- But after initial tab characters
-----------------------------------------------------------------------------
### Change a word, line, or remainder of a line in insert mode
* cw - change word: delete current word and enter insert mode
* cc - change line: delete current line and enter insert mode
* C - change line: delete to end of line and enter insert mode
-----------------------------------------------------------------------------
### Modify a single character
* r - replace character at cursor
* ~ - Change case of current character
-----------------------------------------------------------------------------
### REPLACE MODE
* Characters that are type replace existing characters in the line
* R - enter REPLACE mode
* Backspace restores characters
* Newline inserts a new line, and does not replace next line
-----------------------------------------------------------------------------
### Deleting character, word, line (into a buffer)
* x - delete character at cursor
* dw - delete word to right of cursor
* dd - delete entire line
-----------------------------------------------------------------------------
### Useful with counts - Examples:
* 3x - delete 3 characters
* 3dw - delete 3 words
* 3cw - change 3 words
* 5s - substitute 5 characters
-----------------------------------------------------------------------------
### Put character, word, line (from buffer)
* p - puts (ie inserts) buffer after cursor
* P - (ie inserts) buffer before cursor
* inserted characters are inserted into same line
* inserted lines are inserted above or below
-----------------------------------------------------------------------------
### Shortcut: swap two characters or lines
* xp - swap two characters
* ddp - swap two lines
-----------------------------------------------------------------------------
### Yank - Copy character, word, line into a buffer
* Y - Copy current line into buffer
* yy - Same
* yw - Copy to end of current word into buffer
-----------------------------------------------------------------------------
### Joining Lines
* J - Joins two lines
-----------------------------------------------------------------------------
### Searches: Locate a string in the file
* /string - search for string
- end with enter
- normally wraps [option wrapscan]
- normally case sensitive
- options ignorecase and smartcase
* ?string - search backwards for a string
* n - repeat previous search
* N - repeat previous search in reverse direction
-----------------------------------------------------------------------------
### Find: Locate a character in the line
* f - move cursor to next occurrence of a character
* F - move cursor to previous occurrence of a character
* By default, ; repeats but previous f or F, however, I map ; to :
* t - moves cursor to before next occurrence in line
* T - moves cursor to before previous occurrence in line
-----------------------------------------------------------------------------
### Movement - recognize the pattern: operator-movement
* dw, cw, yw all operate on a word
* dh, ch, yh all operate on a character to the left
* dl, cl, yl all operate on a character to the right
* dj, cj, yj all operate on the line below
* dk, ck, yk all operate on the line above
* d$, c$, y$ - operate to end of line
* d%, c%, y% - find next (matching item) and operate to matching paren
* dfc, dtc - delete, etc, with find or to command
* d/c - delete, etc, with search command
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
### A new mode: COMMAND MODE
* Review:
* Insert mode: typed characters are inserted into file
* Replace mode: typed characters replace characters in file
* Normal mode: typed characters cause editing (eg delete)
* Command mode:
* User for commands that interact with the vim enviroment
* Commands are entered on a command line
* Bring up command line with :
* Exit with Enter or CTRL-c
-----------------------------------------------------------------------------
### File commands:
:w - write - save the current file
:w filename - save the current file under the name filename
:q - quit vim (only works if no unsaved changes)
:q! - quit vim and abandon any unsaved changes
:wq - save vim and quite
-----------------------------------------------------------------------------
### Bang commands:
* Bang is geekspeak for !
* Many vim commands have bang versions
* Bang versions are more dangerous than non-bang versions
* Example:
:w fn - will not execute if file fn already exists
:w! fn - will overwrite file fn if it already exists
-----------------------------------------------------------------------------
### More command line commands
:colorscheme - set colorscheme - example :colorscheme borland
:make - compile current file
:history - display history of commands
:highlight - display highlighting settings
:number - display number of current line
:ascii - display ascii of current character (also ga)
:version - display info on current version of vim
:echo $HOME $MYVIMRC $VIMRUNTIME
:sort
:hardcopy
:! - execute a shell command - example :!ls
:shell - start a new shell - exit new shell with exit
-----------------------------------------------------------------------------
### Command line command abbreviations
:colo delek
:mak :retab :his :hig
:nu :as :vers
:ec $HOME $MYVIMRC $VIMRUNTIME
:sor :ha :! :sh
-----------------------------------------------------------------------------
### Command line command completion
* Use TAB for command completion
-----------------------------------------------------------------------------
### Some options are modified using :set
:set ruler - display ruler in status line
:set noruler - do not display ruler in status line
:set ignorecase - search is not case sensitive
:set smartcase - case sensitive search only when upper case used
:set autoindent - automatically indent new line as previous
:set number - turn on line numbers
:set expandtab - replace tabs with spaces
:set smarttab - backspaces over expanded tabs
-----------------------------------------------------------------------------
### Some options have values
:set tabstop=4 - amount of indent when TAB is pressed
:set shiftwidth=4 - amount of indent on program indentation
-----------------------------------------------------------------------------
### Querying set options and values
:set tabstop
:set - view all options with non default values
:set all - view all options
:options - See below
-----------------------------------------------------------------------------
### Mapping - map a key sequence to a string of characters:
* My favorite mapping: :map ; :
* Maps a ; to a :
* Makes it easier to enter command mode
* Don't need shift
* Tradeoff: Can't use ; to repeat a find command
* :noremap : ; causes serious problems - not recommended
-----------------------------------------------------------------------------
### Help command
:help commandYouWantHelpFor
:help - by itself gives help on help
:h - abbreviation for help
* Splits the current window
* Use :q to exit help window
* Can do more :h commands while help window is open
* Can use TAB to get command completion
* Use CTRL-] to follow a help link
* Use CTRL-o to go back from a help link
-----------------------------------------------------------------------------
### Option window - an easy way to set vim options
:options
* Splits window
* Shows all vim options, organized by topic
* Allows setting vim options
* Example: Number option: (find it with /^number)
* Current is listed first, other values follow
* Exit with :q
-----------------------------------------------------------------------------
### vimrc - configuration startup file
* Commands in .vimrc are executed every time vim executes
* Where is it?
:echo $MYVIMRC
-----------------------------------------------------------------------------
### Substitute command:
:s/OldString/NewString/
* Operates on current line
:s/OldString/NewString/g - changes all occurrences in line
:%s/OldString/NewString/ - operates on whole file
* Can operate on specified range (see later)
-----------------------------------------------------------------------------
### Visual mode - Mark lines, character, blocks:
* v Marks a sequence of characters
* V Marks a sequence of lines
* CTRL-v Marks a block of characters
* Exit visual mode with ESC or v once or twice
-----------------------------------------------------------------------------
### Operating on text selected with visual mode
d delete (or x, D, X, with minor differences)
c change (or r, s, C, S, R)
y yank (or Y)
< > shift
p put
J join
U make Uppercase
u make lowercase
~ change case
I Block Insert
A Block Append
o - go to Other end
O - go to Other end (stays in same line in block mode)
gv - reselect up most recent selection
-----------------------------------------------------------------------------
### Configuring Vim for working with Java files:
* Put these options into the .vimrc (presumably)
* Basic options:
:filetype indent on " Turn on automatic indenting for specific file types
:filetype plugin on " Turn on loading plugins for specific file types
:syntax enable " Turn on syntax highlighting
* My personal preferences on tabs:
:set tabstop=4 " number of spaces that a TAB stands for
:set expandtab " insert spaces instead of tabs when tab key pressed
:set shiftwidth=4 " number of spaces for each autoindent
* Next time we will make java (and ada, etc) specific commands
-----------------------------------------------------------------------------
### Editing Java files
< - shift range left
> - shift range right
= - indent range
<< - shift current line left
>> - shift current line right
== - indent current line
:make - compile
-----------------------------------------------------------------------------
### Ranges
* Specify with visual - V
* Specify with line numbers
* Example
:1,$= will indent entire file
-----------------------------------------------------------------------------
### Command WINDOW
* Open with q:
* :map q; q: saves a keystroke
* Exit by CTRL-c CTRL-c (yes, that's twice)
* Exit by pressing enter on line with only :
* Contains a history of commands
* Can move around in window
* Commands can be edited
* Execute command by pressing enter on command
-----------------------------------------------------------------------------
### Splitting windows
:split filename
* move between windows with CTRL-w-CTRL-j and CTRL-w-CTRL-k
* quit or save window as needed
Dr. Okie's Home Page,
Last modified on