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?

  • 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 2: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 6:02

The easiest way with emacs24 is:

M-x electric-indent-mode RET

That disables auto indentation.

Paste your thing.


M-x electric-indent-mode RET

Or just M-x UP-Arrow ;-)

| improve this answer | |
  • 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? – ohruunuruus 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 emacswiki.org/emacs/CuaMode – Maresh Nov 20 '16 at 22:37

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.

| improve this answer | |
  • 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 5:57
  • This is the only solution that appears to work for me. – user989762 Sep 13 '18 at 7:58

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

| improve this answer | |

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

| improve this answer | |
  • @Bae glad my comment helped, so I made an answer from it. – 4e6 Sep 18 '17 at 9: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

You could try

| improve this answer | |
  • c-toggle-syNtactic-indentation – PanCrit Jun 12 '09 at 17:13
  • Thanks for catching the typo. Fixed. – mamboking Jun 14 '09 at 1:35

Your Answer

