Vim logo vim online Vim Book Ad

basic Tip #153: Making Parenthesis And Brackets Handling Easier

 tip karma   Rating 76/36, Viewed by 1535 

created:   November 4, 2001 11:56      complexity:   basic
author:   Joachhim Hofmann (Schuttberg)      as of Vim:   5.7

1) ++++++++++++++++++++++++++ "Automatic" bracket setting +++++++++++++++++++++++++++++
2) +++++++++++++ Further improvement of parenthesis/bracket expanding +++++++++++++++++
3) ++++++++++++++++++++++++++++ "Late" bracketing of text +++++++++++++++++++++++++++++
4) +++++++++++++++++++++++++++++ Conclusion ++++++++++++++++++++++++++++++++++++++ ++++

=======================================================================================

1) ++++++++++++++++++++++++++ "Automatic" bracket setting +++++++++++++++++++++++++++++

To automatically insert a closing parenthesis when typing an opening
parenthesis you can insert the following simple mapping to your vimrc:

:inoremap ( ()<ESC>i

This ends up with the cursor between the opening and the closing parenthesis
in insert mode.

You can apply this and the following tips, of course, with the kind of parenthesis/bracket
character you want to, i.e. (, {, [, < ..... and, pretty useful as well,
quotation marks ",',.... (to be continued)

2) +++++++++++++++ Further improvement of parenthesis/bracket expanding ++++++++++++++++++

I you are ready with filling the parenthesis/brackets, you likely want to
"escape" from the brackets again to continue coding.
To make this pretty comfortable, I invented the following kind of mappings, which get out
of the last expanded parenthesis/bracket, regardless of the actual type of it, and
enter append mode again.
I mapped this kind of "getaway" with CTRL_j, you may use your favorite keystroke with it.

...
:inoremap ( ()<ESC>:let leavechar=")"<CR>i
:inoremap [ []<ESC>:let leavechar="]"<CR>i
...
:imap <C-j> <ESC>:exec "normal f" . leavechar<CR>a

Explanation: The variable "leavechar" contents the actual char which is to "escape" from.


3) ++++++++++++++++++++++++++++ "Late" bracketing of text +++++++++++++++++++++++++++++

Occasionally I later want already written text parts to put in parenthesis.

I use the following macro, which brackets previously visually selected text.
I mapped it with _(.

:vnoremap _( <ESC>`>a)<ESC>`<i(<ESC>

Furthermore, a sort of mapping for bracketing a *single word* is conceivable.
Because this is not as general like the kind of visual mode mapping, I use
this kind of "word bracketing" only for surrounding the word right behind the cursor in insert mode with **.  I use the following macro to "emphasize" the word i just typed,
for newsgroup articles.

:imap _* <Esc>bi*<Esc>ea*<Space>


4) ++++++++++++++++++++++++++++++ Conclusion ++++++++++++++++++++++++++++++++++++++++++

Since I use these macros, I never caused a syntax error because of missing
brackets, and furthermore I can quickly insert parenthesis and qutotes into code-
and non-code files.

JH 04.11.2001

 rate this tip  Life Changing Helpful Unfulfilling 

<<Spelling checkers for: Dutch, English, German, Hungarian, and Yiddish | Mappings to facilitate the creation of text >>

Additional Notes

[email protected], July 31, 2002 12:12
Great tip!  Led me to create this mapping:

    inoremap { {<CR><BS>}<ESC>ko

to be used in conjunction with my autoindent setup:

    set expandtab
    set shiftwidth=4
    set smarttab
    set autoindent
    set smartindent

Now, any time i type a '{', this is what i get:

    {
        <cursor here ready for coding>
    }

Great time saver and it ensures that i don't ever miss a brace.  And it even works
for you crazy people who put the open brace on the same line as the for/while/function name ;)
atkinssc AT engr orst edu, August 6, 2002 7:31
Well in the last note, someone suggested a mapping that created:
{
        | <= Cursor
}

Although this is fine and dandy, f will not search anything but the current line; so I remapped it to use:
imap <c-j> <esc>:exec "/" . leavechar . "/"<cr>A

Now what would be great is if, when you hit ) or ] or } when you've already filled in the end charcter, to seek to the end and place your cursor outside of the block.

Any ideas?
[email protected], October 1, 2002 17:17
I think I'm missing something.  When I try to use the first tip, :inoremap ( ()<ESC>i, I get the text "()<ESC>i" when I type "(".
[email protected], November 9, 2002 11:53
You probably run vim in 'compatible' mode. (not recommended).
This happens usually when you have no _vimrc created.
Try renaming the file vimrc_example to .vimrc (UNIX) or _vimrc (Windows/DOS),
or set the option nocompatible: ":set nocompatible".

Achim
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo