tags:

views:

134

answers:

5

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?

+3  A: 

The first thing that jumps to mind after reading just the title is surround.vim which is an excellent script to do all kinds of useful things along the lines of what you've described.

To solve your specific problem, I would probably position the cursor on the + and:

s[space]+[space][esc]

To change 3==4 into 3 == 4, I might position the cursor on the first =, and:

i[space][esc]ww.

Greg Hewgill
I do like that second solution of yours because it is seems to work on any token. Plus the double w is easier to type that two different keys.
William Becker
+3  A: 

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 +

Sarah
Nice - I'm still getting used to the fact that deleting things puts them in the paste buffer. And I've been using vi for 10 years... This is why I'm forcing myself to learn it properly finally! sed is next...
William Becker
+1  A: 

Try positioning your cursor over the '+' and typing this:

q1i[space][right arrow][space][left arrow][esc]q

This will record a quick macro in slot 1 that you can re-use whenever you feel like it, that will surround the character under the cursor with spaces. You can re-call it with @1.

There is also the more versatile one:

q1ea[space][esc]bi[space][right arrow][esc]q

Which will surround the word under the cursor ("==" counts as a word) with spaces when you hit @1.

amphetamachine
As I said, I know I could do it with a macro can do it, but I was wondering if there was some smart command that I didn't know about!
William Becker
+1  A: 

You could create a macro with one of the described actions and call it everytime you need it (Like amphetamachine proposed while I was writing) or you could simply search & replace:

:%s/\(\d\)\(+\|-\)\(\d\)/\1 \2 \3/g

You probably have to execute this command two times because it will only find every second occurence of +/-.

EDIT:

This will replace everything without the need to be called twice:

:%s/\d\@<=+\|-\d\@=/ \0 /g
Till Theis
A: 

You could set up a mapping like this (press enter in visual mode to wrap spaces):

:vnoremap <CR> <ESC>`<i<SPACE><ESC>`>la<SPACE><ESC>h
too much php