tags:

views:

115

answers:

4

I've long used this very useful shortcut in vim:

nmap <space> i <esc>r

this means that if I press spacef, for example, it will insert a single character f at the given position.

unfortunately, however, this is not atomic, ie, if I press spacef and then navigate somewhere else, then press ., I get the equivalent of rf, not spacef.

all this makes sense, but here's the question: is there a way of making this atomic, so that . will repeat the 'insert character' operation, and so that undo etc all treat it as one operation, too?

+1  A: 

Sorry I can't provide a specific answer to your problem but I will note that I tend to use the . key when I have to reproduce quite a lot of commands (e.g. I want to insert f 5 or more times).

If that is the case here, I don't think the saving of using your macro is worth it. You save one keystroke by using your macro rather than ifesc and that operation is atomic so you could then . to your heart's content.

I would just use the non-macro version if I know I want to repeat it a lot.

P.S. You know I'm starting to like the <kbd> tag quite a bit :-)

paxdiablo
absolutely, and this is my current behaviour - but using this shortcut is so convenient I want to make it possible to use it all the time. it's less about saving keys, and more about consistency :)
Peter
+1 for random postscript (which I'm assuming isn't mocking me :))
Peter
No, I've actually never used it before, I've tended to use the blockquoting markup, but kbd is much better for keys.
paxdiablo
+2  A: 

You might want to have a look at this plugin script. It may be possible to configure your map so it can be supported. Read the supporting docs

http://www.vim.org/scripts/script.php?script%5Fid=2136

michael
thanks a lot for this. it looks like @too much php's approach is pretty good, and it's based on this.
Peter
+2  A: 

Awesome! Michael's answer pointed me to the plugin I needed to finish my plugin, which can now do what you want - I had been trying to figure out how to do this for ages!

1) Install Tim Pope's plugin

2) Install my plugin

3) Add a mapping to your .vimrc:

nnoremap <space> :<C-U>call InsertChar#insert(v:count1)<CR>
too much php
this is much more complicated than I was expecting, but does make the cursor red (which is a great touch), does allow cancellation with escape, and may just be what I use from here on in...
Peter
I only discovered this today, but I still comment want to say - this is going to be life-changing! Thank you!
Umang
+1  A: 

Does this work for you?

noremap <silent> <space> :exe "normal i".nr2char(getchar())<CR>
Maxim Kim
It's good, and definitely one possible solution. A disadvantage of this approach is that the cursor disappears when you press space, so you lose track of where you're inserting the character. @too much php's approach is much more complicated, but does have the advantage of leaving the cursor in place.
Peter
If you do quick insertions the lack of cursor isn't that annoing.
Maxim Kim