Disable auto indent globally in Emacs - emacs

How to disable auto indent in Emacs globally or only for some modes?
I have a number of packages installed for RubyOnRails (ruby, html, js, css).
Let's say I want to disable autoindent for css-mode.

For me, on emacs 24.x, M-xelectric-indent-mode toggled the behavior that I wanted to disable.
FWIW, the behavior was that RET was bound to the command newline which is defined in simple.el... Among other things, the behavior of that command is altered by electric-indent-mode.

You may want to look for variable names containing the word electric. (This is the common Emacs parlance for actions which occur automatically when particular visible characters are typed.)
In this instance, M-x apropos-variable RET electric RET shows me that there is a css-electric-keys variable containing a list of "Self inserting keys which should trigger re-indentation."
You could use M-x customize-variable RET css-electric-keys RET to set this list to nil, or add (setq css-electric-keys nil) to your init file.
Sometimes a minor mode is used to implement electric behaviours, so that you can switch them on and off more easily. Those would likely be found via M-x apropos-command RET electric RET, and you would probably use a major mode hook to ensure that the electric minor mode was disabled, in a similar fashion to this:
(add-hook 'MAJORMODE-mode-hook 'my-MAJORMODE-mode-hook)
(defun my-MAJORMODE-mode-hook ()
(ELECTRICMODE-mode 0))

Related

Emacs 24.5 , CUA mode. Not paste text in the minibuffer

In Windows 7 / Emacs 24.5
Copy text e.g. "example" in the kill-ring
M-x
C-y (yank)
Success show text "example" in the minibuffer
But if turn on CUA-mode, the text "example" not yank (paste) by 'C-v' in the minibuffer.
CUA mode makes C-v the yank/paste command.
If you start Emacs with :
emacs -Q
Then turn on CUA mode (M-x cua-mode) you'll see that C-v works as you expect.
Without knowing your setup it's difficult to be sure but it's likely you're using a package which modifies the behavior of M-x (E.g. smex, Ido, ivy, etc.)
It's likely CUA mode won't really have anything to do with this problem. You can verify this by trying to do C-y to yank in the minibuffer too.
Packages which enhance M-x may provide a way to allow you to drop out temporarily, so you can yank text in-place.
Update
From your comments we know you are using Helm, which overrides some bindings in the minibuffer, including C-v which is bound to page down.
Because bindings are applied at different mode (context) scopes, the minibuffer modemap (list of key bindings) will override anything that's applied at a more general context (such as cua mode)
To work around this you'd need to add a binding specifically for cua-paste in the affected mode map. It would need to be applied after Helm has loaded.

Emacs: How to bind key only in regular buffers and not in the minibuffer?

I have written a fancy function, which I would like to bind to TAB. The functionality is only meaningful in any non-read-only text buffer. Currently, I bind it either like that:
(global-set-key (kbd "<tab>") 'my-indent-region)
or
(define-key global-map (kbd "<tab>") 'my-indent-region)
The problem with this binding is that now tab-completion does no longer work in the minibuffer, which is an essential feature (e.g. for buffer/file names, or M-x).
Is it possible to bind TAB only for regular modes? I know that I can use define-key some-major-mode-map, but since I want it in all modes except for the minibuffer, this would be annoying to maintain. Thus, I'm probably looking for something like a define-key any-mode-except-minibuffer ....
If such a functionality does not exist: Is there a workaround to get the tab-completion working in the minibuffer again? Maybe I can re-set the original minibuffer tab binding after changing the global binding? I couldn't figure out though which function I actually have to bind to make it work.
After some more research I found a workaround/solution to the problem in this answer.
Apparently, my problem was that I was binding to (kbd "<tab>"). If I understand it correctly, my problem was in fact not that I overwrote the actual keymap of the minibuffer -- I guess they are correctly loaded when entering the minibuffer minor modes. However, there seems to be a precedence of a binding to (kbd "<tab>") over a binding to just "\t". According to the above answer, the minibuffer bindings just use "\t", so binding to (kbd "<tab>") shadows them. I'm now using the following binding instead:
(global-set-key "\t" 'my-indent-region)
Now everything seems to be working fine.
Do you see this behavior when you start Emacs without your init file (emacs -Q)? I doubt it. If not, then recursively bisect your init file to find out what is causing the problem.
The minibuffer uses its own keymaps, which are local and which therefore take precedence over global keymap bindings.
However, any minor-mode keymaps take precedence over local keymaps. So if, for example, you have a (global) minor mode turned on that binds <tab> then that will override any binding for that key in the minibuffer keymaps.
Another thing you can do is simply bind whatever command you want to <tab> in the minibuffer keymaps. But again, you should not need to do that, if you want the usual <tab> behavior for the minibuffer.
[Another possible confusion: Some things, such as Isearch, which you might think use the minibuffer do not use it. Isearch uses its own keymap, isearch-mode-map.]
UPDATE after your comment:
Assigning a key in the global map, as you have done, should not affect what that key does in the minibuffer, provided it has a different binding in the minibuffer keymaps. TAB is typically bound in all of the minibuffer completion keymaps (but not in the non-completion minibuffer keymaps).
See the Elisp manual, nodes Completion Commands and Text from Minibuffer for information about the minibuffer keymaps.
To see what the current bindings are for a keymap that is associated with a variable (such as minibuffer-local-completion-map), load library help-fns+.el and use C-h M-k followed by the keymap variable's name. (See Help+ for more information about the library.)
If you do not want TAB to use your global command binding in the non-completion minibuffer maps (minibuffer-local-map, minibuffer-local-ns-map), then just bind it in those maps to whatever command you like. But for the completion maps you should not need to do anything - TAB should already be bound there.
Did you try emacs -Q, to see if something in your init file is interfering? If not, do that first.

How to know emacs true mode names?

Sometimes I copy configuration options from the Internet to my .emacs. Sometimes they don't work.
(add-hook 'laTeX-mode-hook 'turn-on-flyspell)
doesn't work:
(add-hook 'LaTeX-mode-hook 'turn-on-flyspell)
(notice the uppercase in Latex). But,
(add-hook 'flyspell-mode-hook 'flyspell-buffer)
is correct. Although Emacs shows "Fly" in the lower bar and also M-: major-mode shows latex-mode and not Latex-mode.
How do I know how to write emacs modes name?
It sounds like you're actually more interested in the names of modes' hook variables than in the modes themselves.
You have several options. I will suggest some, based around discovering flyspell-mode-hook:
M-x apropos-variable RET flyspell RET, then search the results buffer for hook
C-h v flyspell-, then tab-complete
Any time you are in the minibuffer tab-completion is a good thing to try
M-x find-function flyspell-mode RET will open up the source code for flyspell, you can then search for hook
If you have configured your Emacs to provide completion for Emacs Lisp, you can simply type
(add-hook 'flyspell-
into your .emacs buffer and let Emacs suggest valid completion
Tools like Helm and ido can simplify the process of finding things
Using the find-function technique with latex-mode (which I tab-completed), I discovered that my version of Emacs calls its LaTeX mode function latex-mode. Searching for LaTeX- showed me that LaTeX-mode is an alias for latex-mode.
Your use of M-: major-mode is good, and gives you the correct major mode name (i.e. the symbol name for the mode function).
I don't believe there's a standard function to list the symbol names for the current buffer's enabled minor modes, but you can see all (loaded) minor mode symbols with C-hv minor-mode-list, so it's not hard to verify a name if you find you need to.
The symbol name for a mode's hook is literally the mode's symbol name with the suffix -hook.
Minor modes also have (in addition) an -on-hook and -off-hook.
The hook variables don't necessarily exist when not in use, but this naming is hard-coded in the standard macros for defining modes (and running their hooks at the appropriate times); and the modes which don't use those macros invariably follow the same conventions, to ensure consistency.

Remove Emacs highlighting

I use the emacs command 'highlight-compare-buffers' to see the changes between two files. But I don't know how to turn the highlighting off. It seems like it should be really easy, but I can't seem to figure out how even with lots of googling and searching the emacs help files.
Try:
C-u -1 M-x highlight-compare-buffers
global-highlight-changes is an
interactive autoloaded Lisp function
in `hilit-chg'.
(global-highlight-changes &optional
arg)
Turn on or off global Highlight
Changes mode.
When called interactively:
if no prefix, toggle global Highlight Changes mode on or off
if called with a positive prefix (or just C-u) turn it on in active mode
if called with a zero prefix turn it on in passive mode
if called with a negative prefix turn it off
I don't have emacs installed on this box, so this is untested, but highlight-changes-toggle-visibility might work.

How do I turn off vhdl-mode in emacs?

I am learning emacs at the moment and tried to write an easy vhdl program for testing. I can see that the vhdl-mode might be an interesting feature, but I would like to know how I can turn it off for the moment and how I can reactivate it later on.
Use the command M-x fundamental-mode, that is:
Press (and hold) the meta key (which is usually the Alt key)
Press x
This will take the cursor into the echo area at the bottom of the screen/frame. Type fundamental-mode and press return.
To disable VHDL mode permanently, you will have to change the file-extension mapping used by emacs to associate a file's extension with a particular major mode. You can do this by writing a custom .emacs configuration file. Look for auto-mode-alist in the emacs manual:
(setq auto-mode-alist (remove (rassoc 'vhdl-mode auto-mode-alist) auto-mode-alist))
Change to some other mode, e.g.
M-x fundamental-mode RET
or
M-x indented-text-mode RET
re-enable it by entering
M-x vhdl-mode RET