3

If I paste a piece of code from the browser to emacs -nw with middle mouse button, or Ctrl+Shift+v (in gnome-terminal paste from clipboard), it's incorrectly indented.

Original code:

for (i=0; i<=10; i++) {
    j = j + i;
    print j;
}

Becomes:

for (i=0; i<=10; i++) {
          j = j + i;
                print j;
                    }

In vim one would turn off auto-indent. In emacs I tried 2 ways that doesn't work:

Method 1:

Configure ctrl+y to use PRIMARY selection in ~/.emacs

(setq x-select-enable-primary t)

And try to paste with Ctrl-y instead of middle-mouse-key or Ctrl+Shift+V. Result: ctrl-y only pastes from emacs' kill ring, not from external selection.

Method 2: Unmap RET from newline-and-indent:

(add-hook 'lisp-mode-hook '(lambda ()
  (local-set-key (kbd "RET") 'newline)))

It works for typing RET (no indent), but for middle-mouse-key or Ctrl+Shift+V auto-indent is applied anyway.

  • Do ^H m and read about configuring auto-fill-mode or ^U 0 ESC X auto-fill-mode to turn it OFF. – waltinator Sep 6 '15 at 3:47
  • Having auto-fill-mode disabled doesn't help. The extra auto-indent can still be reproduced. My understanding is that, for the extra indents, RET - by default defines to newline-and-indent - is at work instead of auto-fill-mode ? – Tankman六四 Sep 7 '15 at 4:25
4

Autoindentation occurs in emacs when the newline character is encountered. You can use electric-indent-local-mode and electric-indent-mode to toggle this setting.

From http://emacsredux.com/blog/2013/03/29/automatic-electric-indentation/

What’s little known is that Emacs 24.1 introduced a new global minor mode called electric-indent-mode. When enabled, typing certain characters(like newlines) triggers reindentation.

  • 1
    I'm running emacs 24.3.1 on Ubuntu, and turning off electric-indent-mode doesn't prevent the indentation. Even after also doing the add-hook suggested by @Zhang Weiwu. – TextGeek Apr 25 '16 at 18:46
  • electric-indent-local-mode worked for me. – ychaouche Apr 1 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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