9

When I learned to program, I would press both the opening and closing brackets/etc then the left arrow key then enter the text. So to put in [hello] I would type [], and then the left arrow so that it is over the first bracket ([).

I like that because I know I always have the right number of brackets/etc and otherwise I feel like my hand has to move back and forth to the bracket/etc key

But, in sticking with the Vim mindset, I don't want to continually reach for the arrow keys. Is there anything I could do that didn't involve using the arrow keys or having to continually switch out of insert mode to just press h and go back in?

6 Answers 6

5

You might be looking for the auto-pairs plugin:

When you type a character like {, ( or [ this plugin will automatically add the symetric character and replace your cursor in insert mode between the two brackets. It is also able to be "smart" while deleting those characters.

Also if I may give you an advice forget the arrow keys. Whatever you want to do, there is always a faster motion accessible without moving your hands from the home row. It may take some time to get used to it, but once it is an habit you won't even miss them.

6
  • Thanks for the comment and, if you don't mind me asking; do you type "[" then "hello" then "]". Without the surround plugins (which does seem like a great idea, thanks) it seems sort of cumbersome to go back and forth even without the problem of potentially losing the right number of brackets
    – JZL003
    May 27, 2015 at 0:44
  • Well actually I always use either auto-pairs or surround.vim (see the answer of Peter) but if I had to do that without plug-ins, I would do [, ] , <Esc> (which will place the cursor on the ] ) and finally i which will place the cursor between the brackets in insert mode.
    – statox
    May 27, 2015 at 5:24
  • @statox when in insert mode, how do you move your cursor two positions to the left? With ctrl-o 2h? Is this faster than moving your hand to the cursors? ;)
    – Vitor
    May 27, 2015 at 14:26
  • @Vitor Nope I'll hit the Esc key to go to normal mode, for a movement of less than 3 characters i'll probably hit several time h or j and otherwise I'll use other movements. It feels faster to me, but I understand that some people feel it in a different way :)
    – statox
    May 27, 2015 at 15:10
  • @statox after 10 years using vim, I'm still struggling to use hjkl and specially in this scenario I just described. Thanks for your comments! :)
    – Vitor
    May 27, 2015 at 16:17
8

There are plenty of "pair" plugins that do this for you automatically. However all of them rub me the wrong way. I tend to use Tim Pope's surround.vim plugin. I do the following: <c-s>] in insert mode to insert [] with the cursor in the middle of the brackets.

1
  • more people need to know about this, vim surround is more useful than just wrapping things. Also, I feel the pair plugin malaise, very aggressive and end up wasting keystrokes to format
    – rgalbo
    Feb 10, 2018 at 20:23
4

If you just need to move one space, ctrl-o in insert mode makes your next input a normal command, so ctrl-o, h will move back one. The modeline will display --- (insert) --- until you input the normal command.

2

lh-brackets provides insert, normal and visual mode mappings to insert pair of brackets.

Notes:

  • Adding new bracket pairs is quite easy.
  • The insert mode mappings are context sensitive -- i.e. not expanded within string or comment contexts, ...
  • Unlike surround plugin, the surrounding mappings are intuitive, but not in vim style. i.e. hitting ( in visual mode will surround (by default), we don't have to hit si( or similar sequences.
2

Previous answers are very good. But I think you might want something easier. Personally I use h j k l for moving around. Therefore, I have added to my .vimrc the following:

inoremap <c-j> <esc>ji
inoremap <c-k> <esc>ki
inoremap <c-h> <esc>i
inoremap <c-l> <esc>2li

Just a small tip. You said that your procedure is i, [], <Esc>hi.

There is Operator-Pending Mappings, Which means you can ci[ or di[ or ii[.

1

I'm personally not a big fan of automagic insertion of characters; you can easily define a set of mappings to insert text & more the cursor to where you want:

" Add brackets with closing bracket
inoremap <C-a>[ []<Left>
inoremap <C-a>( ()<Left>

" For C & C-like languages
inoremap <C-a>{ {<Cr><Cr>}<Up>

" For Ruby
inoremap <C-a>def def<Cr>end<Up>

Note that a plugin like ultisnips also does stuff like this, but more advanced. For example in Javascript I can type:

if<Tab>hello

and I get:

if (hello) {

}

You can add a simple snippet for completing the brackets; this is a special .snippet file that lives in ~/.vim/UltiSnips/all.snippets:

snippet (
(${0})
endsnippet

snippet [
[${0}]
endsnippet

snippet {
{
${0}
}
endsnippet

${0} is the cursor position.

see :help UltiSnips-adding-snippets for more information.

Note that ultisnips requires Python; another well-known plugin is vim-snipmate. The Vim Wiki has a full list of snippet plugins.

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.