Emacs reselect region, as Vim shortcut 'gv' does - emacs

In vim, visual block can be recall by 'gv' command so that multiple commands can be applied easily. (such as, comment out, then indent, then do_something_fun).
In Emacs, how can this be achieved?
[C-xC-x] only works when current cursor position stays where previous block ended.
If previous block was changed, the closest is to go through 'point-to-register' and 'jump-to-register'.
Just I am curious if there is an Emacs built-in command making this in one shot.

If Transient Mark mode is off, the region is always active. If it's on (which it sounds like is your situation), you can set mark-even-if-inactive to non-nil to allow region commands to work while the region isn't highlighted.
However, note you also can cycle back through previous mark positions using C-u C-SPC -- this will pop the mark ring. Once you're back to where you want to be, C-x C-x will rehighlight the region you want. (It may take a little bit of playing with this feature to get a feel for it, but it's why I can't switch away from Emacs now.)

If I understand correctly what you are asking for, then you don't need to do anything. When you select a region in emacs, it stays selected until you select a new one. So you could select the region and then perform as many actions as you want.

Sounds like you're looking for the secondary selection, which stays put even as the region might change. (It stays put until you move it.)
See:
the Emacs manual, node Secondary Selection
Emacs wiki page Secondary Selection
library second-sel.el:

Also narrow-to-region (CTRL-x n n ) applies every command from then on just to that region- you can't hurt the rest of the buffer, it doesn't even show. After done editing , widen (CTRL-x n w )to get back the whole buffer.
CMM

If you use evil-mode, just press gv like in vim.

Since the answers here and for other similar SO questions didn't help for me (CUA-mode, Emacs 24, not only indent-rigidly), I continued searching and finally found a reselect-last-region defined in this collection of custom function (starting line 670). That worked like a charm for me - and hopefully does for others still arriving here.

Related

Emacs - How to enter recursive edit mode when searching (C-r) instead of query-replace (M-%)?

I know how to enter recursive edit mode when query-replace (M-%), but sometimes I really don't want to modify the code instead I'm just in searching. Such as when I was reading code, and from one point I saw a function call, then I want to jump to the declaration of the fucntion (C-r Enter PageList::Init), when I have read the declaration I want to jump back to the calling of the function.
How to enter recursive edit mode when searching (not only when query-replace)?
You can open a recursive edit while you are searching, if you use Isearch+. Then, C-x o is bound during Isearch to command isearchp-open-recursive-edit:
Invoke the editor command loop recursively, during Isearch.
Use C-M-c to end the recursive edit and resume searching from there.
Or use abort-recursive-edit to exit the recursive edit and cancel the previous search.
If you do not want to use Isearch+ then this is all you need:
(defun isearchp-open-recursive-edit ()
"Invoke the editor command loop recursively, during Isearch.
Use `\\[exit-recursive-edit]' to end the recursive edit and resume searching from there.
Or use `abort-recursive-edit' to exit the recursive edit and cancel the previous search."
(interactive)
(with-isearch-suspended (recursive-edit))))
(define-key isearch-mode-map "\C-xo" 'isearchp-open-recursive-edit)
If you are asking about recursive editing generally, and not just during search, then the answer is command recursive-edit. Just use M-x recursive-edit to open a recursive edit. You can also bind this command to a key, of course.
(If the minibuffer is active, then you normally cannot use M-x recursive-edit. In that case, bind a key in the minibuffer keymaps to recursive-edit. Or you can set option enable-recursive-minibuffers to non-nil, to be able to invoke M-x recursive-edit from a minibuffer.)
You enter a recursive edit by calling recursive-edit, but there is no default binding which invokes this anywhere (and doing so arbitrarily might be hazardous).
In the case of query-replace there is some special-case handling which goes on when you type C-r which saves the current match data/point/mark/buffer/window configuration before entering the recursive edit, to ensure that it can restore things when you exit from it.
This need to wrap some situation-specific custom handling around calls to recursive-edit is very common to the existing use-cases, so there might not be a safe way to invoke it generally.
My guess is that custom handling would certainly also be needed to support it during an isearch, and I see no such facility.
Do note that Emacs pushes point to the mark ring when you begin an isearch; so for your specific use-case you would simply jump to & pop the mark with C-uC-SPC
From your workflow description, I'm not sure you really need recursive edit mode.
When you do a search in emacs, the current point is saved. On my system C-r is reverse isearch. When I hit it, I'm prompted for the search string and hit enter. This takes me to the first 'hit'. Hitting C-r againi takes me the next hit etc. Once you find the text you want, provided you don't hit enter, you can read the code and then hit C-g to cancel the search. This will jump your cursor back to the point where you started the search. This is fine provided you can see all the code you want to read on screen.
When you can't read all the code on screen and you need to move around in the buffer, you have to hit enter. Once you do this, you have lost the saved point and I suspect this is where you thought of using a recursive edit mode so that you can jump back once you finish. However, this won't work quite as you want because as soon as you hit enter to select the searched for string, you will come out of recursive mode and lose the saved point.
There are a couple of ways to fix this workflow to achieve what you want. In fact, your workflow is quite a common requirement. Because of this, many programming modes already have this functionality built in. Therefore, the first thing to do would be to ensure your mode doesn't already have this - it is probably called something like jump to definition or similar.
If your mode doesn't have this support, then you can get what you want by saving the point and then jumping back to it later. This is a really handy technique. All you need to do is C-SPACE twice. Then later, after moving the point to a new locaiton, you can just do a C-u C-SPACE to jump back to that point. From the emacs manual
Instead of setting the mark in order to operate on a region, you
can also use it to “remember” a position in the buffer (by typing
‘C-SPC C-SPC’), and later jump back there (by typing ‘C-u
C-SPC’). *Note Mark Ring::, for details.

Emacs multiple-cursor - Cut and yank in multiple lines

Using the 'multiple-cursor' emacs package,
when I mark a word and cut ('C-w') in different multiple cursors, the only word in the 'active' cursor I control is cut.
I want to select different words in multiple positions and yank it to other relative position.
When I first tried this I remember some yes or no option at the screen bottom, but I couldn't really notice this at first and some keystroke made it to "no". I guess this might have been an option for multiple-cut. But I don't know how to revert this choice.
How do you do multiple cut and yank in emacs?
This works by default, so at some point you have indeed asked the library not do do this.
These settings are stored (by default) in ~/.emacs.d/.mc-lists.el
Just edit that file appropriately, and then M-x eval-buffer RET to make those same changes in the running instance.

How to format clojure in emacs [duplicate]

I'm new to Clojure and new to Emacs.
Is there an Emacs short-cut to intelligently re-indent the whole file?
if not, is there at least a way to indent selected regions left or right?
I feel like I'm back in the stone age repeatedly pressing the arrows
C-x h selects the entire buffer. C-M-\ reindents the selected region.
Ctrl-x, h (select all) followed by Tab (to indent)
cider-format-buffer command (Since cider 0.9.0)
When you capture data from a sequence like C-u C-c C-e
(cider-eval-last-sexp), the raw data output to your buffer can be
unwieldy to inspect/work with. And the normal code-indenting commands
(mentioned in answers here) don't handle it well.
For handling results from such evaluated expressions, try
cider-format-edn-region.
As a concrete example, have you ever tried reformatting your
~/.lein/profiles.clj? This is pretty hard to do and keep
consistent, until you discover cider-format-edn-region. Take
caution that it will, however, remove any comments.
Use cljfmt for many configurable ways to reformat/reindent. It has an Emacs plugin, but also can be run via lein.

How to make mouse click set the point, not the mark in aquamacs

In Aquamacs 2.2 I could do the following:
1) Set the mark with C-SPACE
2) Click somewhere to set the point
Consequently this would define the region. Since Aquamacs-2.3 this behaviour is gone: A click sets both the mark and the point. Is there a way to customize aquamacs to have the old behaviour?
For Emacs 24, here's what worked for me (and may also apply to Aquamacs): Unset the binding for the <down-mouse-1> event, which is what is setting the mark (but not the point; the point is set by a different binding, for the up-event <mouse-1>).
That seems to provide the behavior you are asking for: clicking the mouse after setting the mark via C-space sets the point, and one can see the corresponding region highlighted.
I determined this by reading the help for the bindings above, by doing:
M-x helpkclick, and reading the *Help* text, which explains that <down-mouse-1> is bound to command mouse-drag-region (which sets the mark).
I then tested the resulting behavior by interactively disabling the binding, by doing:
M-x global-unset-key and then clicking; but it is probably much safer to do it programmatically, e.g. in the scratch buffer, by evaluating:
(global-unset-key [down-mouse-1])
If you like the behavior that results, then just copy the above line into your .emacs
Employing the solution described above does seem to cause mouse-dragging to be a little funky, in that it does not highlight the selected region during the drag (but you still get to see what the region is once you let go, and you can now fine tune its end point via single clicks, which may be useful in some circumstances.)

How do I intelligently re-indent Clojure in Emacs?

I'm new to Clojure and new to Emacs.
Is there an Emacs short-cut to intelligently re-indent the whole file?
if not, is there at least a way to indent selected regions left or right?
I feel like I'm back in the stone age repeatedly pressing the arrows
C-x h selects the entire buffer. C-M-\ reindents the selected region.
Ctrl-x, h (select all) followed by Tab (to indent)
cider-format-buffer command (Since cider 0.9.0)
When you capture data from a sequence like C-u C-c C-e
(cider-eval-last-sexp), the raw data output to your buffer can be
unwieldy to inspect/work with. And the normal code-indenting commands
(mentioned in answers here) don't handle it well.
For handling results from such evaluated expressions, try
cider-format-edn-region.
As a concrete example, have you ever tried reformatting your
~/.lein/profiles.clj? This is pretty hard to do and keep
consistent, until you discover cider-format-edn-region. Take
caution that it will, however, remove any comments.
Use cljfmt for many configurable ways to reformat/reindent. It has an Emacs plugin, but also can be run via lein.