Why, Oh WHY, Do Those #?@! Nutheads Use Vi?: Misconception #1: Modal Editing
Why, Oh WHY, Do Those #?@! Nutheads Use Vi?: Misconception #1: Modal Editing
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'
=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,
#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#
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"
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 #+: 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#
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#
(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'
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'
)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+
10