Always use :set paste, Is it a good idea?

20,302

Solution 1

As others have written, you don't want to leave 'paste' set. I just wanted to point out that with a good terminal emulator and a properly compiled and configured vim, you shouldn't need to change 'paste'. You need a good terminal emulator such as xterm or GNOME Terminal, vim with the X11 feature included, and the 'mouse' option set to 'a'. Then vim will "know" when you're pasting with the mouse and will effectively set and unset the 'paste' option for you.

One way to get a vim with the X11 feature is to run gvim with the -v option or create an alias,

alias vim='gvim -v'

Then put

set mouse=a

in your ~/.vimrc.

Solution 2

This post has my favorite answer, https://coderwall.com/p/if9mda/automatically-set-paste-mode-in-vim-when-pasting-in-insert-mode

Basically if you start in Insert mode and use Ctrl+Shift+V or right click paste with your mouse, Vim detects that this came from a terminal and automatically sets paste mode, then unsets it once done, so you don't lose remapped keys (which can't work in paste mode because its writing raw data) and you are back to a "sane" state when it is done.

For just vim (put in your .vimrc)

let &t_SI .= "\<Esc>[?2004h"
let &t_EI .= "\<Esc>[?2004l"
inoremap <special> <expr> <Esc>[200~ XTermPasteBegin()

function! XTermPasteBegin()
  set pastetoggle=<Esc>[201~
  set paste
  return ""
endfunction

If you use vim under Tmux (still goes in .vimrc)

function! WrapForTmux(s)
  if !exists('$TMUX')
    return a:s
  endif

  let tmux_start = "\<Esc>Ptmux;"
  let tmux_end = "\<Esc>\\"

  return tmux_start . substitute(a:s, "\<Esc>", "\<Esc>\<Esc>", 'g') . tmux_end
endfunction

let &t_SI .= WrapForTmux("\<Esc>[?2004h")
let &t_EI .= WrapForTmux("\<Esc>[?2004l")

function! XTermPasteBegin()
  set pastetoggle=<Esc>[201~
  set paste
  return ""
endfunction

inoremap <special> <expr> <Esc>[200~ XTermPasteBegin()

Solution 3

As romainl suggested, the documentation explains that setting the 'paste' option disables several other options, and you will certainly find that sooner rather than later it will cause problems for you. For this reason, there is the 'pastetoggle' option. See:

:help 'paste'
:help 'pastetoggle'

Solution 4

IIRC when you paste into vim, it essentially thinks that you typed all those characters yourself. So if auto-indent is turned on, it will indent stuff for you, but the pasted text usually contains indentation already so the indent indeed gets "messed up". Switching to paste mode turns off stuff like auto-indent.

If you like auto-indent as-you-type, you should switch it back to nopaste after you're done pasting. Try it and note how you have to do all indentation manually in paste mode.

Solution 5

If something exists, it must have its meaning. You should take a good look at vim documentation which is very useful.

  :help 'paste'
  :help 'pastetoggle'

Once you read, you may want this :

" Toggle paste mode
"   (prefer this over 'pastetoggle' to echo current state)
nmap <leader>p :setlocal paste! paste?<cr>

I hope you find this post useful :)

Share:
20,302

Related videos on Youtube

Benjamin
Author by

Benjamin

직μž₯인듀을 μœ„ν•œ μ†Œκ°œνŒ… μ„œλΉ„μŠ€ μ»€ν”Όν•œμž”μ„ 개발, μš΄μ˜ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

Updated on September 18, 2022

Comments

  • Benjamin
    Benjamin over 1 year

    In a terminal vim, pasting clipboard data often messes up the code indent. I just knew if I uses :set paste the indent is not broken. Though, after pasting data, should I do :set nopaste again? What if I don't, what problem comes?

  • Lee Daniel Crocker
    Lee Daniel Crocker over 8 years
    When I set mouse=a, I cannot copy text. "Copy" is grayed out in the right-click menu and in the app menu.
  • garyjohn
    garyjohn over 8 years
    @LeeDanielCrocker: Copy will be grayed out or not visible at all unless some text has been selected. How are you selecting the text to be copied?
  • Lee Daniel Crocker
    Lee Daniel Crocker over 8 years
    Just with mouse. FYI, "y" works fine...yanks the text and copies it to system keyboard. It's just the menus that don't work. If mouse is set to "r", then the menus work, but the mouse includes the line numbers. So all in all, I can live with mouse=a, I just have to remember to yank instead of copy.
  • doub1ejack
    doub1ejack about 8 years
    I assume one of the let &t_SI .= "\<Esc>[?xxxxx" lines is detecting the keyboard paste shortcut? Does this work for mac as well given the different shortcut?
  • dragon788
    dragon788 about 8 years
    @doub1ejack These are actually detecting the Xterm escape sequences, not how they were called (ie 2004h is a paste begin) so as long as your terminal supports the Xterm keycodes and passes them through to Vim it should work fine on Mac.
  • maxwell
    maxwell almost 6 years
    omg. you are my hero. I've typed so :set nopaste so many g'd times.