3

I have a problem with vim when I paste something it puts them in by placing in each newline. How can I set default settings of vim.

1

If you paste from the system clipboard, vim thinks the pasted text is user input and, if confiured to provide autoindent, vim makes it's job and automatically indents the code.

You can turn off that autoindent when you paste code into vim directly. Type that in the command mode:

:set paste

Now, your pasted code should be written without indent. To re-enable autoindent again use:

:set nopaste

To simplfy that, you can define a shortcut in you .vimrc as follows:

set pastetoggle=<F2>

Now, when you press F2 in insert mode to have the desired behavior. Then press again F2, to work with autoindent as before.

1

I learnt this neat automation from coderwall, write the following into your .vimrc file

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

and it will switch on and off paste mode for you.

Using vim within tmux can be a little bit tricky, as you may (I use "may" because I do not have to double escape when I am inside tmux) have to double escape using

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()
0

You can paste directly from clipboard ("*) using vim's paste (p) instruction: "*p without having to enter INSERT mode.

Since the above won't work when you're connected to a remote terminal, chaos's suggestion would be your best option.

vim-unimpaired adds a nice shortcut yo and yO which automatically does set paste, insert a new line after/before the cursor's current line and then set nopaste once you exit INSERT mode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.