vimtricks.wiki Concise Vim tricks, one at a time.

How do I automatically close brackets and quotes as I type?

Answer

auto-pairs plugin

Explanation

auto-pairs automatically inserts closing brackets, parentheses, and quotes when you type the opening one. It mimics behavior from modern editors.

How it works

  • Type ( and ) is automatically inserted
  • Type " and a closing " is inserted
  • Cursor is placed between the pair
  • Pressing ) when the cursor is before ) jumps over it

Example

Typing func( produces func(|) with the cursor between. Typing "hello produces "hello|" with the cursor before the closing quote.

Tips

  • Install: Plug 'jiangmiao/auto-pairs'
  • Works with (), [], {}, "", '', ``
  • <BS> deletes both characters if the cursor is between an empty pair
  • Can be configured per filetype
  • Alternatives: vim-closer, lexima.vim, nvim-autopairs (Neovim)

Next

How do you yank a single word into a named register?