21
vim introductory training Colin Graham [email protected] please do not re-distribute without my permission

vim introductory training

  • Upload
    malo

  • View
    37

  • Download
    0

Embed Size (px)

DESCRIPTION

vim introductory training. Colin Graham [email protected]. please do not re-distribute without my permission. History. Vi – pronounced “vee eye” - PowerPoint PPT Presentation

Citation preview

Page 1: vim introductory training

vim introductory training

Colin [email protected]

please do not re-distribute without my permission

Page 2: vim introductory training

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

Page 3: vim introductory training

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

Page 4: vim introductory training

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

Page 5: vim introductory training

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.

Page 6: vim introductory training

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

Page 7: vim introductory training

Modes - Command-line Mode

• For Ex commands:– :w[rite] – write– :q[uit] – quit

• For searching: / and ?

• For filtering: !

Page 8: vim introductory training

Modes – Visual Mode

• (not in vi)

• Movements highlight an area

• Non-movement command performs action on the highlighted section.

Page 9: vim introductory training

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

Page 10: vim introductory training

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

Page 11: vim introductory training

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”

Page 12: vim introductory training

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”

Page 13: vim introductory training

Simple copy-paste

• Yank text with “y” (i.e. “yw”)

• Paste or put text with “p”

• Put text before “P”

Page 14: vim introductory training

nCmd & Combos

• dd – delete line

• yy – yank line

• 2dd – delete 2 lines

• 3cw – change 3 words

• 4x – delete 4 characters

• xp – swap two characters

Page 15: vim introductory training

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

Page 16: vim introductory training

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”

Page 17: vim introductory training

Marks

• Create a mark “m<char>” (i.e. ‘ma’ creates mark ‘a’)

• Jump back to mark a: ‘a

Page 18: vim introductory training

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

Page 19: vim introductory training

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”

Page 20: vim introductory training

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

Page 21: vim introductory training

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