Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 339
eat_a_lemon Avatar asked Apr 07 '11 17:04

eat_a_lemon


People also ask

How do you paste in vim on a Mac?

The standard Mac paste shortcut, Command-v, should paste the contents of the clipboard into vim, running in a terminal window, assuming vim is in insert mode.

How do you paste into Terminal in Vim?

You can use a movement command or up, down, right, and left arrow keys. Press y to copy, or d to cut the selection. Move the cursor to the location where you want to paste the contents. Press P to paste the contents before the cursor, or p to paste it after the cursor.

How do I paste in Terminal Mac?

To copy something on Mac, select it and press Command + C . This copies your data or item to clipboard, and you can paste it where you need it. Use Command + V to paste. If you want to remove a piece of text or a file as you are copying it, simply swap out the Command + V for Command + X .

How do I use vim in OSX Terminal?

You can start Vim by typing vi or vim in the terminal, or vim in graphical mode by typing gvim on Linux or macvim on the Mac. Doing so should bring up a blank screen, with details about Vim. However, most attempts to type text will fail! You are in the wrong mode!


1 Answers

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.

like image 102
Chris Page Avatar answered Sep 24 '22 20:09

Chris Page