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
<<Spelling checkers for: Dutch, English, German, Hungarian, and Yiddish |
Mappings to facilitate the creation of text >>
Additional Notes
|