{"id":317,"date":"2013-02-28T01:51:55","date_gmt":"2013-02-28T01:51:55","guid":{"rendered":"http:\/\/tech.moosaico.com\/blog\/?p=317"},"modified":"2013-03-04T10:42:09","modified_gmt":"2013-03-04T10:42:09","slug":"vim-safe-copy-paste-without-bad-indentation","status":"publish","type":"post","link":"https:\/\/thejoyofstick.com\/blog\/2013\/02\/28\/vim-safe-copy-paste-without-bad-indentation\/","title":{"rendered":"Vim safe copy paste without bad indentation"},"content":{"rendered":"<h2>The Horror<\/h2>\n<p>One thing that was always great with Linux desktop X11 (and continued with Ubuntu 12.10) was copying text with a mouse selection and just pasting it with the middle button. That&#8217;s why I became horrified when it started to not work with Vim!<\/p>\n<p>I was evaluating a <a title=\"Dancer::Plugin::Auth::Tiny\" href=\"https:\/\/metacpan.org\/module\/Dancer::Plugin::Auth::Tiny\" target=\"_blank\">Perl Module<\/a> and when I copy\/pasted a block of an example code this happened<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/borked-paste-500.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-325\" alt=\"Borked Paste\" src=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/borked-paste-500.png\" width=\"500\" height=\"320\" srcset=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/borked-paste-500.png 500w, https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/borked-paste-500-300x192.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>It has to be a way to have a clean copy\/paste again!<\/p>\n<h2>The Solution<\/h2>\n<p>Fortunately Vim has the notion of paste and so has a mode to activate it and ignore indentations and other features. You just activate it with <code>:set paste<\/code><\/p>\n<p><a href=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/normal-set-paste-500.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/normal-set-paste-500.png\" alt=\"normal-set-paste-500\" width=\"500\" height=\"28\" class=\"alignnone size-full wp-image-332\" srcset=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/normal-set-paste-500.png 500w, https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/normal-set-paste-500-300x16.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/paste-mode-500.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/paste-mode-500.png\" alt=\"paste-mode-500\" width=\"500\" height=\"28\" class=\"alignnone size-full wp-image-333\" srcset=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/paste-mode-500.png 500w, https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/paste-mode-500-300x16.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>Change to insert mode<\/p>\n<p><a href=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/paste-mode-insert-500.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/paste-mode-insert-500.png\" alt=\"paste-mode-insert-500\" width=\"500\" height=\"27\" class=\"alignnone size-full wp-image-334\" srcset=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/paste-mode-insert-500.png 500w, https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/paste-mode-insert-500-300x16.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>and now just paste the text the way it was supposed.<\/p>\n<p><a href=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/paste-ok-500.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-326\" alt=\"Paste Ok\" src=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/paste-ok-500.png\" width=\"500\" height=\"320\" srcset=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/paste-ok-500.png 500w, https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/paste-ok-500-300x192.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>Hurray! Now press <code>Esc<\/code> and get back to our helpful indentations with <code>:set nopaste<\/code>.<\/p>\n<p><a href=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/paste-set-nopaste-500.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/paste-set-nopaste-500.png\" alt=\"paste-set-nopaste-500\" width=\"500\" height=\"27\" class=\"alignnone size-full wp-image-336\" srcset=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/paste-set-nopaste-500.png 500w, https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/paste-set-nopaste-500-300x16.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/normal-set-nopaste-500.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/normal-set-nopaste-500.png\" alt=\"normal-set-nopaste-500\" width=\"500\" height=\"27\" class=\"alignnone size-full wp-image-331\" srcset=\"https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/normal-set-nopaste-500.png 500w, https:\/\/thejoyofstick.com\/blog\/wp-content\/uploads\/2013\/02\/normal-set-nopaste-500-300x16.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<h2>Less typing, may I ?<\/h2>\n<p>Of course there&#8217;s a pragmatic way of doing all these. We can define a keyboard key to just make the copy\/paste a safe operation. I&#8217;m using key <code>F12<\/code>.<\/p>\n<pre lang=\"vim\">\r\n:set pastetoggle=<F12>\r\n<\/pre>\n<p>In insert mode you can toggle the paste mode so you don&#8217;t despair with the wrong indentation. Add it to your <code>.vimrc<\/code><\/p>\n<p>Another way it&#8217;s to just paste what you want into the line you are at in visual mode.<\/p>\n<pre lang=\"vim\">\r\nmap <F12> :r! xsel<CR><Esc>\r\n<\/pre>\n<p><code>xsel<\/code> is a command line X11 tool to manipulate its &#8220;clipboard&#8221;.<br \/>\nThis mapping reads into Vim the output of <code>xsel<\/code> which is the clipboard copy buffer. I&#8217;m using this one.<\/p>\n<p>You may need to install it but it&#8217;s simple<\/p>\n<pre lang=\"linux\">\r\n$ sudo apt-get install xsel\r\n<\/pre>\n<h2>Conclusion<\/h2>\n<p>Vim customization and plugins broke a plain simple copy\/paste. However we got a no brain and fast solution with our <code>F12<\/code> key.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Horror One thing that was always great with Linux desktop X11 (and continued with Ubuntu 12.10) was copying text with a mouse selection and just pasting it with the middle button. That&#8217;s why I became horrified when it started&hellip; <a href=\"https:\/\/thejoyofstick.com\/blog\/2013\/02\/28\/vim-safe-copy-paste-without-bad-indentation\/\" class=\"more-link\">Continue Reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[7,3,11],"tags":[53,41],"class_list":["post-317","post","type-post","status-publish","format-standard","hentry","category-apps","category-linux","category-perl","tag-copypaste","tag-vim"],"jetpack_shortlink":"https:\/\/wp.me\/pcmwlL-57","jetpack_sharing_enabled":true,"jetpack_likes_enabled":false,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/thejoyofstick.com\/blog\/wp-json\/wp\/v2\/posts\/317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thejoyofstick.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thejoyofstick.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thejoyofstick.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thejoyofstick.com\/blog\/wp-json\/wp\/v2\/comments?post=317"}],"version-history":[{"count":37,"href":"https:\/\/thejoyofstick.com\/blog\/wp-json\/wp\/v2\/posts\/317\/revisions"}],"predecessor-version":[{"id":363,"href":"https:\/\/thejoyofstick.com\/blog\/wp-json\/wp\/v2\/posts\/317\/revisions\/363"}],"wp:attachment":[{"href":"https:\/\/thejoyofstick.com\/blog\/wp-json\/wp\/v2\/media?parent=317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thejoyofstick.com\/blog\/wp-json\/wp\/v2\/categories?post=317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thejoyofstick.com\/blog\/wp-json\/wp\/v2\/tags?post=317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}