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 Answers
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 ;-)
2 Comments
mattsilver
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?
Maresh
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
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
lindes
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.easytiger
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
ishmael
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.
Bae
@ishmael The
*scratch* buffer defaults to Lisp Interaction mode. Manually change it via M-x fundamendal-modeuser989762
This is the only solution that appears to work for me.
Prefixed yank command C-uC-y would yank without indentation. Works with Emacs 25.
emacs -q)?C-u C-yshould yank without indentation. Works for me with Emacs 25