sponsor Vim development Vim logo Vim Book Ad

basic Tip #474: have . restore the cursor position a la emacs in viper mode

 tip karma   Rating 11/5, Viewed by 1108 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   May 14, 2003 21:39      complexity:   basic
author:   Brett Williams (brett_williams at agilent dot com)      as of Vim:   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.

 rate this tip  Life Changing Helpful Unfulfilling 

<< "compiler" for perl | wrap visual selection with fold markers >>

Additional Notes

Anonymous, May 15, 2003 2:19
You can also use the following mapping, that doesn't modify the z mark

noremap .  .'^
JohnSivak at Hotmail.com, May 15, 2003 7:56
You can also use "set nostartofline" in your .vimrc.

See ":help startofline"
Anonymous, May 15, 2003 12:47
nostartofline doesn't apply to '.' according to the help:

       This applies to the commands: CTRL-D, CTRL-U, CTRL-B,
        CTRL-F, "G", "H", "M", "L", , and to the commands "d", "<<" and ">>"
        with a linewise operator, with "%" with a count and to buffer changing
        commands (CTRL-^, :bnext, :bNext, etc.).  Also for an Ex command that
        only has a line number, e.g., ":25" or ":+".
JohnSivak at Hotmail.com, May 16, 2003 5:58
You're right. I thought "startofline" was doing something it isn't..
[email protected], May 26, 2003 2:33
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 begining
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, ... .
see :help objects

                      Stano
[email protected], May 27, 2003 18:30
Stano, great tip on the text objects.  'ciw' is great and I'm learning more of the objects.

Does anyone know if the concept of text blocks etc. is extended to use matchit delimiters so that changing a block would work not only with {}, but with do ... end for example?
Anonymous, June 23, 2003 19:15
>You can also use the following mapping, that doesn't modify the z mark

>noremap .  .'^

Wouldn't this be:
noremap . .'[
Anonymous, June 23, 2003 19:16
Shoot. I meant
noremap . .`[

The special single quote jumps the cursor to the correct column
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo