I use vim with ruby. I have a habit of opening and closing brackets when I define new function or a block.

For example I would type:

array.map {}_ # _ - cursor position after finishing typing

So I am still in Insert mode I have a mapping imap jk <esc> to get me out to normal mode. Cursor is going back under the closing bracket and I can press i to start typing inside the brackets.

My question is how would I go around creating the mapping <leader>i that would insert spaces automatically on both sides of the cursor?

array.map {}_ # jk<leader>i
array.map { _ }

I think, instead of:

array.map { _ }

It would be better to have:

array.map { _}

That is, the cursor is on the second space. Then, whatever you'd type would have a space after it. And a mapping for that would look like:

nnoremap <leader>i i  <esc>i
  • i, followed by two spaces
  • an <esc> to go back to normal, at which point the cursor would be on the second space,
  • another i to go back to insert mode.

If you have arrow keys working in insert mode, you could use this:

nnoremap <leader>i i  <left>

You could simply do:

inoremap <leader>i i<space><space><left>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.