vim introductory training
DESCRIPTION
vim introductory training. Colin Graham [email protected]. please do not re-distribute without my permission. History. Vi – pronounced “vee eye” - PowerPoint PPT PresentationTRANSCRIPT
![Page 2: vim introductory training](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/7.jpg)
Modes - Command-line Mode
• For Ex commands:– :w[rite] – write– :q[uit] – quit
• For searching: / and ?
• For filtering: !
![Page 8: vim introductory training](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/8.jpg)
Modes – Visual Mode
• (not in vi)
• Movements highlight an area
• Non-movement command performs action on the highlighted section.
![Page 9: vim introductory training](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/17.jpg)
Marks
• Create a mark “m<char>” (i.e. ‘ma’ creates mark ‘a’)
• Jump back to mark a: ‘a
![Page 18: vim introductory training](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062314/568148c2550346895db5df0f/html5/thumbnails/21.jpg)
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