The VIM Text Editor

Dan Price

Most users who are familiar with the command line have heard of or had some experience with vi. In this tutorial, I presented the most popular and advanced of the vi clones: vim. The tutorial consisted of a slow introduction to vim, with an emphasis on *how* to learn the editor. A more advanced discussion on effective editing (with a case study in C programming) followed this talk.

VIM for Beginners

Why Learn VIM?

  • Powerful
  • Cross-platform
  • Console or GUI
  • Encourages touch typing
  • Scriptable
  • Fun

History

  • vi written by Bill Joy (founded Sun Microsystems)
  • vim written by Bram Moolenaar (now works for Google)
  • Latest Version (7) released May 2006

Basic Movement

  • h = left
  • j = up
  • k = down
  • l = right

Modal Editor

  • Modes
    • Normal (Default)
    • Insert
    • Command
    • Visual
    • Others. . .
  • To escape to normal mode (Esc) (or better yet ctrl-[ )

Normal Mode

  • Navigate the document
  • Search
  • Copy, paste, delete text

Insert Mode

  • Most other editors are always in this mode

Command Mode

  • Save, load, etc.
  • Set options
  • Special Functions

Visual Mode

  • Select text to copy, delete, edit, etc.

Some essential commands

  • :write (:w)
    • saves your document
  • :quit (:q)
    • quits vim
    • fails if current document isn't saved – to force, use :quit! (:q!)
  • :wq
    • save and quit

Some essential keys (normal mode) - x

  Delete character under cursor

- dd

  Delete current line

The most important keys for beginners - Esc ( ctrl-[ )

  Goes back to normal mode, you can't hit this too many times

- u

  undo

- :help

  Vim is exceptionally well-documented

How should I learn vim? - vimtutor

  This is a well written introduction to all the essential features of vim - I
  encourage everyone to take a look (simply type vimtutor in your console).

- Read the help docs

  :help

- Force yourself to use it - If something feels tedious, you are probably doing it wrong – look for a

better way

More on Keys - In vim, almost every key is mapped

  type carefully – a mis-typed key can cause frustration

- Avoid remapping keys that are already used

Advanced editing


- Options

  vimrc
      .vimrc is loaded upon starting vim - ideally is located in your home
      directory
  Syntax highlighting
      :syntax on
      :syntax off
  Line numbering
      :set number
  To turn off just about any option, prepend the option name with "no"
  Example:
      :set nonumber

- Motions

  Words
      Watch as I navigate through this sentence by using the 'w', 'b', and 'e'
      keys.  There are also uppercase versions of these characters, which
      navigate through whitespace delineated
      words(for_example_a_'W'_would_skip_over_all_of_this).
  Sentences
      Sentences.  Can be navigated. by the ')' key.
      
  Paragraphs
      Sentences.  Can be navigated. by the '}' key.
  Character Searches
      f   navigate to next typed character
      t   navigate until next typed character
      
      Try f( and t( on the line below.  Note the difference.  Try typing dt)
      when on the first "
          printf("Starting up my amazing program\n");
  Text Objects
      What is the best way to delete a sentence?  For example, If I were to
      ask you to delete the sentence that you are currently reading, what is
      the best way to do it?  It might surprise you how simple it can be.
      (Try typing das when over one of the sentences above)
      <A>Before some tag</A><Some_tag>The value of said tag</Some_tag><A>After
      some tag.</A>
      (Try typing dat when over one of the tags above)
      "Inside of quotes"  (also works for parentheses)
      (Try typing di" when inside the quotes above)

- Histories

  Jump histories
      :jumps
      Ctrl-o  = reverse through this list  
      Ctrl-i  = forward through this list
  Change histories
      :changes
      u  = undo
      Ctrl-r  = redo
  Command history
      q:
  search history
      q/

- Formatting

  
  gq - beautifies the selection/motion to which it is applied
  =  - indents the selection/motion to which it is applied

- Word Completion

  Ctrl-n     completes a word
  Ctrl-x-f   completes a file name (in the current directory)
tux