ChatGPT解决这个技术问题 Extra ChatGPT

Pasting code into terminal window into vim on Mac OS X

When I paste code into my Mac OS X terminal window into vim it indents each line. For each line it adds an indent so the text looks like this...

"ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud        
   ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
        reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
             Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
                    deserunt mollit anim id est laborum."

My current workaround is I paste the text first into textmate text editor which keeps the correct formatting. Then I save that file and open it up in vim. Then I use vim yank to paste it. Is there a setting in my .vimrc that could change this behavior? Or is this a terminal issue?

Bracketed paste solves this without the need to call other commands such as :set paste. Bracketed paste is available by default in vim 8. But it will only be enabled if vim thinks you are in a Xterm compatible terminal. There are several ways to enable it see this answer on vi.stackexchange.

C
Chris Page

UPDATE: Vim 8 includes native support for Bracketed Paste Mode. It is enabled by default. See Vim’s xterm-bracketed-paste help topic. Users no longer need to do anything to configure Vim to support this.

As of Mac OS X Lion 10.7, Terminal supports “bracketed paste mode,” which enables the terminal emulator to tell the program connected to the tty when the user pastes text, so that the program won’t interpret it as editing commands. Programs that support it send the terminal an escape sequence to enable this mode, in which the terminal surrounds pasted text with a pair of escape sequences that identify the start and end.

To enable this in Vim, put the following code in your ~/.vimrc file:

if &term =~ "xterm.*"
    let &t_ti = &t_ti . "\e[?2004h"
    let &t_te = "\e[?2004l" . &t_te
    function! XTermPasteBegin(ret)
        set pastetoggle=<Esc>[201~
        set paste
        return a:ret
    endfunction
    map <expr> <Esc>[200~ XTermPasteBegin("i")
    imap <expr> <Esc>[200~ XTermPasteBegin("")
    vmap <expr> <Esc>[200~ XTermPasteBegin("c")
    cmap <Esc>[200~ <nop>
    cmap <Esc>[201~ <nop>
endif

This makes it so that when Vim switches the terminal to/from the alternate screen† (t_ti, t_te) it enables/disables bracketed paste mode (ESC [? 2004 h, ESC [? 2004 l). When it receives the escape sequence indicating the start of a paste (ESC [ 200 ~), it enables Paste mode (set paste) and switches to Insert mode if necessary ("i"). When it receives the matching end-of-paste marker (ESC [ 201 ~) it disables Paste mode (pastetoggle) and remains in Insert mode. The cmap commands arrange for the Vim command line to ignore the escape sequences and accept the pasted text as-is.

Note that this only enables bracketed paste mode when the $TERM value starts with "xterm…"; if you're setting $TERM to something else, you may want to revise that test to include your $TERM value. Or, you could omit the test altogether, since it isn’t strictly necessary—it’s just trying to be careful not to do something that might be incompatible with some other terminal type.

In Terminal, this works with all the various Paste commands, as well as drag-and-drop.

† The terminal has a main screen and an "alternate" screen. Each screen has its own contents and state. Text in the alternate screen does not scroll up into the scrollback log. It is typically used by programs that take over control of the whole screen and are therefore referred to as "full screen" programs. This includes vim, emacs, less and top, for example.

As noted by @DenilsonSáMaia this answer has been packaged into a plugin; although, it is obsolete starting in Vim 8: https://github.com/ConradIrwin/vim-bracketed-paste


You should also add a mapping for command mode (the on you enter with a colon). Otherwise you loose the ability to paste into the command line: cmap <Esc>[200~ <nop> cmap <Esc>[201~ <nop>
@ChrisPage some additions to speed up the esc mapping and make it work in tmux: github.com/aaronjensen/vimfiles/blob/master/vimrc#L449-483
This answer has been packaged into a plugin: github.com/ConradIrwin/vim-bracketed-paste
If you don't use the alternate screen, you can enable it in (just) insert mode by modifying t_SI and t_EI instead of t_ti and t_te (respectively).
J
John Bachir

Within vim:

:set paste

Put Vim in Paste mode. This is useful if you want to cut or copy some text from one window and paste it in Vim. This will avoid unexpected effects.


I'd put it on a toggle; namely; I have this in my vimrc... set pastetoggle=
D
Denilson Sá Maia

Another way to do this, assuming you have your system clipboard set up properly is to do

"+p

This will paste from the system clipboard.


"*p or "+p. The * register is the X11 primary selection, and + is the clipboard. I don't know if Mac OS X has the same concept as X11.
d
dancavallaro

In addition to the other answers, if you want a quick way to toggle paste mode, add

set pastetoggle=<F2>

to your .vimrc. Now you can toggle paste mode by pressing F2 (or whatever key you choose).


I also set a pastetoggle to but do so by having F2 call a function. That is because when in paste mode I also want vim to unset "showbreak", so that I can cleaning copy from the editor window.
Note: I also make good use of "listchars" (toggle using set "list") so when not in paste-mode, I can see no-break spaces, tabs, and extra spaces on the end of the line. This normally hidden characters cause me a lot of greif, and listchars lets me know they are their. There are also a number of other hidden unicode space characters vim does not 'show'.
W
WenbinWu

In vim

:set paste

when you want to disable it

:set nopaste


Or :set paste! to toggle it.
C
Community

When working inside a terminal the vim-bracketed-paste vim plugin will automatically handle pastes without needing any keystrokes before or after the paste.

This works in Terminal, iTerm2, and any "modern" x-term compatible terminals that support bracketed paste mode. As an added bonus it works also for tmux sessions. I am using it successfully with iTerm2 on a Mac connecting to a linux server and using tmux.

The plugin is basically a packaged version of the functionality that @Chris Page listed in his answer.


Vim-8 has it built in. BUT also does a insert-paste if you try and past while in command mode. That is you can no longer paste vim commands! Something I have been doing for more than 30 years (from back in 'vi' and 'elvis' days)
Vim 8 has it built-in... from Patch 8.0.0210 but Debian's current stable, Stretch, doesn't include that patch, only included selected patches past 0197.

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now