/enwise

Enterwise bracket completion utility for Vim.

Primary LanguageVim ScriptMIT LicenseMIT

enwise

A vim plugin that allows you to use Enter key to close unbalanced brackets.

I always feel a bit awkward when using auto-pairing plugins in Vim. The auto-generated closing brackets not only save no keystrokes (advancing past the closing brackets requires typing the same keys), but also cause a lot of distractions when coding. What enwise does is simple: it keeps your currently-typing lines untouched, but will close any unbalanced brackets after you have pressed the Enter key, so you gain the advantages of auto-pairing and eyes-concentration.

Installation

Install enwise with your favorite plugin manager, e.g vim-plug:

Plug 'mapkts/enwise'

or install manually by coping this repo to ~/.vim/pack/vendor/start/ (Vim 8+ only).

Features

  • Free you mind from closing unbalanced brackets { ( [.
  • Respect your tab size and space size when inserting newline.
  • Smart enough to escape brackets inside quoted string.
  • Behave soundly when you press enter in the middle of a line.
  • Brackets inside comments like // { /* { */ are normally escaped.

Examples

  • Write a line of code from left to right ( | stands for cursor position )

before

sort_by('hello', function (x) {|

after

sort_by('hello', function (x) {
  |
})
  • Brackets inside quoted string are automatically escaped

before

println!("brackets: {{([ {} {}"|

after

println!("brackets: {{([ {} {}"
    |
)
  • Press enter inside non-eol brackets will push contents after the cursor down two lines

before

sort_by('hello', function (x) {|})

after

sort_by('hello', function (x) {
  |
})
  • Brackets inside comments will also be escaped

before

// {([|

after

// {([
// |

before

/*
 * {([|

after

/*
 * {([
 * |

Customization

  • enwise only closes brackets for a list of supported languages (see here). In order to bypass this limitation, put let g:enwise_enable_globally = 1 in your vimrc to enable it globally.
  • By default this plugin takes control of the enter key if <CR> hasn't been mapped yet. If you want to overload the enter key, put let g:enwise_disable_mappings = 1 in your vimrc before creating a conditional mapping. e.g.:
" Use <CR> to navigate completion menu (if pumvisible) or close brackets.
imap <silent><expr> <CR> pumvisible() ? "\<C-N>" : "\<CR>\<Plug>(EnwiseClose)"
  • Calling :EnwiseToggle will toggle enwise on and off. You might want to temporarily disable this plugin when writing some syntax extension code (e.g. Rust macros). Note that you can create a mapping for this command like so:
nnoremap <leader>te :EnwiseToggle<CR>
  • Put let g:enwise_auto_semicolon = 1 in your vimrc to allow enwise inserts needed semicolons after brackets when you hit Enter. This is a experimental feature currently.

Bug Report

Currently the languages enwise supported is limited. If you have encountered any bugs or inconvenience when using this plugin, please don't hesitate to open a issue.