vim intro

advertisement
vim introductory training
Colin Graham
cagraham@gmail.com
please do not re-distribute without my permission
History
• Vi – pronounced “vee eye”
• “Although other stories exist, the true one tells that Vi was
originally written by Bill Joy in 1976. Bill took the sources
of ed and ex, two horrendous programs for Unix that try to
enable a human being to edit files, and created Vi. A truly
remarkable, and somewhat paradoxical, event. Read the
interview with Bill Joy for a more accurate history of Vi.”
[from http://www.thomer.com/vi/vi.html]
• Vi is available on virtually all Unix-type systems ;
Vim is available for most platforms
Pros & Cons
•
Learning curve
– Vi has a steep learning curve
– Once you master the basics, additional mastery comes naturally as you realize what
you want to do.
– Vim enhancements to plain vi follow the same basic structure
•
•
Vi (and vim) is fast!
Vi vs. Emacs
– Vi is always available on Unix ... Emacs isn’t
–
“...I observe that an effective Vi user simply edits files faster than Emacs people. Last but not
least, you don't need a third hand (or nose) to type impossible key combinations. Don't get me
wrong: Emacs is a great operating system---it lacks a good editor, though.” [from
http://www.thomer.com/vi/vi.html]
•
Vim vs. SlickEdit/CodeWright
– Vim is free
– Along with use of external programs, vim does it all
• I read that the last major caveat will soon be in the main-line vim distribution!
– IDE-type functionality is not automatic
•
Any of these are better than pico or notepad
Modal editor
• Vi uses the concept of modes
– Developed before there was a mouse
– Remains useful for efficiency because moving your
hand to the mouse is slow
– One of the most difficult aspects to get the hang of
• When in doubt, hit ‘esc’ (vim also shows your
current mode)
• See :help vim-modes
Modes – Normal Mode
• Normal mode
– I think of this as “movement mode”
– Here you can move around the files using the 4 basic
direction keys h-j-k-l
• side note: h-j-k-l was originally from some popular unix game
... before the days of arrow keys.
– You can move the screen, or the cursor position with
various keys and control-keys.
– Combine movements with numbers to shorten
keystrokes even further.
Modes – Insert Mode
• This is where you actually type the
characters (twice as many as the normal coder since you haven’t been using
the mouse )
• Go from Normal mode to Insert mode by
giving the command:
– "i", "I", "a", "A", "o", "O", "c", "C", "s" or S".
– (more on this later ...)
– [from :help mode-switching]
• Hit ‘esc’ to return to normal mode
Modes - Command-line Mode
• For Ex commands:
– :w[rite] – write
– :q[uit] – quit
• For searching: / and ?
• For filtering: !
Modes – Visual Mode
• (not in vi)
• Movements highlight an area
• Non-movement command performs action
on the highlighted section.
Basic Movement
• Basic cursor movement: h-j-k-l
– Let go of the arrow keys as soon as you can!
•
•
•
•
Move by a word: w ; b
Move by a big word: W ; B
Move by the end of a word: e ; ge
Move to matching: %
– ([{}])
– /* */
– #if, #ifdef, #else, #elif, #endif
Basic Movement - Scrolling
• Scroll by a full page: <cntrl>f ; <cntrl>b
– i.e. forward ; backward
• Scroll by a half page: <cntrl>d ; <cntrl>u
– i.e. down ; up
• Scroll by a single line: <cntrl>e ; <cntrl>y
– i.e. expose one more line ; y is close to u
Entering Insert Mode
•
•
•
•
•
•
•
•
•
•
Insert before current character “i”
Insert at the beginning of the line “I”
Append after current character “a”
Append at the end of the line “A”
Open a new line below “o”
Open a new line above “O”
Change “c” (plus a movement character, i.e. “cw”)
Change to end of line “C”
Substitute one character “s”
Change current line “cc” or Substitute current line “S”
Deletion
• Delete “d” (plus a movement character, i.e.
“dw”)
• Delete to end of line “D”
• Delete a single character “x”
• Delete single character before cursor “X”
• Join two lines together “J”
Simple copy-paste
• Yank text with “y” (i.e. “yw”)
• Paste or put text with “p”
• Put text before “P”
nCmd & Combos
•
•
•
•
•
•
dd – delete line
yy – yank line
2dd – delete 2 lines
3cw – change 3 words
4x – delete 4 characters
xp – swap two characters
Undo & Redo
• Undo last change “u”
– In vi, only a single level of undo
• Undo all changes on current line “U”
– In vi, only while you haven’t moved from the line
• Redo last undo “<cntrl>r”
– Not in vi
Search & substitute
•
•
•
•
Search for foo: “/foo”
Go to next occurrence: “n”
Go to previous occurrence: “p”
Replace all occurrences of foo with bar:
“:%s/foo/bar”
• Same as previous, only all occurrences on
the line and confirm each change:
“:%s/foo/bar/gc”
Marks
• Create a mark “m<char>” (i.e. ‘ma’ creates
mark ‘a’)
• Jump back to mark a: ‘a
Tags
•
•
•
•
•
Jump to a tag: “:ta TagName”
Jump to the tag under cursor “<cntrl>]”
Jump back from the tag “<cntrl>t”
This is used by vim help files
Use exuberant ctags to generate tags file for
your source tree
Ex commands
•
•
•
•
•
Edit a file: “:e foo”
Quit “:q”
Quit and discard changes “:q!”
Write-quit “:wq”
Read the contents of a file into the current
buffer: “:r foo”
Vim extras & demos
•
•
•
•
•
•
•
•
Syntax highlighting
Multiple level undo
Multiple windows (:split)
File-explorer plugin (built-in)
Taglist plugin
IDE-like setup (i.e. my “:Wings”)
Diff
Tags
Links
• Vim home: http://www.vim.org
• Exuberant ctags: http://ctags.sourceforge.net/
• Cygwin: http://www.cygwin.com
• http://directory.google.com/Top/Computers/Software/Editors/Vi/
• http://www.thomer.com/vi/vi.html
– http://docs.freebsd.org/44doc/usd/12.vi/paper.html
• Taglist plugin: http://www.geocities.com/yegappan/taglist/index.html
Download