Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making inserting a single character in Vim an atomic operation

Tags:

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?

like image 450
Peter Avatar asked Oct 13 '09 02:10

Peter


People also ask

What does Ctrl o do in Vim?

When you move your cursor to a particular position in a file, Vim remembers this and lets you move around between where you are where you were. CTRL-O goes to the older position, and CTRL-I or tab goes to the newer one.

What does F do in Vim?

If you press "F", Vim will move the cursor backwards instead of forward. Given the previous sentence, if pressed "Fq", and the cursor was at the end of the line, it would move to the "q" in "quick".


2 Answers

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>
like image 66
too much php Avatar answered Sep 28 '22 08:09

too much php


Does this work for you?

noremap <silent> <space> :exe "normal i".nr2char(getchar())<CR>
like image 36
Maxim Kim Avatar answered Sep 28 '22 07:09

Maxim Kim