Essentials for Day-to-day Use - Fall 2011


-----------------------------------------------------------------------------
### First, summarize commands covered by  vim tutor.  
    1. hjkl, :q!, x, i, esc, A, wq
        [modes, status line, case sensitive, muscle memory]

    2. de, dw, d$, d motion, count motion, d number motion, dd, u, U, ^r
    3. p, r, ce, cw, c$, c count motion, esc

    4. ^G, G, gg, number G, /, ?, n, N, ^o, ^i, %,
    4.4 :s/old/new/, :s/o/n/g, :#,#s/o/n/g, :%s/o/n/g, :%s/o/n/gc

    5. :!, :!ls, :w fname, v move :w fname, :r fname, :r !ls
    6. o, O, esc, a, R, v move y, yw

    6.5 :set ic, hlsearch, incsearch, hls, is, noic, /foo\c
    7. , :help, ^W^W, :q, vimrc, :anything^D, 

    Some of these commands are repeated below, with related commands

-----------------------------------------------------------------------------
### More on Help command
    :help commandYouWantHelpFor

    :help - by itself gives help on help
    :h - abbreviation for help

    * Help splits the current window

    * Use :q to exit the help window

    * Can do more :h commands while help window is open
    * Can use TAB to get help command completion

    * Use CTRL-] to follow a help link
    * Use CTRL-o to go back from a help link

-----------------------------------------------------------------------------
### Moving by words: w, b, e
    *  w - move forward one word
    *  b - move backward one word
    *  e - move to next end of word
    * ge - move to previous end of word

-----------------------------------------------------------------------------
### 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
    * CTRL-e - move screen up 
    * CTRL-y - move screen back
    * CTRL-u - move half screen up 
    * CTRL-d - move half screen back

-----------------------------------------------------------------------------
### Search
    * /pattern - search for pattern
    * ?pattern - search for pattern backwards
    * n, N - next occurence in same or opposite direction
   
-----------------------------------------------------------------------------
### Substitute
    * :s/old/new/ - substitute new for old
    * :%s/old/new/ - on all lines
    * :s/old/new/g - all occurrences on a line
    * :%s/old/new/g - all occurrences on all lines
    * :%s/old/new/cg - all occurrences on all lines, with confirmation

-----------------------------------------------------------------------------
### Undo, Redo, repeat
    * u - undo
    * CTRL-r - redo

    * . - repeats the most recent change  command

-----------------------------------------------------------------------------
### Ways to start insert mode and INSERT characters
    *  o - open new line BELOW
    *  O - open new line ABOVE
       
    *  i - insert BEFORE cursor location [shifting chars to right]
    *  a - insert AFTER  cursor location [shifting chars to right]
       
    *  s - insert AT cursor (replacing current char and shifting chars to right)
       
    *  A - insert at end of current line [adding character to the line]
       
    *  I - insert at beginning of current line
         - But after initial tab characters

    * gI - insert at beginning of current line
         - But before initial white space characters

-----------------------------------------------------------------------------
### The g command
    * Many commands have a g version
        *  e - go to next end of a word
        * ge - go to previous end of a word

        *  J - join 2 lines with one blank in between
        * gJ - join 2 lines without adding a blank

        *  I - Insert at beginning of line (after initial tab)
        * gI - Insert at beginning of line (before initial tab)

    * Other g commands: 
        * ga - show ascii of current character
        * gd - go to a declaration of identifier under curser
        * gg - go to top of file

    * A related command:
	* ^G - file location and status appears on status line
    
-----------------------------------------------------------------------------
### Change a word, line, or remainder of a line in insert mode
    * cw - change word: delete to end of current word and enter insert mode
    * c$ - change word: delete to end of 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

-----------------------------------------------------------------------------
### Delete
    * x - delete character
    * de - delete to end of word [leaves blank]
    * dw - delete word [deletes blank]
    * d$ - delete to end of line
    * dd - delete line
    * Use with a count

-----------------------------------------------------------------------------
### Change and delete text-objects
    * ci( - change inner contents of parens (this is an example)
    * ca( - change outer contents of parens (this is an example)
    * Works for [("'`
    * cit and cat - html tabs  example of this 
    * cip and cis - change paragraphs and sentences
    * cw - word delimited by non-letter
    * cW - word delimited by space

-----------------------------------------------------------------------------
### Simple Cut and Paste
    * Use delete and paste
    * Or yank and paste
    * ^X, ^C, and ^V don't cut and paste (have other uses)
    * ^X, ^C, and ^V cut and paste can be enabled

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

-----------------------------------------------------------------------------
### Learn this shortcut: swap two characters or lines
    * ddp - swap two lines
    * xp - swap two characters

-----------------------------------------------------------------------------
### Yank - Copy character, word, line into a buffer
    * Y - Copy current line into buffer
    * yy - Same

-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
### A new mode: COMMAND MODE
    * Review of some other modes:
        * 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 vim command line
        * Bring up command line with :
        * Abbreviations and tab are a help with remembering
        * Exit with Enter or CTRL-c

-----------------------------------------------------------------------------
### Simple examples:
    :number
    :set number
    :Set ignorecase - search for set and Set
    :Set noignorecase - search for set and Set
    :Set smartcase - search for set and Set
    :set ruler
    :set 
    :set all

-----------------------------------------------------------------------------
### File commands:
    :w - write - save the current file
    :w filename - save the current file under the name filename
        - and stay in same file

    :saveas filename - save the current file under the name filename
        and edit filename

    :q - quit  vim (only works if no unsaved changes)
    :q! - quit vim and abandon any unsaved changes

    :wq - save vim and quit

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

	* Bang also used with os commands:
		:!ls - ls in os
		:!pwd - print working directory

-----------------------------------------------------------------------------
### Mapping - map a key sequence to a string of characters:
    * Not a basic command, but useful
    * My favorite mapping:  :map ; :
	* :map q; q:
    * Maps a ; to a :
    * Makes it easier to enter command mode
        * Don't need shift

    * Tradeoff: Can't use ; to repeat a find command
        * Warning: dont do this: :noremap : ; causes problems 
        * Can noremap gf ; - this causes gf to repeat last find command

-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
### vimrc - configuration startup file
    * Commands in .vimrc are executed every time vim executes
    * .vimrc stands for vim run commands
    * More than one .vimrc can exist: system and user

    * Where is user .virmc, if it exists?
        Windows or Linux: $HOME/_vimrc
        Linux: $HOME/.vimrc

        Check with :echo $MYVIMRC 
        Check with :scriptnames

    * Where is the system .vimrc?
        Windows: $VIM/vimrc
        Check with :echo $VIM 
-----------------------------------------------------------------------------
### Example a very minimal _vimrc 
----------------------------------------------------------------------------
### Configuring Vim for working with Java files:
    * We will look at
        * Configuring the .virmc file
        * Commands that are useful for editing Java files
        * Compiling from within vim
        * Setting up abbreviations (using ftplugin files)
        * Automatic command completion

-----------------------------------------------------------------------------
### Configuring Vim for working with Java files:
    * Put these options into the .vimrc 

    * 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 preferences on searching:
        :set ignorecase      " ignore case when searching
        :set smartcase       " But, consider case if uppercase is used in search

    * My 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

-----------------------------------------------------------------------------
### Line Ranges
	* V - marks beginning of a range
	* Then use movement to mark range
	* Then do a command on the range:
		* cut/paste
		* shift (see below)
	* Other range types are possible

-----------------------------------------------------------------------------
### Editing Java files 
    < - shift range left
    > - shift range right 
    =    - indent range

    << - shift current line left
    >> - shift current line right
    == - indent current line

-----------------------------------------------------------------------------
### Why is my paste messed up
    * When pasting programs, autoindent indents too much
    * Do the following to solve this problem:
        * :set paste - this will turn off autoindent, and some other options
        * do the paste
        * :set nopaste - this will turns autoindent and other options back on

-----------------------------------------------------------------------------
### Set up make command to do compile
    * Put this command in your .vimrc
        :set makeprg=javac\ %

    * the \ escapes the space 
    * the % is the file name, with extension

    * Or put the command in java.vim in your ftplugin directory (see below)

-----------------------------------------------------------------------------
### Automatic compilation
    * Now compile with :make
        * press enter to return to file
        * notice that cursor is on first error
        * Get to current, next, previous errors with :cc, :cn, :cp
        * Open error window with :copen [we haven't yet discussed split windows]

-----------------------------------------------------------------------------
MORE ADVANCED CONFIGURATION: IF WE HAVE TIME
-----------------------------------------------------------------------------
### Personal  Configuration for Java programs 
    * More advanced, personalized configuration uses a ftplugin file 
    * Must create ftplugin directory containing java.vim

-----------------------------------------------------------------------------
### Your Own Plugins and FT Plugins - On Linux
    * In /home/username (ie $HOME, your H: drive) 
		* Make directory .vim (if not already there)
		* Create directory .vim/ftplugin
		* In this directory, create java.vim

	* In java.vim, put commands such as 

        * :set makeprg=javac\ %

        * :ab psvm public static void main(String[] args){}

        * :ab fori public static void main(String[] args){}

	* Let's edit a Java file ...

-----------------------------------------------------------------------------
### Where to put your plugin directory with  windows at home
    * windows: c:\software\vim\vimfiles\ftplugin
    * or wherever you installed vim

### How to make .vim accessible from windows on campus
    * .vim may be accessible from windows machines by default (ie from $HOME)
    * If not, try this:
        * In H: drive - make a shortcut to .vim called vimfiles
        * Caution: not thoroughly tested


Last modified on
Dr. Okie's Home Page,