21

How do I completely disable all auto-indentation in Emacs? I need to disable it at least for Fundamental mode, but I'm fine with it being disabled globally for the current session.

Please read the rest of this question before declaring it a duplicate. All the other answers I've found are for much more targeted disabling of indentation. I really want to completely disable all auto-indentation for the whole session.

Setting electric-layout-mode to nil does not work.

Details

Sometimes I like to compose large Git commit messages with my Windows ASCII text editor. These messages frequently have bullet lists, code blocks, or other things where the precise formatting in my source document is meaningful. My codebase is on Linux, and I connect to the box using a text-only SSH client. When Git enters Emacs for editing the commit message, I want to be able to copy the source commit message to the Windows clipboard then paste it using Windows paste commands. From the perspective of Emacs, I'm typing what was found in the Windows text file. Unfortunately, when Emacs sees indented lines, it adds its own auto-indentation, so I get this huge triangle of whitespace along the left edge of the buffer.

I'm using GNU Emacs 24.3.1.

Workaround

My current workaround is to transfer my Windows text file to a filesystem that's mounted by my Linux host, load the file into Emacs, then use kill and yank to transfer the content. This is a bit clunky.

Example

Here's an example block of text:

a

  - b
    c
    d

Here is how it appears when I paste that into Emacs-under-ssh via MSWin paste:

a

  - b
      c
          d

If I press C-h l after doing the paste, here is what I see. Note that the newlines are input using C-j, not RET.

a C-j C-j SPC SPC - SPC b C-j SPC SPC SPC SPC c C-j
SPC SPC SPC SPC d C-j C-h l
3
  • Inasmuch as some of the indent related variables are buffer-local, there would need to be something that runs after the major-mode hook -- e.g., perhaps [?] hack-local-variables-hook -- to essentially undo what other major-modes set as part of their standard initialization. fundamental-mode is just a little function inside simple.el -- i.e., (kill-all-local-variables) (run-mode-hooks). M-x find-function RET fundamental-mode RET -- you can modify/create your own as needed; however, if you change the name, it will not be used by some functions -- so consider keeping the same name.
    – lawlist
    Commented Jul 28, 2015 at 15:46
  • Related: How to disable auto-indentation of new lines? Commented Jul 31, 2015 at 20:12
  • 1
    Does emacs have an equivalent of vim's :set paste? That basically says "don't touch the pasted text in any way". Which also includes auto-indentation.
    – omajid
    Commented Oct 11, 2016 at 22:32

2 Answers 2

25

Emacs ≤24.3

Pressing RET doesn't indent by default (except in some programming modes, for which you need to check mode-specific configuration, e.g. “electric” settings).

Unix terminals normally paste a carriage return character when the clipboard contents contains a newline (represented by the line feed character). In Emacs, a carriage return looks like the RET key (in fact, RET is the carriage return character), while a line feed looks like (is) C-j.

However, in your case, pasting a newline results in a C-j, which in Emacs is bound to the command newline-and-indent. As the name indicates, this command indents. I don't know if PuTTY has a setting you can change. If you want to fix the problem on the Emacs end, there's an easy workaround which I think you won't mind: bind C-j to newline (same as RET).

(global-set-key "\C-j" 'newline)

Emacs ≥24.4

In Emacs 24.4, Electric Indent mode is switched on by default. It's a global minor mode, so you can turn it off everywhere by calling (electric-indent-mode 0) and you can toggle it interactively with M-x electric-indent-mode RET.

You can find this information in the manual, or by looking at the documentation of the command invoked by pressing Return (press C-h k RET).

You can turn it on in a specific buffer with electric-indent-local-mode. Some programming modes (especially third-party ones) may have their own automatic indentation features that don't follow the settings of Electric Indent mode.

6
  • 1
    electric-indent-mode is disabled by default (this is consistent with what you said because I'm using Emacs 24.3.1). Toggling it on and off doesn't help. electric-indent-local-mode isn't available to me via M-x. Help on RET mentions three settings: use-hard-newlines, auto-fill-function, and fill-column. Toggling or modifying those via M-x use-hard-lines, M-x auto-fill-mode, and M-x set-fill-column RET 0 RET don't help.
    – Mr Fooz
    Commented Jul 28, 2015 at 22:13
  • @MrFooz Oh, sorry, I hadn't paid attention to the Emacs version in your question. But then I wonder why you're getting automatic indentation. Hmmm. Do you get automatic indentation with emacs -q? Please paste some short text containing a newline (say foo newline bar) into Emacs and press C-h l, what do you see? … f o o RET b a r or … f o o C-j b a r or … f o o RET C-j bar or something else? What terminal do you run the SSH client in? Commented Jul 28, 2015 at 22:25
  • I've added an example text blurb to the original post. Adding -q does not help (e.g. emacs -q -nw --no-desktop). I see C-j instead of RET. I am using PuTTY 0.63 as my SSH client. I see the same behavior with TERM set to screen and xterm.
    – Mr Fooz
    Commented Jul 29, 2015 at 13:44
  • 1
    Thanks, the global-set-key hack fixed the problem. I'll figure out a way to setup my EDITOR aliases to automatically invoke it on startup when editing Git commit messages. Newlines are being pasted instead of carriage returns because I've configured my favorite Windows text editor to use UNIX-style line endings instead of Windows ones.
    – Mr Fooz
    Commented Jul 29, 2015 at 16:03
  • 1
    Disabling electric-indent-mode works perfectly for me on Emacs 24.5.1. Thanks. Commented Jul 4, 2016 at 18:39
1

You might also try bracketed paste mode. This is a feature of your terminal emulator (PuTTY in this case, versions 0.63 and up) which uses escape sequences to signal that the user is pasting something, and then again when the paste ends. There's an Emacs package called bracketed-paste which you can install, it enables Emacs to recognize the escape sequences and transition to a safer keymap for the duration of the paste. Not only does this prevent the indentation from getting messed up, it also prevents a rogue paste from running commands in your emacs session using M-x.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.