Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command to surround a character with spaces in vim

Tags:

vim

vi

I am trying to use vim properly - to aid me I've mapped my arrow keys to "" so that I am forced to use {hjlk} to move around.

This is causing me a problem when I want to just surround a character with spaces, eg:

"2+3" is better formatted "2 + 3"

Previously I would have put my cursor over the + and typed:

i[space][arrow-right][space][Esc]

That's 5 presses.

To do this without the arrow I seem to need to put the cursor over the + and go:

i[space][Esc]lli[space][Esc]

That's 8 presses.

I can convert the "li" into an "a" which reduces it to 7 presses:

i[space][Esc]la[space][Esc]

Short of writing this into a macro is there a better way of doing it? Is there some magic vim command which will allow me to do it in less than even 5 presses - and some way to generalise it so that I can do it to entire words or symbols, eg if I want to convert 3==4 to 3 == 4?

like image 217
William Becker Avatar asked Mar 14 '10 19:03

William Becker


1 Answers

Personally, I think it makes most sense to destroy what you want to surround, and then repaste it.

c w "" ESC P

Obviously, you can replace both the object and the quotes with whatever you like. To change just one character + to be [space]+[space], you would do

s [space] [space] ESC P

on the +

like image 143
Sarah Avatar answered Oct 08 '22 22:10

Sarah