1

I want to map a Shift + Insert, as I usually paste code for studing (or even take some of stackoverflow's questions to prepare an answer). But as mapped for .cpp and .h the pasted code appears messed up.

Example:

1  class A {
2 }
3 public:
4     class B {
5     }
6             // ...
7             //     };
8             //         static void f();
9             //             // ...
10             //             };
11             //             )

Original code from question Import nested classes into namespace - C++:

class A {
public:
    class B {
        // ...
    };
    static void f();
    // ...
};

To avoid this is necessary to run: :set paste.

So, the question is, how to map a Shift + Insert, just like imap <S-Insert> <Esc>:set paste<CR>i and very here pass foward the Shift + Insert to its natural behavior, that is pasting and then turn back and set :set paste! back?

Obs.: { is mapped to break line and put and ending }, along with that kind of maps the pasted text will be messed up.

Thanks.

Community
  • 1
  • 1
Rodrigo Gurgel
  • 1,696
  • 2
  • 15
  • 32

2 Answers2

3

From :help 'paste':

Since mapping doesn't work while 'paste' is active, you need to use the 'pastetoggle' option to toggle the 'paste' option with some key.

So the solution is to e.g.

:set pastetoggle=<F12>

and to use the following sequence when pasting: <F12>i(paste)<Esc><F12>

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
1

Vim's own special registers * and + allow bypass the shaky pasting through terminal. "*p pastes from X selection register and "+p pastes from clipboard. In Windows these are both linked to same IIRC.

mike3996
  • 17,047
  • 9
  • 64
  • 80