29

When i'm trying to paste some code from browser to Emacs, it will indent code automatically, is there any way to stop Emacs from indenting temporarily like :set paste in vim?

Kara
  • 6,115
  • 16
  • 50
  • 57
ZelluX
  • 69,107
  • 19
  • 71
  • 104
  • 5
    Are you pasting into a terminal? This shouldn't happen if you're pasting into the GUI Emacs which gets the paste "directly"; when you're pasting into the terminal it's effectively like typing all those characters one-by-one. – ShreevatsaR Jun 12 '09 at 13:25
  • 1
    Does this happen to in all modes/buffers? Or just some? Have you tried pasting into Emacs when you start emacs w/out your customizations (ala `emacs -q`)? – Trey Jackson Jun 12 '09 at 16:37
  • Didn’t know about 'paste'—thanks! – andrewdotn Jun 13 '09 at 02:43
  • 2
    Prefixed `C-u C-y` should yank without indentation. Works for me with Emacs 25 – 4e6 Mar 27 '17 at 13:03
  • @4e6 why did you put this in as a comment? This is a valid answer and easier than Jouni K. Seppänen's and I missed it on the first read. – Bae Sep 18 '17 at 06:02

5 Answers5

39

The easiest way with emacs24 is:

M-x electric-indent-mode RET

That disables auto indentation.

Paste your thing.

renable

M-x electric-indent-mode RET

Or just M-x UP-Arrow ;-)

Maresh
  • 4,644
  • 25
  • 30
  • I'd love to be able to add a hook so that when I paste, this is automatically disabled and then reenabled. Anybody have any ideas? – mattsilver Nov 17 '16 at 21:11
  • I think you could by having your own key binding overriding C-c and calling a function that does this, calls the paste from CUA or other https://www.emacswiki.org/emacs/CuaMode – Maresh Nov 20 '16 at 22:37
14

Switch into the *scratch* buffer (or just to some nonexistent buffer; it will be in Fundamental mode, which shouldn't do any autoindentation unless you have somehow configured it to do so), type C-SPC to start the region, paste your text, type C-w to cut it within Emacs, switch back to your original buffer, type C-y to paste.

viam0Zah
  • 25,949
  • 8
  • 77
  • 100
Jouni K. Seppänen
  • 43,139
  • 5
  • 71
  • 100
  • This is a nice workaround, though not *quite* the same thing. For example, I note that this still gives a warning for mismatched parenthesis, when pasting in text (into a terminal-based Emacs instance) that has a smiley. ( `:)` ) It'd be nice to have some sort of "mode" (using that term very loosely) that turns all such things off (because they can make the pasting take a long time, if it's a large chunk of text). Still, this gets around some aspects, at least -- and clearly solves the original asker's main problem. – lindes Mar 24 '12 at 12:08
  • This doesnt work for a lot of inputs.The reason being when you paste windows/dos encoded text, the \r\n forces the appearance of a tab. Switch the file on windows to unix line endings if you can, and it stops – easytiger Aug 13 '15 at 10:14
  • Pasting into the scratch buffer removes all indentation for me. I would like to preserve the indentation that was originally there in the copied text. The solution which disables electric-indent-mode works. – ishmael May 16 '17 at 18:46
  • @ishmael The `*scratch*` buffer defaults to Lisp Interaction mode. Manually change it via `M-x fundamendal-mode` – Bae Sep 18 '17 at 05:57
  • This is the only solution that appears to work for me. – user989762 Sep 13 '18 at 07:58
5

A quick workaround for Python is to paste into a """ ... """ block.

Matthijs Douze
  • 106
  • 1
  • 3
5

Prefixed yank command C-uC-y would yank without indentation. Works with Emacs 25.

4e6
  • 10,696
  • 4
  • 52
  • 62
  • @Bae glad my comment helped, so I made an answer from it. – 4e6 Sep 18 '17 at 09:24
  • 1
    @ 4e6 here I am 2 years later googling for the same thing, and still missing this answer :) At least I've upvoted it now so I will find it 2 years from now... – Bae Jul 30 '19 at 23:47
0

You could try

c-toggle-syntactic-indentation
mamboking
  • 4,559
  • 23
  • 27