Apr 18 2009

Learning ViEmu, part 2

Category: zvolkov @ 10:41

They say, once you've groked Vi, there's no way back. I'm still nowhere near "grokking" but I'm starting to get the feel of it.

If you have played with commands I listed in the first post, you probably found them pretty limiting. I found myself switching to Insert mode every time I needed to do anything involving copy/paste, identation, and paging up/down. This is not surprising, considering that none of these basic needs were covered in the previous post. Let's see how we can do them in ViEmu, while having fun with one or two new commands.

Moving around

Ctrl+u / Ctrl+d scroll the page half-a-screen Up or Down. This seems to be more useful than the usual full-screen paging as it makes it easier to see how the two screens relate. For those who still want to scroll entire screen at a time there's Ctrl+f for Forward and Ctrl+b for Backward. Ctrl+Y and Ctrl+E scroll down or up one line at a time.

Crazy but very useful command is zz -- it scrolls the screen to make this line appear in the middle. This is excellent for putting the piece of code you're working on in the center of your attention. Sibling commands -- zt and zb -- make this line the top or the bottom one on the sreen which is not quite as useful.

^ and $ are Vi versions of Home and End buttons. They're real easy to remember as these are same exact symbols used in Regular Expressions to indicate beginning and end of string (I like to think Vi invented them first :). Once again, these are most useful on notebooks where Home and End are misplaced at the far upper right. Note that ^ goes to first non-whitespace character. If you want to go to the very beginning of the line, use 0 (stands for "position number zero") -- I use this to fix the horizontal scrolling after looking at the end of the line beyond the screen.

+ and - are used to go to the "soft" beginning of the next or previous line. The only problem with them, minus is a straight keydown while the plus requires the Shift pressed -- got to remember this when going back-and-forth. The good news is: you can use Enter instead of the plus.

The "Big Moves": H, M and L move cursor to the High, Middle or Low row on the current screen and G Goes to the last line in the file.

Finally, to complete the list of moving commands, [number]gg go-goes to the line indicated by the [number] (just gg without any number go-goes to the first line).

Search

f[char], e.g. f followed by any other character, Finds the next occurence of the specified character and moves the cursor onto it.

t[char] is similar to Find, except that it only goes unTill the specified character, placing the cursor just before it.

% finds and jumps to the matching parenthesis.

* and # find next and previous occurances of the word under cursor. Unexpected sideeffect: they also highlight all the occurances in the file. This is very useful if you know how to unhighlight them when you no longer need. I'll show you how to do this in a second.

The question mark key (actually the wack symbol on it -- i.e. /) turns on the incremental search which highlights the first matching word as you type it. Naturally you can press Esc to quit the search operation or Enter to confirm and jump to the found word. From then on you can keep on pressing n to jump to each subsequent occurance of the matching string. This is made simpler by the fact that incremental search also happens to highlight all matching occurances -- annoying but informative. To clarify potential confusion the actual question mark ? symbol on the same key as / requires a Shift press, so they decided not to use it for forward search for the sake of typing efficiency and made it search backwards in file instead. As you see, creators of Vi put a lot of effort in making these commands easy to remember AND efficient but sometimes that required sacrificing logical simplicity. Now, to unhighlight the last search result just search for a string that definitely does not exist in the file. I usually smack something random like /gfthhdhj -- cool huh?!

Chaining Commands

Perhaps one of the coolest features in Vi (well, definitely the coolest one I know so far) is ability to chain commands.

The first chainable command I discovered was d (Delete the next move). Perhaps I should say d[cmd] where [cmd] is any move command. Examples of chains you can build with d:

de -- delete from cursor to the end of the word (you can also do dE to delete until the next space)

bde -- delete the current word, from left to right delimiter

df[space] -- delete up until and including the next space

dt. -- delete until next dot

dd -- delete this entire line

this chaining stuff lays the groundwork for exactly where we want to be next... which is:

Copy/Paste

For the fans of the classic Select-Copy-Paste style (using good old Ctrl+Ins / Shift+Ins for clipboard operations of course, not these ugly Ctrl+C / Ctrl+V) let me assure you that in ViEmu you can still do similar thing. Just press v to turn on the so called Visual mode -- which is akin to pressing and holding the Shift key in a normal editor, meaning the text will be highlighted as you move over it with hjkl. You can even use e, b, and /-incremental search to move the cursor where you want it and all the text between the point where you pressed v and the current point will be highlighted!

Once the text is highlighted, you can use y to yank it (yank is the Vi slang for Copy To Clipboard) or d to delete it. BTW, it turns out the d command we learned in the last section was actually a clipboard command. It places the text it deletes into clipboard which makes it equivalent to Cut command in normal editors. There's another variation of Cut command in ViEmu called c (short for Change). It cuts selected text into clipboard and swithes to Insert mode so you can start typing the replacement text.

Pasting in Command mode is done by pressing p. If you find yourself having to paste while in Insert mode, do not despair. You can still do Ctrl+R to paste Right now (the silly mnemonic is mine :)

Now, if you want to do copy/pasting the native Vi way you should use the sequences y[cmd]. This makes Vi Yank the text from the current cursor location to destination of the move specified by the [cmd]. As you could have guessed, d[cmd] and c[cmd] work in the same fashion.

In the spirit of "easier done than said", here are few examples:

ye (or yE) -- yanks text from here to the end of the word

ce - cuts through the end of the word

bye -- copies current word (makes me wonder what "hi" does!)

yy -- copies the current line

cc -- cuts the current line, you can also do S instead. There's also lower cap s which cuts current character and switches to insert mode.

viwy or viwc -- Yank or Change current word. Hit w multiple times to keep selecting each subsequent word, use b to move backwards

vi{ - select all text in figure brackets. va{ - select all text including {}s

vi(p - highlight everything inside the ()s and replace with the pasted text

Identation

Go to part 3 for identation and more fun stuff.

Tags:

Comments

1.
format computer format computer says:

bye -- copies current word (makes me wonder what "hi" does!)
rofl xD

2.
trackback Yesterday's news says:

Learning ViEmu

Learning ViEmu

Comments are closed