Have . restore the cursor position a la emacs in viper mode

From Vim Tips Wiki

Jump to: navigation, search
Tip 474 Printable Monobook Previous Next

created May 14, 2003 · complexity basic · author Brett Williams · version 6.0


Before making the switch to Vim, I spent time in limbo using EMACS in viper mode (which is EMACS with vi emulation). Despite the vast improvements in most areas, there were a few things I missed from EMACS. One thing I really missed was the behavior of the . command, which always restored the cursor position.

For example, let's say I had the following:

foo.some_method.each do { |x| puts x }
foo.some_other_method(Time.now)
foo.close()

and I wanted to change foo to bar. I would always use a cw and then the . command:

cwbar<CR>j.j.

rather than making a substitute command which would make me worry about the range over which it should take place etc.

When making the switch to Vim, I was disconcerted because after the first . my cursor was at the end of the second 'bar'.

In fact I pretty much always wanted this behavior from '.', so this mapping helped me out:

"make . not move the cursor
noremap . mz.`z

I've since noticed this is just a special case of the tip *restore-position* in the help manual, but it took me a while to find out how to do this. One other former emacs/viper user also was searching for this. Hopefully this tip makes it easier for the next person to find who finally finds their way out of EMACS.

[edit] Comments

You can also use the following mapping, that doesn't modify the z mark

noremap . .'^

When changing the text you always have several ways of how to specify the text to change.

You can change a word by typing cw at the beginning of the word. However I would prefer ciw - using text objects.

It was the "dot" command that made me to start using text objects aw, iw, ap, ip, etc. :help objects


>You can also use the following mapping, that doesn't modify the z mark

>noremap . .'^

Wouldn't this be:

noremap . .`[

The backtick jumps the cursor to the correct column.


Personal tools