Using Vim - Beyond the Basics

-----------------------------------------------------------------------------
### My Favorite Keyboard Mapping {{{1
    * :map ; :
-----------------------------------------------------------------------------
### Learn to use windows {{{1
    * :split file
    * :vsplit file
    * movement: ctrl-w ctrl-hjkl
    * -w--hjkl - move between windows
    * -w-+, -w-- - increase, decrease size of window
    * :q - quit a window, :qa - quit all windows (careful)
    * :only - make this the only window
-----------------------------------------------------------------------------
### Learn Help {{{1
    * :help name, eg :h split 
    * -D or  gives more information
    * :help help
    * :help

    * exit help with :q
    * f3 may enter help

    * :h number vs :h 'number'

    * -], o - jump to help on tag, jump back
    * -] - search for help on any word in help

    * :helpgrep - :cn, :cp, :cc, :cope 

    * Or, use google!
-----------------------------------------------------------------------------
### Learn Abbreviations and Use Tab{{{1
-----------------------------------------------------------------------------
### Learn the Command Window {{{1
    * q:
    * I use :map q; q:
    * Completion with ctrl-X ctrl-P

    * Bring line down with ctrl-C

    * Exit with :quit, :,  ctrl-C ctrl-C
-----------------------------------------------------------------------------
### Learn the Options Window {{{1
-----------------------------------------------------------------------------
### Learn to set and query options {{{1
    * Option forms: toggle, number, string
    * Query
        * :set ruler?
    * Toggle True/False
        * :set ruler
        * :set noruler
    * Toggle Invert
        * :set invruler
        * :set ruler!
    * Set Number
        * :set history=50
        * :set history+=10
    * Set String
        * :set makeprg=gnatmake\ %
        * :set fo+=a
-----------------------------------------------------------------------------
### Use marks {{{1
    * mark with ma, ..., mZ
    * go to mark with 'a, `a, 
    * go to default mark (ie previous location) with ''
    * Query marks with :marks
-----------------------------------------------------------------------------
### Use Registers {{{1
    * can delete and yank into registers and put from registers
        * Denote registers using " and a letter
        * Examples - "aY   "bdw  "ap  "bp - replace register
        * Examples - "AY   "Bdw  "Ap  "Bp - append to register

    * "" is the unnamed register
        * it contains text from most recent delete or yank

    * numbered registers contain previous values of "" (see help)

    * "% contains file name
    * ": contains most recent command line command
    * "/ contains most recent search
    * "*, "+, "~ contain clipboards in gui

    * :registers lists registers and their contents

-----------------------------------------------------------------------------
### Recognize that most things can be queried {{{1
    * -g
    * :version
    * :set  - not default
    * :set all
    * :scriptnames
    * :function
    * :function FunctionName
    * :abbreviate
    * :changes
    * :command
    * :highlight
    * :registers
    * :marks
    * :args     -list of files currently open
    * :map
    * :WhereFrom ???
-----------------------------------------------------------------------------
### Learn to Move {{{1
    * ctrl-F, B, U, D
    * ctrl-Y, E
    * H, M, L, nH, nL, %
    * w, b, e, W, B, E
    * {} - paragraph)
    * () - sentence
-----------------------------------------------------------------------------
### Use counts {{{1
-----------------------------------------------------------------------------
### Use Movement with d and c {{{1
-----------------------------------------------------------------------------
### Use . commands (ie dot commands) {{{1
-----------------------------------------------------------------------------
### Use undo and redo {{{1
    * u, -r - undo and redo, multiple times
    * backup file is saved for recovery on crashed sessions
    * undo branches
    * : changes
-----------------------------------------------------------------------------
### Learn to use the visual modes {{{1
    * Define area/range with 
        * v - visual, 
        * V - visual line 
        * -v, - visual block 

    * called visual mode 
    * change, delete, indent area

    * changing one kind to another - use another command

    * o, O - move to other corner, across, diagonal 
    * gv - restore previous area
-----------------------------------------------------------------------------
### Substitute {{{1
    * :s/oldstring/newstring/g    ---- all occurrences on line
    * :%s/oldstring/newstring/    ---- operate on entire file
-----------------------------------------------------------------------------
### Use g commands {{{1
    * gI
    * g~
    * etc
-----------------------------------------------------------------------------
### Search Command {{{1
    * /abc - searches for abc and puts cursor on a
    * ?abc - searches backwards for abc
    * end with enter
    * n and N repeat previous search in same or reverse direction

    * /wxyz/2 - search for wxyz and put cursor on second line below
    * /wxyz/-1 - search for wxyz and put cursor on line above

    * /abc/b+1 - puts cursor on b

    * /abc/e - puts cursor on c
    * /abc/e+1 - puts cursor on character after c

    * q/ - search history window


    * * - search for word under cursor

    * :set ic - ignorecase - search ignores case
    * :set smartcase - ignore ignorecase if search for upper case

    * :set incsearch - highlight partial matches as you type /string
    * :nohlsearch to turn off current search highlighting

    * :set nohlsearch to turn off highlight search

    * q/ to get search history
-----------------------------------------------------------------------------
### Folding {{{1
    * How folds work:
        * Folds can be created in a file
        * Folds can be open or closed
        * Existing and being open or closed are independent
    * zf - creates a fold and immediately closes it

    * zo - opens the fold under the cursor
    * zc - closes the fold under the cursor

    * zM - more folding - close all folds
    * zL - less folding - open all folds

    * :set foldmethod ... - automatically creates folds
        * :set foldmethod indent - creates folds based on indentation
        * :set foldmethod marker - creates folds based special marker
        * :set foldmethod manual - only have user created folds (with zf)

    * :set foldlevel=n
    * z looks like a fold
-----------------------------------------------------------------------------
### Buffers {{{1
    * :buffers
    * :ls
    * :buffer n (:buff)
    * :sb n (split buffer)
    * :vi #, :sb #
-----------------------------------------------------------------------------
### Tabbed Editing {{{1
    * :tabe filename
    * :tabe
    * :tabdo
    * :tabmove N - put current tab in position N, 0 to make it the first tab
    * :tabs
    * gt, gT - go forward, backward one tab
    * n gt - go to tab n
-----------------------------------------------------------------------------
### Directory Browser {{{1
    * vi mydir or :split
    * enter opens
    * o opens in split window
    * vfilebrowser is a nice plugin
-----------------------------------------------------------------------------
### Compiling a Java Program {{{1
    * :set makeprg? - Check current value of makeprg
    * :set makeprg=javac\ %
        * \ escapes the blank
        * % represents the name of the file
        * later we will put this into a plugin to make it automatic

    * Now compile with :make
        * press enter to return to file
        * notice that cursor is on first error
        * Get to errors with :cc, :cn, :cp, :copen
-----------------------------------------------------------------------------
### Review of Error Window {{{1
    * :make - compiles
        * :cc, :cn, :cp - to see current, next, prev errors
        * :copen to open error window, :q to quit
-----------------------------------------------------------------------------
### Configuration: .vimrc {{{1
    * Commands in .vimrc are executed on startup
    * :echo $MYVIMRC
-----------------------------------------------------------------------------
### Commands to set up for programs {{{1
    * filetype plugin on - enable loading plugins for file types
    * filetype indent on - indentation based on file type
    * syntax enable   - syntax coloring
-----------------------------------------------------------------------------
### More Configuration - System Plugins and FT Plugins ... {{{1
    * echo $VIM
    * echo $VIMRUNTIME
    * vi $VIMRUNTIME
    * plugin, compiler, ftplugin, syntax
-----------------------------------------------------------------------------
### Your Own Plugins and FT Plugins - On Linux{{{1
    * In /home/username (ie your H: drive) 

    * Make directory .vim (if not already there)

    * Create directory .vim/ftplugins

    * In this directory, create java.vim

    * In java.vim, put commands such as 
        * :set makeprg=javac\ %
        * :set autowrite - automatically save the current file before doing :make

        * :ab psvm public static void main(String[] args){}
        * :ab fori public static void main(String[] args){}
-----------------------------------------------------------------------------
### Where to put your plugin directory with  windows at home{{{1
    * windows: c:\software\vim\vimfiles\ftplugin
    * or wherever you installed vim

### How to make .vim accessible from windows on campus{{{1
    * In H: drive - make a shortcut to .vim called vimfiles
    * Caution: not thoroughly tested
-----------------------------------------------------------------------------
### Commands for Ada {{{1
    * set makeprg=gnatmake\ %:r 
    * Warning: in vim 71, the default ada mode is broken
        - see week 1 of 320 notes to see how to fix it
      
    * ab fori for i in 1 .. 1 loop end loop;
    * ab aito ada.integer_text_io; use ada.integer_text_io; 
    * ab ato ada.text_io; use ada.text_io; 
      
    * ab proc procedure foo is beginend foo;
-----------------------------------------------------------------------------
### Make for C {{{1
        * C: set makeprg=gcc\ -o\ %:r\ %
-----------------------------------------------------------------------------
### Filetypes {{{1
    * Filetype is set by extension: 
    * :set filetype? - to query current filetype
    * :filetype detect - detects file type again
-----------------------------------------------------------------------------
### Runtimepath {{{1
    * Where does vim look for plugins?
    * :set runtimepath?

-----------------------------------------------------------------------------
### Marks {{{1
    * Indentation automatic if filetype detected from extension
    * Indent with: = for range, == for line

    * Explicit shift with: <, >, <<, >>
    * Explicit shift with: <, >, <<, >>
-----------------------------------------------------------------------------
### Completion {{{1
    * Use ctrl-x ctrl-N to get word completion 
    * Plugin supertab causes tab to get word completion
    * :help completion - Lots of other things can be completed
    * omnicompletion and tags are useful ...
-----------------------------------------------------------------------------
### Comparing files: vimdiff, diffsplit, scrollbind {{{1
    * vimdiff  fn1 fn2 fn... from command line
    * :diffsplit fn - split and compare files
    * :set scrollbind - causes this file to follow scrolling of another
-----------------------------------------------------------------------------
### Let's look at a .vimrc {{{1
-----------------------------------------------------------------------------
### Let's look at a script {{{1
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
OTHER STUFF
-----------------------------------------------------------------------------
### INDENT mode indentation {{{1
    * INDENT mode indent and unindent with ctrl-T, ctrl-D, 0ctrl-D
-----------------------------------------------------------------------------
### Combining motion and changing: {{{1
    * operator motion: cw, dw, cb, db, c0, c$
-----------------------------------------------------------------------------
### Getting information - Saving results {{{1
    * How to save the information from a query
    * :redir[!] > myfile
        * now you can edit myfile
        * can redirect into register

    * :vfile=myfile
        * now you can edit myfile
        * do some commands
        * :vfile=
        * :vfile=newfile   - check the help
-----------------------------------------------------------------------------
### Vim info {{{1
    * viminfo Saves information about sessions
-----------------------------------------------------------------------------
### Multiple files in one session: args {{{1
    * vim *.adb - edits all adb files in one session
        * each one becomes an argument
    * :args - list current arguments
    * :prev, :next
    * :argdo command - do command to each argument
    * :args foo* - set arguments
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
### spell {{{1
    * new to vim 7
    * :set spell - turn on spell checking
    * ]s, [s - find next,prev misspelled word
        * [] is used frequently
    * z= - suggest spellings
    * zg - add current word to list of good words
    * Note:
        * when syntax=html must have :set syntax spell toplevel
        * so, I put :syn spell toplevel into ~/.vim/syntax/html.vim
-----------------------------------------------------------------------------
### shell like commands: {{{1
    * :pwd, :cd
    * :!
    * :sort - sort a range
    * :grep
    * :r!
    * :make
    * :echo &shell
    * :echo &shell
    * :echo &runtimepath
    * :echo &runtimepath
    * :echo $RUNTIMEPATH
-----------------------------------------------------------------------------
### Backup files {{{1
    * Editing file creates a backup file: 
        * vim myfile.fn creates .myfile.fn.swp 
    * used to tell if a file is being edited
    * used for recovery of crashed sessions
    * :set backup causes backup file to be kept after editing 
-----------------------------------------------------------------------------
### Printing {{{1
    * :ha prints
    * :ha on windows brings up dialog
    * :set printoptions 
    * I define needed print commands, eg :DA205
    * Printing can be complex
-----------------------------------------------------------------------------
### More cool things you can do: {{{1
    * :Calendar
    * binary edit
    * :mksession to save the session, then vim -S Session.vim
    * :tohtml - a nice plugin - creates html file of file with current coloring
    * clean up newlines between different OS - see help filemodes
        * or use dos2unix!
    * record a macro:
        * q1 - start recording in buffer 1
        * q - stop recording 
        * @1 - play back recording
    *  in a source file gives html syntax
    *  This modeline - tells vim how to start up
    * status line is highly configurable
-----------------------------------------------------------------------------
### More cool things you can do: {{{1
    * edit zip and jar files 
    * decompile java byte code - plugin required (and a decompiler - try jad)
    * reverse input
-----------------------------------------------------------------------------
### Automatic justification {{{1
    * Justify a paragraph: gq} 
    * auto justify:
        - :set fo+=a  "formatoption+=a automatic formatting
            - memory intensive since it remembers each change

        - :set fo+=2  "formatoption+=2 format based on second line

        - :set fo+=n  "formatoption+=n format numbered lists
-----------------------------------------------------------------------------
### What you can't do {{{1
    * console - you can't get a console in a window - by design
-----------------------------------------------------------------------------
### ruby {{{1
    * :ruby print 3
    * :ruby 3.updo(10){|i| print i}
    * :rubydo print $_.reverse operates on a range
    * :ruby VIM::Buffer.current.append(1, VIM::Buffer.current[1].reverse)
    * $curwin, $curbuf
    * must be ruby enabled (see :version for +ruby)
    * python and perl also available
-----------------------------------------------------------------------------
### scripts {{{1
    * variables
    * let
    * loops and ifs
    * com and functions
        * begin with capital letters
    * comments
    * echo
    * script shell
    * shell
-----------------------------------------------------------------------------
### Entering a Control Character {{{1
    * Enter control character, eg ^X, with -V--X
-----------------------------------------------------------------------------
### The ascii value {{{1
    * ga displays the ascii value of the character under the cursor
-----------------------------------------------------------------------------
### More commands {{{1
    * number
        * :number        number as command 
        * :set number    number as option 
    * ga or ascii - display ascii value
    * :verbose
    * :colorscheme - set a colorscheme
    * :function name  - display a function
    * :function - define a function
-----------------------------------------------------------------------------
### Modes summary  {{{1
    * normal
    * insert: exit insert mode with esc
    * visual
    * select
    * replace
    * command line
    * ex: enter with gQ, exit with vi
    * see :help mode-switching
-----------------------------------------------------------------------------
### New things - Not yet integrated into the notes: {{{1
    * Warning: watch out for searches that involve regexp characters
    :ls, :bu, :sbuff
    :options
    visual mode: o, gv
    :% range
    :m/
    * finds next occurrence of current word
    q macro definitions can be put in a script (eg .vimrc)
    word completion (supertab)
    role of shiftwidth in indenting
    g, and g;
    :changes and the changelist
    :set cursorline, cursorcolumn
    :foldmethod=marker
    Modelines:
        
        
    autocmd
    :list
    :set list/nolist
    :listchars
    vimsh
    how vim is implemented
    :NoMatchParen - turn off matching paren highlighting
    ctx
    difference between w and W
    g~$, guib
    text objects: {i,a}{wWspbB<[ >
    g commands
    :infercase
    * :echo $RUNTIMEPATH
    swap character: xp; lines: ddp; words: dwep or dWEp
    recover file after crashed session and swap files
    Things you can define:
        map, abbrev, command, function, macro, variable
    From one run to the next: .viminfo, return to previous line in vimrc:
    map  ;set invspell;set spell?
    map  ;set hls!;set hls?
    regexp:
        ^/$ only have special meaning at beginning/end of line
        Remove ^M at end of line: 
            %s/
$//
            Press cntl-v and cntl-m to enter cntl-m
        Add {{{1 to end of lines that have ###: 
            %s/\(###.*$\)/\1 {{{1/
        Locate where " " and " " appear at the start of 2 successive lines
            /^ <\/tr>\n /
        Make HTML Comments from lines when " " and " " appear at the start of 2 successive lines
            %s/\(^ <\/tr>\n \)//
        Remove all lines that contain only white space
            %s/^\s*$\n//
        Make all lines that start with Q: into html list items
            %s/^Q: \(.*\)$/
  • Q: \1<\/li>/ Search for lines that start with Q: and DON'T end with ? ^Q: .*[^\?]$/ Change all single line list elements to bold single line list elements %s-^
  • \(.*\)
  • -
  • \1
  • - How to make this work over multiple lines??

    Dr. Okie's Home Page,
    Last modified on