0% found this document useful (0 votes)
66 views10 pages

Why, Oh WHY, Do Those #?@! Nutheads Use Vi?: Misconception #1: Modal Editing

The document discusses why some programmers prefer the vi text editor despite its age. It argues that vi's modal editing approach is misunderstood, as users are meant to stay in normal mode and only briefly enter insert mode to type text. Vi's system of combining commands and motions allows for powerful multi-key shortcuts to edit text efficiently. Though vi uses regular expressions, its main power comes from basic editing commands that can be repeated across the document quickly from the home row keys. Learning vi's system provides lifetime benefits for programmers who edit text for many hours each day.

Uploaded by

g_teodorescu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views10 pages

Why, Oh WHY, Do Those #?@! Nutheads Use Vi?: Misconception #1: Modal Editing

The document discusses why some programmers prefer the vi text editor despite its age. It argues that vi's modal editing approach is misunderstood, as users are meant to stay in normal mode and only briefly enter insert mode to type text. Vi's system of combining commands and motions allows for powerful multi-key shortcuts to edit text efficiently. Though vi uses regular expressions, its main power comes from basic editing commands that can be repeated across the document quickly from the home row keys. Learning vi's system provides lifetime benefits for programmers who edit text for many hours each day.

Uploaded by

g_teodorescu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

Why, oh WHY, do those #?@! nutheads use vi?

The VI Gang Sign


by Jon Beltran de Heredia, May 16th, 2007

Yes, even if you can't believe it, there are a lot fans of the 30-years-old vi editor (or its more recent, just-15-years-old, best clone & reat im!rovement, vim"# $o, they are not dinosaurs %ho don't %ant to catch u! %ith the times - the community of vi users just &ee!s ro%in ' myself, ( only ot started ) years a o (after over 10 years of bein a !rofessional !ro rammer"# *riends of mine are convertin today# +ec&, most vi users %ere not even born %hen vi %as %ritten, Yes, there are definite reasons %hy the vi-vim editin model is just su!erior to any other out there# .nd you don't need to be a /ni0 %hi1 to use it, either' vim is available for free for almost any !latform out there, and there are !lu -ins to et the functionality inside all major (23s# 4et's try to brea& a fe% misconce!tions, and see some real e0am!les of %hy it's the &illerest'

Misconception #1: modal editing


The first time you stumble into vi or vim, you are shoc&ed and dis usted that you have to use 'i' to start ty!in te0t# 5an't remember %hich of hj&l moves in %hich direction# .nd you need to !ress 'a' if you %ant to insert after the last character of the line# 6ince you're already !robably used to other editors, and if the arro% &eys %or& in insert mode (they do if the system is confi ured !ro!erly, %hich is over 708 of the cases", you %ill stay in insert mode and not o bac& to normal mode e0ce!t if absolutely necessary# You'll !robably s!end )0 minutes %ith it, and never o bac&# .nd also com!lainin ' 9+o% on earth am ( oin to remember %hether ('m in insert or normal mode:9 Turns out, this is just a completely rong %ay to use vi or vim# /sin vi-vim !ro!erly, you don't use it modally# You are al ay! in normal mode, and only enter insert mode for !hort b"r!t! of ty!in te0t, after %hich you !ress ;3sc< to o to normal mode# Thus, the rememberin -the-mode !roblem just doesn't e0ist' you don't ans%er the !hone in insert mode to et bac& to vi and not remember %here you %ere# (f you are ty!in te0t and the !hone rin s, you e0it insert mode and then ans%er the !hone# =r you !ress ;3sc< %hen you come bac&# >ut you never thin& about insert mode as a mode %here you !tay# 4et me e0!lain the !hiloso!hy behind this# 5ommands in vi-vim are meant to be combined - 'd' means delete, 'e' means 'move to end of %ord', then 'de' is a com!lete command that deletes to the end of the current %ord (somethin li&e 5trl-6hift-?i ht, 4eft, 2el in most re ular editors"#

=ne ood benefit of this is that the '#' command re!eats the last complete, combined editin command (not movement commands"# .fter doin 'd%', '#' %ill act as a command to delete to the be innin of the ne0t %ord# You can move around at %ill %ith all the !o%erful navi ation commands, and !ress '#' at %ill to delete to the ne0t %ord @uic&ly# This turns out to be incredibly !o%erful# .nd no% %e come to insert commands# These commands enter insert mode and let you ty!e te0t until you !ress ;3sc<# =&, in the case of these commands, the full command includes all the te0t you ty!ed bet%een 'i' (or %hatever command you used to enter insert mode, as there are several" and ;3sc<# Ahat this means is that if you ty!e 'i+ello;3sc<', %hich enters the te0t '+ello' at the cursor's !osition, %hat no% '#' does is 9Ty!e '+ello' at the cursor's !osition9# .ll of it# .nd you can understand that is !retty !o%erful# >ut it's better than this# '.' oes to the end of the current line and enters insert mode there# 6o, after com!letin the insert by !ressin ;3sc<, you can !ress '#' any%here to o to the end of the line and do the same thin # =ne other even more !o%erful e0am!le' let's ta&e the 'ce' command, %hich is com!osed of 'c' and 'e'# The 'c'han e command deletes the ran e s!ecified by the ne0t motion command, and also enters insert mode# (t's the same as 'd', but %ith the distinction that it enters insert mode, instead of stayin in normal mode# The boon is that the te0t you ty!e in the ne0t (short" in!ut session is also !art of the command# 6o if you do 'ce+ello;3sc<<', %hat you do is re!lace from the cursor to the end of the %ord by '+ello', and the '#' command after%ards %ill %or& e0actly li&e that' re!lace u! to the end of the %ord %ith '+ello'# Botions (cursor-movin commands" can also et more com!le0, and there are all sorts of insert-mode-enterin commands ('o' to create a ne% line belo%, '=' to enter one above, '6' to delete to the end of line, etc### all of them enterin insert mode after%ards" so you can ima ine %hat ty!e of !o%erful editin mini-o!s you can build and re!eat %ith sim!le '#'s,

Example #1: the onde!"ul dot command


4et's see a concrete e0am!le# 6ay you have declared three ne% functions in your header file, and you need to im!lement them in the module# You co!y the follo%in te0t and !aste it into your im!lementation file'

#ll code !tart! li$e thi!, doe!n%t it& $o% you have to remove their semicolons, and addin an em!ty body %ould be a ood idea# Aith the cursor as above, you can use '.' to o strai ht into insert mode at the end of the line'

#'ter %#% ( !ee the in!ert mode c"r!or )*ertical line+ at the end $o% you delete the semicolon %ith ;bac&s!ace<'

,eleting i! !imple .nd ty!e ;return< C ;return< D ;return< to insert the body'

In!erting too, a! in any other editor .nd finally, ty!e ;3sc< to return to normal mode'

#nd no , -"ic$ly., e ret"rn to normal mode .nd no% you have to re!eat this %ith the other t%o# +o% do you do it: 3asy' first, !ress 'j' to move the cursor do%n# .nd then, !ress '#' to re!eat the last full editin command (the '.' command %ith the bac&s!ace and the inserted te0t"# (f you do 'j#j#', that is, t%ice, you et the follo%in '

J"!t /0/0 to do thi!. The vi command architecture %as &ey here, to ether %ith the fact that ;bac&s!ace< is as !art of the editin se@uence as re ular ty!in , and the editin o!erations bein !retty re!etitive# >ut thin& about it, ho% much of your daily editin is re!etitive: Yeah, ( thou ht so#

Misconception ##: it$s not all a%out !egula! exp!essions


Ei-vim is !retty !o%erful handlin re ular e0!ressions# .ll half-serious editors have re ular e0!ression su!!ort for searchin , re!lacin , etc, but only vi (that ( &no%" can use them in hi hly com!le0 %ays, such as doin a certain re e0 search and re!lace in the second occurrence of 'be in' after lines that contain '!roc', or %hatever you thin& u!#

6o this is not to demean the !o%er of re e0es, or the vi-vim re e0-usin !o%er# >ut the main !o%er of vi, and the !o%er that you can't live %ithout after you've ot used to it, is the !o%er of the basic editin model'

=ne- or t%o-&ey motions to move directly any%here on the line, or on the screen =!erators such as 'd' or 'c' that can be combined %ith any motion to directly modify some te0t and maybe enter insert mode, and bein able to re!eat it all as many times as you %ish %ith '#' .nythin can be done %ithout movin your hands a%ay from home ro%, $o more sufferin %hen editin on a la!to! %ith a braindead &eyboard layout (most of them"

Example ##: sma!t !anges


4et's see the follo%in ty!ical e0am!le# (t's just a function call embedded in a some%hat com!le0 e0!ression'

1omple2 e2pre!!ion and '"nction call .s you see, %e have the cursor !ositioned at the start of the call# $o% ima ine that %e %ant to e0tract this and assi n it to a local variable# The first !art is selectin the relevant call, then co!yin and deletin it to move above, ty!in the var name, and movin above to ty!e the declaration# (n re ular editin models, you %ill !lay hunt-and-!ec& %ith 5trl-?i ht and left-ri ht until you et it e0actly ri ht# $ot %ith vi or vim# The '8' motion moves from a !arenthesis (or similar rou!in character" to its matchin one - but if you're not !ositioned at one of these s!ecial characters, it %ill scan character by character to the ri ht, until the first one is found, and then movin to the character matchin that one# 6o, in the above situation, it %ill move to the ri ht closin !arenthesis, Fno%in about the 'c' command, %hich deletes the e0tents of the ne0t motion (also co!yin it to the cli!board" and enters insert mode, %e can ty!e just 'c8' (9chan e match9" from the above case and %e et the follo%in '

#'ter /"!t %c3% )4change match4+ The relevant call has one to the cli!board, and %e're also ready to ty!e the name of the variable# $ot bad for just t%o &eystro&es, .fter ty!in the variable name and !ressin ;3sc< to o bac& to normal mode, it %ill loo& li&e this'

5yping and 67!c8 $o% in normal mode, you can ty!e '=' to o!en a ne% line above the current one and enter insert mode, and start ty!in the declaration'

J"!t pre!!ed %9% and typed the !tart o' the declaration

.nd no%, %hen %e have to insert the !revious e0!ression, since %e have it in the cli!board, %e can use 5trl-?, 9 to !aste it in insert mode (admittedly a bit unintuitive to remember, but it's the &ey to sim!le multi!le-cli!board su!!ort"# This %ill brin in the function call %e deleted at the !revious !oint# 6o 5trl-?, 9, %e ty!e a semicolon, and ;3sc< to et bac& to insert mode est us here'

:ini!hed.

Misconception #&: you gotta %e nuts and'o! a genius to use it


Aell, ( ho!e that %ith the above e0!lanations and e0am!les, you have already seen some of the !o%er that vi-vim !rovides# 4earnin it is tou h (see belo%", but if you're oin to be editin code H hours or more a day for years, it's the second best investment after learnin touch ty!in (%hich you already &no%, ri ht: (f not, don't bother %ith vi, learn that first"# . learnin curve of %ee&s ma&e sense for such a lifetime investment# .nd, at least, you %on't have a dumb assistant to annoy you to death# The !oint is, %ith vi, your &eyboard becomes a hu e s!eciali1ed te0t-editin ame!ad %ith almost a hundred buttons# 3ach of them has at least t%o functions, shifted and unshifted, so you have almost t%o hundred functions at a sin le &ey!ress (not countin 6hift"# 5ommands are incredibly !o%erful for te0t editin , and you can even combine them to obtain the best results# Ahile ty!in some te0t, it is a re ular &eyboard, but %hen you're bac& to normal mode you have the best-desi ned te0t-editin machine there is, and it sho%s#

Example #&: manipulating delimited %loc(s


=ne other sim!le e0am!le# This one only %or&s %ith vim (uses one of vim's s!ecific motions"# 6ay you are inside an an le-brac&eted section, as is so common in these days of IB4' ;i'e at an <M; tag000 +o% do you select the te0t inside for co!yin -deletin -modifyin it: (n re ular editors, once a ain, you have to move your hands off their current, comfortable !osition, and o for the arro% &eys or, even %orse, the mouse# .ny of those is !robably a !ain, es!ecially %hen %or&in on a la!to!# +o% do you do it in vim: You just use one of the te0t-object motions, %hich all start %ith 'i' or 'a' (they can only be used after an o!erator li&e 'd' or 'c'"# 'i<' refers to 9the current 'i'nner an le brac&et bloc&9, so you can do 'di<' (9delete inner an le-bloc&9" to just delete all the contents from the above situation' #'ter typing di8 )4delete inner angle(bloc$4+ You can use '(' or '"' for the current !arenthesi1ed bloc& (or even 'b' from 'b'loc&", 'J' or 'K', 'C' or 'D', '%' for !unctuation-delimited-%ord, or 'A' for the current s!ace-delimited %ord, !re!endin 'i' to any of them for the 'inside' contents, or 'a' to include the delimitin characters too# 5

Misconception #): h*(l to move a!ound?


Bany !eo!le find it %eird to use hj&l instead of arro% &eys for movin around# The actual reason for this im!lementation seems to be that terminals of the time didn't reliably have arro% &eys, and that this terminal in !articular had hj&l &eys doublin as cursor &eys# >ut the side effect is that you don't need to move your hands off the home ro% to move around, %hich is reat# >ut in any case, althou h you %ill use hj&l to move around at first, once you master vi-vim you !robably %on't use 'h' and 'l' ('left' and 'ri ht'" at all, and you %ill use 'j' and '&' s!arin ly# Ahy is that: >ecause there are other, more !o%erful motions, that %ill often et you %here you %ant to o much faster# Ahen movin inside a line, ( find that there is al%ays a motion to ta&e me strai ht to %here ( %ant to o, so ( use those motions' 'f' follo%ed by any other character to find its ne0t occurrence, '8' to use matchin !arens to o %here you %ant to o, etc### Ahen navi atin the file, you have motions to o to the to!-middle-bottom of the screen directly, '-' %hich is effortless to ty!e to search for a strin , 'KK' and the li&es to navi ate by functions, etc#

Example #): nice commands


6ome commands are just so useful that you %ould miss them if you &ne% them# '+', 'B' and '4' ta&e the cursor directly to the '+'i hest, 'B'iddle, or '4'o%er line on the screen# '1t', '11' and '1b' &ee! the cursor at the current !osition, but scroll the vie% so that it falls at the 't'o!, '1' center, or 'b'ottom of the screen# 'L' searches for%ard for the ne0t occurrence of the %ord under the cursor ('M' does the same bac&%ards, at symmetrical !ositions so its easier to remember"# .nd there are do1ens more###

Misconception #+: since you a!e thin(ing ,-. o" the time, and editing 1-., the p!oductivity gain might %e the!e, %ut it$s useless any ay
Aell, those are e0a erated fi ures, but this fact is often mentioned a ainst any editin bein im!ortant to develo!ment !roductivity# ('ll %a er that this is %ron # ains

*irst thin , in the chances %here ( really have to thin& a !roblem, and there is no need to loo& at code, ( !ic& my bi&e and o for a one hour ride# =r a t%o hour ride# .t least if the %eather is ood# (t's much nicer than stayin in front of the com!uter# .lso, %hen ( have to analy1e some hard !roblem and desi n a solution, ( often brin out a noteboo& (a real noteboo&, made of !a!er %ith cardboard covers and bound %ith a s!iral", and a !en, and try to clarify my thou hts there# You can bet that, %ith a fe% e0ce!tions, !roductive %or& can't be done a%ay from the com!uter# This is because, most of the time, you have to loo& at the code to thin& and desi n# .nd this involves navi atin the code %ith an editor# .nd also, very often, you LareL indeed ty!in or editin continuously# You maybe just thin& for 1 minute, but then you s!end one other full minute editin the chan es you just thou ht about# .nd %hen you are editin , you %ant to have the best damn tool for the job#

5omfortable editin hel!s you stay in 9the 1one9, the state of concentration that ets you the ma0imum !roductivity# .s you master a !o%erful tool such as an editor, it just disa!!ears from your conscious, and you are free to concentrate in the !roblem, and your editin ha!!ens unconsciously# ?e ular editin ma&es you hunt-and-!ec&, use 5trl-?i ht, 5trl-?i ht, 5trl-?i ht, 5trl-?i ht, 5trl-?i ht to et to %here you %ant to o, it ma&es you move your hand to your mouse, o!en a menu, select an o!tion, enter stuff into a dialo and clic& '=&' to acce!t it# (n vi-vim most stuff is a sur!risin ly small number of &eystro&es a%ay, in a direct fashion# =ther vi users have also shared this %ith me, so ( &no% ('m not alone in the feelin ' once you start to master vi, there are times in %hich, after finishin a 30-second-lon full-steam editin session, you &ind of '%a&e-u!' to a faint memory of the sound of a continuous stream of &eystro&es# (t feels li&e you have been hearin them in the bac& of your head %hile you %ere dicin and slicin lines of te0t, bloc&s, motions and modifications# .nd this durin this !eriod of editin , you are feelin a tremendous sensation of !o%er#

Example #+: indenting a %loc(


Ei and vim &no% your code has some structure# Bany of the commands reflect this# 6ay 'a>', as described in e0am!le M3 above' it selects the current 'C' and 'D' delimited bloc&, includin the braces themselves ('aD' does the same"# .nd let's ta&e it and combine %ith the '<' o!erator, a useful o!erator that indents the re ion indicated by the ne0t motion# Oicture the follo%in code'

Improperly indented +o% often do you encounter such a case: Yes, you can !aste %ith auto-reindent (just 'K!' in vim", but often you for et to, or you reach this case not because of !astin the bloc&, but because you added or removed stuff above# You just have to indent it a bit, and you'll be done# (n other editors, you move around, you select, you ty!e T.> or the shortcut to autoindent# $ot so %ith vim, just three &eystro&es' '<a>' (9indent a >loc&9" and you et this'

=e didn%t e*en ha*e to mo*e the c"r!or. +o% cool is that: You didn't move or select, you just told it directly %hat you %anted to do, and it did it# ( believe it's this ty!e of 9strai ht9 editin that ives you the feelin of !o%er and hel!s you enter and stay in 9the 1one9#

Misconception #/: it$s *ust stic(ing to a disappea!ing past


Ei has been around for 30 years, and it's still there# Eim, a full-vi-clone %hich introduces even more im!rovements than vi has features, has been alive and &ic&in for 15 years, available for free on almost every sin le !latform on earth# Oeo!le %ho love vi have found a %ay to use it every%here' there is a !lu -in to et vi emulation in 3cli!se, a !lu -in to et vi emulation in many Bac =6 I a!!s, a !lu -in to add vi emulation to (ntelliQ (23., even 3macs has not one, but several built-in vi emulators (%hen ( end u! tin&erin lis! code in emacs, ( start by tryin to use emacs &eybindin s, and al%ays end u! ty!in 9B-0 vi!ermode9", ( develo! and sell Ei3mu, a family of !lu -ins to et vi-vim emulation in Eisual 6tudio, 6R4 6erver, Aord and =utloo&, Oaul Sraham still uses it for his lis! and arc hac&in , Tim ='?eilly is a confessed vi-er, 6lic&3dit and 5ris! have vi emulation### =f course, the vi-vim community is not a majority' most com!uter users are not even touchty!ists, and vi adds yet another stee! learnin curve# Those %ho have seen the li ht, thou h, %on't o bac& to other, !oorer, editin systems - so vi-vim editin is uaranteed to survive for many years to come# .ctually, usin vi(m" &eybindin s is !robably the closest you can et to ensure you have it available in any environment you are or %ill be in the future, from old /ni0 systems to the latest !o!ular (23#

Example #/: visual natu!e


.nd as a final e0am!le, even if it seems vi is just about cry!tic and unreadable commands, %e are oin to see some of the more visual as!ects (actually, they are vim features, rather than vi ori inal ones"# =ne of them' %ith 'hlsearch' enabled (off by default in vim, but easily enabled %ith 9'set hlsearch9", %hen you search for a strin , all its matches are hi hli hted on the screen# 6ay you have the follo%in html code'

Some html !o"rce H

(f you !ress 'L', the %ord under the cursor ('div'" is searched for# (t results in this'

>o er'"l a!teri!$. .s you see, the cursor has moved, and the different occurrences are conveniently hi hli hted# 4et's see some more# Ae've seen o!erators li&e 'd' and 'c' act u!on the re ion iven by the ne0t command# Aell, if %e %ant visual feedbac&, %e can use visual mode' !ress 'v', move around %hile you see the re ion from the initial !oint hi hli hted, and then !ress the o!erator directly to see its effect# 6earch commands %or& here as %ell# (f you use 'E' instead of 'v', hi hli htin %ill be done by lines# 4et's !ress 'E' and then '&' (u!" after the above'

#'ter V$ )4*i!"al(line!, "p4+ .s you see, the t%o lines are hi hli hted as selected# 6ay %e %ant to select until the closin div ta (the search hi hli ht sho%s it"# Ty!e '$' (!revious match", and %e et the follo%in '

#'ter %?% )4pre*io"!(match4+ .nd no% %e can do %hatever %e %ant, say, the ' /' o!erator to ma&e everythin u!!er-case (and return to normal mode"'

;o*ely "pper ca!e .nd no%, let's just o for a correct common 'conce!tion' about vi-vim'

0o!!ect1conception #1: steep lea!ning cu!ve


There is one thin %hich is a commonly held !iece of &no%led e, and %hich is true as %ell# (t is sho%n on this !a e to the best effect# (t's the fact that learnin vi-vim is an activity that %ill ta&e a lon time (%ee&s to months", and that the first e0!erience is not !leasant# ( ta&e it

this is the main reason %hy vi-vim editin isn't, and %ill never be, a !o!ular thin # You need to invest @uite some effort to learn, memori1e, and internali1e the 30 or so commands that start ma&in you more !roductive than %ith other editors# 6ince they're all arbitrary one-&ey commands (althou h all of them have some easy mnemonic to aid in rememberin them, and even some form of coherence", this is not an easy tas&# (t's easy to thro% in the to%el and o bac& to familiar jedit or !ico, ultraedit or te0tmate, or even emacs# >ut once the effort to learn it has been made, ( &no% of no one %ho oes bac&# .nd ( &no% do1ens of !eo!le that have told me that they've been usin 'vi' for over 10 years, and that they're accustomed to and e0!ectin even the smallest details#

0losing o!ds
2o %hatever you %ant# 2on't learn it if you feel it's too much effort just for nothin # 4earn emacs instead# =r stay in your (23 usin a lousy editor# Ahatever# >ut in any case, don't ever claim a ain that those 'vi uys are nutheads' - ( ho!e that ( have succeeded in sho%in you %hy they ( e" stic& to it, and you should at least be able to understand its !o%er, even if you !refer to stay a%ay from it#

(f you %ant to research vi-vim editin some more, here are some useful references'

4earn %hy ( ot started /se my ra!hical cheat sheet and tutorial to learn vi-vim editin The a%esome 9vi lovers9 home !a e 4earn Qonathan BcOherson's hints for effective editin %ith vim

.nd of course'

So to vim's home!a e for anythin you %ant

)Special than$! to I*an Vecerina, #ndrey B"to*, Jo!e Gon@al*o, Mar$ >etri$ So!a, #itor Garay and =oody 5hro er 'or commenting on early dra't! o' thi! article+ )#ll capt"re! ta$en 'rom Vi!"al St"dio ith Vi7m", my )commercial+ *i em"lator, and ith code$ana, my "pcoming prod"ct, pro*iding the enhanced !ynta2 highlighting+

You can comment the a!ticle at my %log

10

You might also like