Search code examples
vimeditorbraces

How to insert matching brace in vim MANUALLY?


There is some text in my buffer

[{(lorem) ipsum <cursor here>

How can I insert closing brace }, then type something, and then insert ] matching with opening braces { and [

Let me explain why I need this.

For example I have to type some pure javascript code (because the coffee machine is out of order at the moment)

$(document).ready(function(){
  var classA = (function(){
     function classA(){}
     <100 lines of code here>
     return classA;
  -->oh, which closing brace should I insert here....!

Another case

[theWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", HostName, @"some/path/here"]] -> huh, what next? How many closing braces am I missing?

And why I clarify MANUALLY in the question title. Because there are some key bindings to automatically insert closing braces. For example in ( ()<Left>. However, I want to insert closing brace only when I need.

I know there is i_<C-x>_<C-o> to insert closing xml tag. But I don't know how to deal with these ones.

Thank you in advance.


Solution

  • Finally, I come up with this vim-close-pair plugin.

    Just type <Ctrl-L> (insert mode), it will find and insert the missing brace automatically.

    • Install with Vundle:

      • Append Plugin 'nissasssin17/vim-close-pair to your .vimrc file
      • Restart vim or run :source ~/.vimrc
      • Run :PluginInstall
    • Manual install:

        git checkout https://github.com/nissassin17/vim-close-pair.git
        cd vim-close-pair
        mkdir -p ~/.vim/plugin
        cp -f plugin/close-pair.vim ~/.vim/plugin/
        mkdir -p ~/.vim/autoload
        cp -f autoload/close-pair.vim ~/.vim/autoload/