Vim Tips Wiki
Register
Advertisement

Previous TipNext Tip

Tip: #1142 - Repeat last command and put cursor at start of change

Created: February 20, 2006 9:23 Complexity: basic Author: Adam Wolff Version: 5.7 Karma: 38/19 Imported from: Tip#1142

I rely on this behavior so much, I'm always surprised when I use VIM on a system that doesn't have this map installed:

nmap . .`[ 

This changes the behavior of the very useful . command to leave the cursor at the point where it was before editing started. This means that if I have, say, a list of files from a change summary that look like this:

.../foo/bar/pick.c 
.../cram/bar/yup.c 
.../drop/bar/slop.c 

And I want to change the leading path, I can go to the first one, and type (say) c3t/anotherdir<ESC> This gives me:

anotherdir/pick.c 
.../cram/bar/yup.c 
.../drop/bar/slop.c 

Now I can just go the beginning of the second line and hit .j. to change the second and third lines. Of course I could you can also use an RE for stuff like this, but often . is a little faster and saves precious brain cells.

:help .

Comments

I like that tip. I made something similar to the .

:map <a-.> <down>. 

knowing this tip I will change it to

:map <a-.> `[<down>. 

So you can do changes in more than one line with repeating only one key.

Sometimes it is useful to make changes like this with a Visual Block, but in this example, shown in the tip, it doesn't work

:help v_b_I

:help v_b_A

info at jochen minus behrens de , February 21, 2006 11:26


I use this to make changes to a visual block:

vnoremap <silent> . :normal .<CR> 

If you do something like this on a line:

Aline ending<esc> 

(Basically, adding 'line ending' to the end of the line.)

You can then visually select a bunch of lines and hit . to have the same happen to each of them.

salmanhalim--AT--hotmail.com , February 21, 2006 20:53


There is a very useful feature of vim built it for doing these kinds of edits. Take the original example for instance:

.../foo/bar/pick.c 
.../cram/bar/yup.c 
.../drop/bar/slop.c 

Instead of changing the first one and repeating the change for the other lines... Visually select the desired text using ctrl-v. Then with them visually selected (in this case the leading .'s would be selected) press c and then type the new text. It will appear to only be changing the first line however once you hit escape it will apply the change to all the lines.

No extra commands and no remapping needed. Works everywhere.

For a better description of all the things you can do with the visual block stuff just do

:help blockwise-operators

montumba--AT--gmail.com , February 22, 2006 14:54


Advertisement