Download presentation
Presentation is loading. Please wait.
1
Program Development with Vim
2
Brief History ed – original (line-oriented) editor of Unix
ex – superset of ed commands (Joy) vi – visual (full screen) interface for ex (Joy) circa 1976 vim – Vi improved
3
Why Use vi/Vim? Part of the standard Unix distribution
Always there Available on just about all other platforms Powerful Programmable/configurable Free
4
Why Use vi/Vim? It’s a programmer’s editor
For programmers, by programmers Lets you think like a programmer
5
Some Philosophy Modal editor
insert: typed text becomes part of document normal: typed chars are editing commands Occasionally called command mode (ugh) Allows user to edit without removing hands from the keyboard’s home row - feature? unintended consequence of ‘dumb terminal’ keyboard?
6
Some More Philosophy As a programmer’s editor, orthogonality plays a big role in the design Combining two features usually works… … and works the way you’d expect it to
7
A Suggestion Toss out the GUI Toss out the mouse Toss out the toolbar
Toss out the menu bar
8
Before Anything Else… Help
:help – enters the help system :help <topic> - positions the help system at the topic It’s not always that simple– read the beginning of the help pages The help system is a text document And can thus be somewhat navigated using vim
9
Before There was Hypertext
… there was the vi help system (I think) Positioning the cursor over a highlighted topic and pressing <Ctl-]> takes you to the topic <Ctl-O> / <Ctl-T> takes you back
10
And Then There’s Vimtutor
Go to the installation directory and run vimtutor.bat
11
Normal Mode Movement h, j, k, l – left, down, up, right
0, ^, $ - beginning, 1st nonblank, end of line w – forward one word b – backward one word <n>G – go to line n G – go to last line gg – go to first line <Ctl-F>, <Ctl-B> - forward back one page
12
Search within Line f<c> – forward (on)to character c
F<c> – backward (on)to character c t<c> – forward (up) to character c T<c> – backward (up) to character c
14
Other Normal Mode Commands
i, a – enter insert mode (insert/append) exit via <Esc> x – delete char r – replace char w – write file ZZ – write file and quit vi
15
Adding Count <n><command> - perform command n times 10x 5w
10j
16
Movement-Based Commands
d – delete text of movement dw – delete word dt) – delete text until ) c – change text of movement (insert mode) cw – change word y – yank (copy) text of movement y$ – yank to end of line
17
The General Form <n><action><n><movement> 10dw
c2w 2cw
18
Doubles and Caps Doubling action’s character often makes it operate on the entire line dd, yy Capitalizing action often goes to end of line (or some other ‘enhanced’ action) C, D R – replace multiple chars (enters insert mode) Y – delete to end of line
19
Repeating Commands . (period) - repeats the last command performed in normal mode. I use it for inserting HTML tags in my lecture notes i<code><Esc> Then I move to the next spot and press . … and so on
20
Marking Locations m<letter> - Marking a location
'<letter> (single quote) - go to the beginning of the line of marked location `<letter> (backtick) - go to line and column of marked location Uppercase letter marks work between files
21
Searching /<pattern>/ Wraps around at end/beginning of file
/main/ Will incrementally search as pattern is typed ?<pattern? - searches backwards //, ?? - repeats search Wraps around at end/beginning of file
22
Command (Line) Mode Commands requiring more user input than a couple of characters Commands operating on multiple lines Entered by typing : Subsequent entry occurs on bottom line General form: <line range><action>… Line range is of form <start>,<end>
23
Some Commands wq – write and quit (like ZZ) :<n> - go to line n
:<range>d – delete range of lines :10,20d :<range>s/<patt>/<replacement>/gc :5,10s/hello/goodbye/ Can use ‘any’ character as delimiter wq – write and quit (like ZZ)
24
Line Specifications Line # Symbolic Expression Search Marks ., $, %
.+1, $-2 Search /main/ Marks 'a
25
Not Everyone Can Be Perfect
u – undo <Ctl>-R – redo
26
Repeating Commands Cursor keys work in command line mode
There’s also a command line history you can search and edit For another day
27
Seeing is Believing – Visual Mode
Highlights selected text From normal mode v – plain visual mode Highlights on a character basis <Shift-V> - linewise visual mode Highlights on a line basis <Ctl-V> - block visual mode Highlights on a column (rectangular) basic
28
Reg\(ular \)\?Ex\(pression\)\? /Search\(ing\|es\)/
Provides search pattern based on regular expressions Added bonus: you get to learn regex’s
29
Regular Expressions Patterns used for search (and replacement)
Uses metacharacters for pattern specification Perl is the standard these days Vim’ is similar but not identical Filename wildcards on LSD
30
Basic categories . (period) – any character except newline
\s – any whitespace character \d – digit \a, \l, \u – alpha, lowercase, uppercase Uppercase flips parity \S – non-whitespace \D – non-digit
31
Anchors ^, $ - beginning / end of line \<, \> - word boundaries
32
Character Ranges [aeiou] – matches any of the specified characters
[0-9] – matches any character from 0 through 9 (in the underlying character set) Above two canbe combined [A-Za-z0-9_] [^A-Z] – matches any character except A through Z
33
Grouping \(, \) Used for any precedence issues
But more importantly allow subsequent retrieval of matched text (during replacement) These are called backreferences
34
Alternation <pattern1> \| <pattern2>
Matches pattern1 OR pattern2 \(int | double\)
35
Replacement Has its own set of meta-characters to allow retrieval
&, \0 – all the text that matched the pattern \1, \2, … - the text of the n’th matched group
36
Buffers – Multiple Files
:set hidden – allows you to move freely between buffers :e <file> - brings a new file into the editor Placed into its own buffer :buffers – displays info about the buffers :b<n> - edits buffer n
37
Multiple Windows :split – horizontal window split
:vsplit – vertical window split <Ctl-W><action> performs window-related action h, j, k, l – moves to window in that direction There are resizing, and moving actions as well :q (ZZ) closes window
38
Programming Aids Vim can just about be transformed into a full IDE
We’ll look at a few simple features Indentation Delimiter matching/navigating Running commands from within the editor Navigating compilation errors
39
Fixing Indentation <<, >> (normal mode) – shift one tab position to the left/right Don’t forget about . <, > does same in command mode :4,20> = – indent Works with motion =10 =/void/
40
Indenting as You Code :set cindent :set smartindent :set autoindent
Follows C/C++/Java ‘standard’ rules :set smartindent Indents after {; unindents after ‘}’ :set autoindent Indents to same level as previous line
41
Autocompletion <Ctl-N>, <Ctl-P> - next/previous word matching the prefix under the cursor :set complete option controls where to search for words Dictionaries can be also be loaded and used for non-code files Commands are slightly different Spell checking is available as well
42
Running Commands :!<command> - runs command in a shell
:sh – enters a shell exiting the shell (e.g. <Ctl-D> or <Ctl-Z>) returns you to the editor In particular: :!g++ *.cpp :!a.out
43
Navigating Compiler Errors
Compile your code, redirecting errors to a file :!g++ *.cpp 2>errors Inform Vim of the existence of the error file :!cfile errors :set errorfile=errors sets the file as the default error file Then just have to write :!cfile
44
Navigating Compiler Errors (cont’d)
Navigate through the errors with :cc - display current error :cc<n> - display error n :cn – next error :cp – previous error :clist – list all errors
45
Tips Don’t try to master it all at once Try to add one feature a week
If you ‘discover’ a new feature, try to use it often Google, google, google
46
A Most Important Tip Learn to touch type!!!
In case you didn’t hear me—
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.