Return to previous mode in Emacs - emacs

Is there a key binding to quit a mode and return to the previous mode in emacs?
For example suppose I entered line number mode using the following command:
Alt+x linum-mode
How can I quickly disable this mode and return to the mode which I was in before (not using the same command again)?

Why wouldn't you want to use the same command again?
M-xM-pRET
It doesn't get much simpler than that.
Edit: You can repeat M-p in that sequence to step back further in the command history, and you can search the command history with M-xC-r.
Also, when you disable a minor mode you're not "returning to the mode you were in before"; you're just disabling one (of many) minor modes which are all active at the same time.
Tangentially, the concept of "returning to the mode you were in before" could apply to major modes (as there's only ever one active major mode in a given buffer), but strictly speaking there's no notion of 'disabling' a major mode -- only of 'enabling' the one you wish to change to -- so to 'toggle' between two major modes, you would need to call them alternately.

Just repeat the same command: M-x linum-mode. Such minor-mode commands are toggles: on/off.

C-x z calls repeat - repeat last command.
Repeatedly calling a minor-mode enables/disables it.
One more way to do it is with smex: the last command
that you called with M-x kinda sticks around.
So you can enable linum-mode with smex, do a bunch of editing
with usual shortcuts and then disable linum-mode with
M-x RET.

A solution might follow the path kill-ring-save works: store the modes being active as current-modes-listing in a previous-modes-ring.
The code needed therefor exists basically inside describe-mode, see upward from "Enabled minor modes" - respective for the major mode.
Then a hook should check if this-command has "-mode" in it's name. If yes, check, if current modes-listing equals car of previous-modes-ring. If not, add new setting.
Finally write a command which sets current modes according to selected listing from previous-modes-ring.

Related

Some questions about emacs mode

As we know, mode is very important in emacs. But I feel I am not very clear about how to set it. For example, I often see something like (***-mode 1) or (***-mode) in .emacs file. And some tutorials also say that a mode can be set by M-x ***-mode. Could you tell me what's the differences between them and how to use them? Thanks!
A Lisp function is a piece of code which declares a name for another piece of code to be executed later.
(defun hello ()
(message "Hiya!"))
Now, you can invoke the named code from anywhere else in Lisp.
(hello)
Only at this point does the message form get executed.
Many Lisp functions contain an interactive form which specifies how they should behave when called interactively (for example, should it prompt for an argument, or use the cursor or mouse position as the argument, etc). Those which do can be invoked with M-x and the function name.
A major mode specifies a function which sets up some variables to exclusively control the behavior of Emacs. For example, M-x text-mode sets a (very basic) regime for word wrapping and cursor movement which is suitable for text files. When you are in text mode, you cannot be in C++ mode, or Lisp mode, or fundamental mode. These are other major modes which define different or additional functionality suitable for editing other types of text.
Because a major mode is exclusive, it is usually a function which doesn't take any arguments. So to put the current buffer in text mode, the Lisp code is simply
(text-mode)
Minor modes, by contrast, specify additional behavior which is independent from the major mode. For example, Overwrite mode specifies a different behavior when inserting text before some other text -- normally, Emacs pushes any existing text ahead, but when overwrite mode is active, existing text in front of the cursor will be replaced as you type.
You can have multiple minor modes active at any time -- you could have flyspell (spell checking as you type), tool bar mode, menu bar mode, and line number mode active at the same time as you are in text mode and overwrite mode.
Because of this, a common (though not universal) convention for minor modes is to perform a toggle. When you are already in toolbar mode, M-x toolbar-mode will disable this minor mode. To unambiguously disable the mode, pass it a negative numeric argument;
(toolbar-mode -1)
Without the argument, the code will toggle -- the result will depend on whether the mode was already active, or not.
(As noted in a comment, this changed in Emacs 24; I'm describing the historical behavior.)

emacs - stop adding indentation to current and next line when pressing RET

I have a file open called test.scss, and when I press RET on a line, emacs will add 2 spaces to the current line and 4 extra spaces to the next line.
I've highlighted what the file looks like with whitespace-mode.
before pressing RET
after pressing RET
You can see that the .my-element row was auto-indented by 2 spaces, and the new line is indented by 4 spaces too many.
I want the output to look like this instead
desired output
What can I do to make emacs produce my desired output?
Here is the output of describe-mode:
Enabled minor modes: Auto-Composition Auto-Compression Auto-Encryption
Electric-Indent File-Name-Shadow Font-Lock Global-Eldoc
Global-Font-Lock Line-Number Menu-Bar Tooltip Whitespace
(Information about these minor modes follows the major mode info.)
SCSS mode defined in `css-mode.el':
Major mode to edit "Sassy CSS" files.
In addition to any hooks its parent mode `css-mode' might have run,
this mode runs the hook `scss-mode-hook', as the final step
during initialization.
Although in this case I'm in scss-mode, I see similar behavior with most of the other modes I use, such as ruby-mode, sgml-mode, js.el mode and others. I'd like to make the behavior match the desired output shown above.
Each mode can handle indentation in its own way, and you may have to look for mode-specific settings to get two-space indentation working everywhere.
For starters, you can set css-indent-offset, which should cover css-mode and scss-mode:
(setq css-indent-offset 2)
You can set the basic indenting of many other modes similarly. ruby-mode seems to use ruby-indent-level, sgml-mode uses sgml-basic-offset, and js-mode uses js-indent-level.

Shift selection in Emacs 24 does not highlight text

I must be stupid, but I just switched to Emacs 24, and holding Shift while moving the point no longer highlights text. Mouse selection works as before. What am I missing? Did I turn it off by chance? Or has Shift selection been deemed unergonomical so we have some other, better keyboard-based selection at hand?
Shift selection is enabled by default in Emacs 24.
You can always run emacs -Q to disable your init file and any other default libraries, in order to determine what Emacs' default behaviour is.
You can also run emacs -q which will disable only your init file (other system-wide init files can be loaded).
If the feature works without your init file and does not work with it, you can then start to narrow down what part of your init file is at fault (often by commenting out functionality until the feature starts working as expected -- note Drew's comment).
In this case I suggest that you firstly verify (using C-hv) that the shift-select-mode variable is non-nil (when running with your normal configuration), as a nil value means that shift selection is disabled.
I also found it useful to look at load-path variable, C-h v load-path, which directed me to the culprits, old elpa packages.

call emacs commande automatically

I would find a way to execute the commands in the file emacs. Emacs and therefore automatically.
For example I often use: highlight-80
So I'm forced to type every time: Meta key + highlight-80 +-fashion
it's the same with linum-mode and plenty of other.
I have been trying to put in the file emacs.:
(highlight-80 +-mode)
But the option is not enabled.
Thank you in advance for your help. I am looking desperately for a moment, emacs is my working tool quotidient.
Regards
Use C-h f or C-h v, and read the Emacs manual about such choices.
Some of them are user options (variables), whose values you can customize, using M-x customize-option, so the default setting becomes what you want.
Others are modes, which you can call/set in your init file (~/.emacs) --- see the Emacs manual for how to do that. Typically, you use a positive number to turn a mode on and a negative number to turn it off. E.g.: (menu-bar-mode -1) in your init file turns off the use of a menu bar.
In sum, the Emacs manual (C-x r) is your friend. Sit down and have a first chat with it.
You seem generally a bit unsure about how customising Emacs works, so reading the manual on this topic should probably be your next step:
http://www.gnu.org/software/emacs/manual/html_node/emacs/Customization.html
If you are not using the current stable release (Emacs 24.3 at the moment), there's a chance that some of that information will not apply. The in-built manual is, of course, always correct for the version you are running:
C-hig (emacs) Customization RET

Standard behavior for C-x in CUA mode

I recently switched from vi to emacs. I like CUA mode. However, CUA mode makes C-x kill the active region. This is annoying when I want to, say, clear a rectangular selection with C-x r c.
How can I use CUA mode while having completely out-of-the-box behavior for C-x?
If you look at the help for cua-mode it gives you options for using commands which conflict with the prefix keys (like C-x):
If you really need to perform a command which starts with one of
the prefix keys even when the region is active, you have three
options:
- press the prefix key twice very quickly (within 0.2 seconds),
- press the prefix key and the following key within 0.2 seconds, or
- use the SHIFT key with the prefix key, i.e. C-S-x or C-S-c.
Use cua-selection-mode instead of cua-mode.
The former provides CUA's other configuration and facilities (for example the rectangle editing mentioned by Francesco), but it leaves C-x, C-c, C-v, and C-z alone, meaning that it's highly compatible with out-of-the-box Emacs usage.
In addition to jtahlborn's general answer, please also note that cua-mode reimplements rectangular commands (IMHO in a much fancier way than the standard C-xr commands)
Just press C-RET to enter rectangular selection mode. From there regular commands work on the rectangular selection: you can for example delete the region using DEL.