Lance
Lance

Reputation: 2913

How to match qoutes, doubleqoutes parenthesis brackets and braces in vim automatically?

I'm starting to use vim in my everyday coding. While I was exploring, I discovered the use of *map. So I decided to add the following mapping to my .vimrc file.

inoremap ' ''<left> 
inoremap " ""<left> 
imap ( ()<left>
imap { {}<left>
imap [ []<left>
imap < <><left>

The idea is to match every ',",(,{,[,< with its closing equivalent. The problem with that is that the mapping works even if I'm pasting while in insert mode.

// Pasting this 
()=>{ console.log("Hello World"); }

//Will result to something like this
())=>{} console.log())""Hello World"");}

What can I do to prevent that?

Upvotes: 0

Views: 65

Answers (2)

cecky
cecky

Reputation: 61

Give a try to the rainbow plugin. It highlights bracket pairs, and indicates the 'depth' you're in by changing the highlight colour.

Upvotes: 0

romainl
romainl

Reputation: 196526

This happens because you are pasting using your terminal emulator or desktop environment's shortcuts instead of Vim's: the text is not "pasted", it is "inserted", just as if you typed it very quickly, and insert mode mappings are triggered.

To prevent that, you can :set paste before pasting and :set nopaste afterward or you can use Vim's own y, p, and P commands.

For the first option, see :help 'paste' and :help 'pastetoggle'.

For the second option, see :help y, :help p, :help registers, and :help 'clipboard'. Note that you may need a Vim built with clipboard support.

Upvotes: 5

Related Questions