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?

5
  • 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. Commented Jun 12, 2009 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)? Commented Jun 12, 2009 at 16:37
  • Didn’t know about 'paste'—thanks! Commented Jun 13, 2009 at 2:43
  • 2
    Prefixed C-u C-y should yank without indentation. Works for me with Emacs 25 Commented Mar 27, 2017 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. Commented Sep 18, 2017 at 6:02

5 Answers 5

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 ;-)

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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
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.

5 Comments

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.
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
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 The *scratch* buffer defaults to Lisp Interaction mode. Manually change it via M-x fundamendal-mode
This is the only solution that appears to work for me.
5

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

Comments

5

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

2 Comments

@Bae glad my comment helped, so I made an answer from it.
@ 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...
0

You could try

c-toggle-syntactic-indentation 

2 Comments

c-toggle-syNtactic-indentation
Thanks for catching the typo. Fixed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.