Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is correct usage to go around vim missing "insert one char" command?

Tags:

vim

insert

This is possibly not a good question for SO, but it's been bugging me for years, and Google didn't know, so let's give it a shot, as it does affect my programming work on weekly basis:

I often find myself in situation where one char is missing, like "=" instead of "==", a missing space, surrounding something with quotes/brackets, etc.

So, why doesn't vim have a proper command to insert a single character? By proper I mean, supports count and repeating with ..

What is the rationale, and what is the correct usage pattern that I am missing, which makes this feature unnecessary? I seem to need the all the time, so there must be some reason it has not been added to original vi already.

I know adding a simple basic keybinding like :nmap <Space> i_<Esc>r is easy enough, but when doing just a quick edit in a new environment, that's rather inconvenient, and this simple version does not work quite properly anyway.

PS. If there in fact is a default binding to insert just one char with total two keystrokes and remain in command mode, similar to r to replace one char with two keystrokes, I promise a bounty of 100 to the first answer which tells me what it is.

like image 905
hyde Avatar asked Feb 28 '13 09:02

hyde


People also ask

How do I go to a specific character in Vim?

You can type f<character> to put the cursor on the next character and F<character> for the previous one. You can also use ; to repeat the operation and use , to repeat it in opposite direction.

How do you go back one word in Vim?

Press w (“word”) to move the cursor to the right one word at a time. Press b (“back”) to move the cursor to the left one word at a time. Press W or B to move the cursor past the adjacent punctuation to the next or previous blank space.

What is insert in Vim?

Insert mode is the mode to be in when inserting text into the file. Command mode is the mode to be in when giving commands which will move the cursor, delete text, copy and paste, save the file etc.


2 Answers

I also struggled with this question and I found somewhere on the internet (I can't remember where) someone who did this:

:nnoremap s :exec "normal i".nr2char(getchar())."\e"<CR>

It is not perfect because it doesn't support count but it can be repeated with . I use that now so maybe other users will be satisfied with that too.

like image 76
statox Avatar answered Nov 16 '22 02:11

statox


No. There's no default keybinding for that (do :viusage for a complete list of normal mode commands).

If you want to know why, you'll have to ask Bram Moolenaar or Bill Joy, I'm afraid.

But here is an idea: r and s work on the character under the cursor. What they do is fairly limited and one dimensional but how would your command work?

Would it work like i, inserting that single character before the current character or would it work like a, inserting that single character after the current character?

Because "inserting text" can happen before or after the current character, we have i and a and, rather obviously, we need two commands for quickly inserting a single character.

Which makes the problem a little more complicated.

What keys should we use since all the alphabetical keys are already taken? <C-something>? <C-i> is taken, and <C-a> is also taken. <C-S-i> and <C-S-a> are both non-practical and not guaranteed to work everywhere so what? <M-something>? It won't work everywhere as well. Maybe a two-characters mapping? But which one and following what mnemonic logic?

like image 26
romainl Avatar answered Nov 16 '22 03:11

romainl