Ambiwhateverous - sunday 2009-03-29 0137 last modified 2009-03-31 2004
Categories: Nerdy, Daily Grind
TrackBacks Sent: None

My first exposure to UNIX as a user was at the University of Minnesota where I first formally learned a computer language (they had us doing BASIC and LOGO in elementary school; strangely enough, at the time that was the last of programming for the remainder of a student's tenure there). Perhaps in part to avoid the ongoing holy war of emacs vs. vi, the teaching assistants and professor never bothered introducing us to vi, only emacs. So my first instinct in editing is to type emacs.

In my first exposure to snowboarding, I waffled a bit, then made a choice to ride goofy (right foot forward). Not that I'm any good at it, but I happen to be at a point in learning where, if I don't make a conscious effort to try to ride both regular and goofy, I'll probably never be able to ride regular with any precision. So I try to switch; whenever I happen to take that month or two or that whole season to live on a mountain, I'll keep at switching. I have this unfounded suspicion that being able to ride facing either way is a useful skill.

And so it is with editors. I'm trying to learn to use vi now, in part by force because one environment simply doesn't have emacs available. My first instinct there is still to turn to emacs; a number of times I've tried to launch it only to be reminded it isn't there. Learning progresses, and fourteen years are hard to unlearn, but there are some interesting reversals. I'll be sitting at an emacs terminal and type dd where I needed ^KK (that would translate into "delete one line"). I haven't figured out how to cut and paste as well in vi, and I imagine there are interesting shortcuts in both I'm unlikely to ever work into my repertoire.

There's a general philosophy of traveling on both sides of an issue that one might extract if they were digging too deeply. Or maybe a fear of commitment? Now if only I could learn to write with my left hand.


vi cut and paste

heh - I took to emacs fairly quickly (unless I blacked it all out from my memory) - I love keyboard shortcuts, and after reading the tcsh manual and copying various bits of .emacs and .zephyr.vars from friends, I was quite happy with my setup.

But... the only vi command I knew for a long time was :q!

I've since been on some systems that didn't have emacs, and where I couldn't get it installed, or I was only going to be on the system for a minute, so wasn't worth installing anything, though it's probably a toss-up between vi and nano/pico, which is sometimes installed on minimal systems (and I use Pine for email, so I'm quite familiar with the keyboard shortcuts in pico).

The only cut I know is dd, like you say, but you can prefix it with a digit(s) to grab a bunch of lines. And then 'p' or 'P' will paste it in (after or before the current line).

(btw, did you see the last couple emails on nerdcomm? Should I try to figure that out, or is the site dying a slow, unrecoverable death?)

Jon Daley on April 01, 2009 08:23 AM

Advanced cut and paste

Emacs has the ^-space, ^w/esc-w method of cutting / copying arbitrary blocks of text without bothering about line boundaries; I think it also has a rectangle definition mechanism as well. Having to count lines before cutting (or yy for copying) them is a distinct drawback to moving the cursor. I keep thinking there has to be a way to move the cursor to the beginning / end of a line while in editing mode, but I haven't found it yet, and ^a also works in my shell of choice.

Ryan Lee on April 03, 2009 12:10 AM

Vi Tips

I've become ambieditorous to the point of using VIPER mode in emacs. I love the way vi shortcuts don't cause me to contort my hands and type several keys at once; it's probably better for avoiding RSI.

Copy and cut commands in vi start with either d for cut or y for copy, followed by any movement command. So 'de' is cut to end of word, 'y$' is copy to end of line. 'dt(' is cut up to the next '(' char found. You can also store these snippets in cut buffers using the double quote char and a letter (the name of the buffer) as a prefix, like so: '"aye' puts the current word in cut buffer 'a', from which you can later paste it with '"ap'.

There's also visual mode, which is great for more complex copying where you don't know ahead of time how to specify the end of the copy region. Go to the place you want to be like emacs' mark, and hit 'v'. Now move around with any movement keys (e.g. '/' to search or 'f(' to find '(' chars, followed by '.' to repeat the find). When the selection is correct, hit 'y' or 'd' to copy or cut and exit visual mode.

Jesse Byler on April 03, 2009 10:29 AM


It's nice to know the keyboard shortcuts exist, though it is hard to wrap my head around them. I also like that the emacs shortcuts work in shells and in other programs too occasionally.

Jon Daley on April 05, 2009 05:49 AM

You are henceforth

dubbed The vi Man, Jesse. Visual mode hits the sweet spot for sure. Dare I ask for insights on replace? I pull a basic text replace or RegExp replace from time to time in emacs, when I can get the syntax right. Anything of the sort in vi? I think my major block actions would be on par by then.

Ryan Lee on April 06, 2009 11:50 PM

Hmm, I think I'm in the habit of firing up emacs if I want to do complex replacements or use macros. I usally get by with this type of thing in vi: '/foo<RET>' to hilight all instances of 'foo' (if you have 'set hlsearch' on in ~/.vimrc). Then I do a first replace with 'cwbar<ESC>' (change word to 'bar') or '3sbar<ESC>' (replace 3 characters with 'bar'). From there you can navigate among the search results with 'n' and 'N' and use '.' to repeat the replacement command. This can actually cover a lot of cases though since the search string is a regexp and the change command can be anything.

Just for curiosity's sake though, I looked up how to do an emacs-style search and replace with back-references and prompting, and here it is: ':%s/foo/bar/gc' The core is 's/foo/bar/g', like the sed command. '%' means apply to all lines in the file, and 'c' means to prompt interactively for each match. The whole thing is introduced by ':' which is how you get the M-x prompt. And it turns out there's a lot of crazy stuff supported for searches, like search-and-move, search-and-second-search, etc. Try ':help pattern.txt' if you're curious.

And you probably want 'set incsearch' and 'set smartcase' in your ~/.vimrc.

Attachment: dot.vimrc

Jesse Byler on April 10, 2009 10:06 AM

Beautiful. Than...

Beautiful. Thanks, Jesse. I also found the command line history feature to be pretty useful, in combo with what I assume is a ~/.vi_history sitting somewhere, to repeat substitutions across files. I'm sure I can do that with emacs and just haven't bothered learning, but I do find myself doing emacs substitutions instead of sed or something else across files often enough that being able to type :%s<up> in vi and get the substitution I just did on a prior file an extreme time saver.

Emacs was installed on that environment as part of an upgrade, so now I have a net to fall back on. But I'm still going with the practicing vi thing - if there's nothing major to learn from a project, go with the little things... Maybe it's time I incorporated explicit copy buffers into the flow too.

Ryan Lee on April 16, 2009 10:23 AM

You must login to leave a comment


No TrackBacks for this entry.