I have tried to disable the auto indentation in Emacs in JavaScript for three hours now and could not find a (good) solution.

My Emacs version is 23.3.1 (i486-pc-linux-gnu, GTK+ Version 2.24.3), at least it says that. It is a standard package from Debian Linux (Wheezy), the GUI one.

I found emacs javascript auto indentation (and others), but did not want to install another mode just for removing auto indentation (and there's no garantuee it would work with it), the fundamental mode disables syntax highlighting as well, which I want to keep, C-c C-l does nothing, setting javascript-auto-indent-flag to nil does nothing.

What I want exactly: Insert a tab if and only if I press the TAB key. The "if" part works, but the "only if" part does not. I wonder why it's so hard?

link|improve this question
feedback

2 Answers

You say you do not want TAB's unless you pressed the TAB key. Does that mean you are happy with the indentation but want lines to be indented using spaces rather than TAB's? Or do you not want any indentation at all?

link|improve this answer
I do not want automatic indentation at all. I could accept automatic indentation on a new line (if I press <ENTER>, the next line is indentend as the current line). What Emacs does (and what I do not want) is changing the indentation of a line when I insert certain characters at the end of a line. That sucks, because I indent a line properly, add a { at the end, Emacs changes indentation and I have to fix it. The TABs are fine, I do not want to indent with spaces (and I would know how to do it). – GodsBoss Jul 29 '11 at 16:08
feedback

In Emacs' Javascript mode, the character } is bound to function js-insert-and-indent. You can bind it to just insert itself by putting this code into your .emacs startup file:

(defun my-js-mode-hook ()
  "My personal Javascript mode hook."
  (local-set-key (kbd "TAB") 'tab-to-tab-stop)
  (local-set-key (kbd "RET") '(lambda () (interactive) (newline 1)))
  (local-set-key (kbd "SPC") 'self-insert)
  (local-set-key (kbd "}") 'self-insert))

(add-hook 'js-mode-hook 'my-js-mode-hook)

and restarting Emacs. If you find other characters like that, you can add more calls to local-set-key in your hook function, each with a different character passed to the kbd function.

link|improve this answer
I've updated my Emacs-Lisp code above to increase the chances that it solves your problem. Please give it a try. – Fran 15 hours ago
feedback

Your Answer

 
or
required, but never shown

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