ChatGPT解决这个技术问题 Extra ChatGPT

How can I select or highlight a block in Emacs?

I want to select or highlight a block in Emacs without using the mouse, but doing it from the keyboard like Vim's visual mode. What is the easiest way to do this from a keyboard?

See the article: "Working with rectangular selections", especially the comments section. See also the section of CUA mode documentation titled "CUA rectangle support". There's also a nice video on Vimeo.
C-M-h mark-defun
You can also use Spacemacs to get the vim keybinds inside emacs.

S
Svante

If I understand the question correctly, it is not about rectangular regions originally.

C-Spc puts a mark at the current position.

Wherever your cursor is afterwards, the text between the last mark and the current position is "selected" (you can highlight this by activating transient-mark-mode, but this will also mean that marks have to be deleted when you don't want highlight).

You can operate on that region with commands like:

C-w . . Kill region. This deletes and puts the region into the kill ring.
C-y . . Yank. This inserts the last snippet from the kill ring.
M-y . . Cycle kill ring. Immediately after C-y, this replaces the yanked part by the other snippets in the kill ring.
M-w . . Save region into kill ring. Like C-w, but doesn't delete.

This is just the basic usage. Marks have other uses, too. I recommend the tutorial (C-h t).


Worth noting that C-s C-r are commonly used in this context to go find the end/start of the region. (Especially useful in a macro.)
Well, it is very good commands, but... How to select the marked region? After all I am still need it, i.e. to indent the code between marks.
The region between the last mark and the current point (that is where your cursor is) is the selected region. Just run M-x indent-region.
@Svante No, it is not selected. Although the indent-region works just fine, but the c-indent-line-or-region(the one, that bound to the tab key) is not. The last function works not on the marked regions, but on the selected ones, i.e. the region that is highlighted like if you had selected it with mouse.
@YagamyLight Ah, you can do that by hitting C-Spc twice where you put the mark. That activates transient-mark-mode temporarily (until you cancel (with C-g)). Then move the point to see the selected region appear.
P
Peter Mortensen

Take a look at region-rectangle in Emacs.

In short, you start selection like usual with Control-Space, then kill region with Control-x r k and paste (or yank) killed block with Control-x r y.


That doesn't kill regions, it kills rectangles, which is a lot different from what vim's visual mode does, in my experience.
P
Peter Mortensen

Emacs 24.4 now has rectangle-mark-mode. Use Ctrl + X, Space to invoke it.


How do you move the rectangle block ?
This is correct, but no source cited...
M
Micah Elliott

Although C-SPC is a common way to start marking something from wherever your point is, there are often quicker/easier ways that don't involve explicitly moving to start/end points...

Built-in selection shortcuts

M-h — an important means to mark a paragraph. A "paragraph" often means a block of code.

C-M-h and C-M-@ — for marking sexps and defuns, respectively. This works for several languages, not just lisps.

hold down shift — another slick way to highlight during movement. E.g., M-S-f selects forward a whole word. This is shift-select-mode, and it is enabled by default in Emacs 24+. On some (non-chiclet) keyboards, you should be able to hold down C-S- with a single pinky.

You can press any of these repeatedly to grow the selection.

There are also a few special ways to mark things:

C-x h — mark the whole buffer

C-x SPC — enter rectangle mark mode

(NOTE: use C-g often to cancel marking while experimenting.)

Add-ons

There are a few add-on packages that improve selecting regions and things. These are all play nicely together and fit different use cases. Use them all!

expand-region: Expand region increases the selected region by semantic units. Just keep pressing the key until it selects what you want. C-= is a recommended binding for it. Hit it a few times to get what you need.

easy-kill: Use M-w and a mnemonic to select different types of things, like words, sexps, lists, etc.

zop-to-char: Like zap-to-char, but provides nice selection and other menu-driven actions.

diff-hl: Highlight uncommitted changed regions. Use diff-hl-mark-hunk to select/mark a hunk.

symbol-overlay: Select symbol at point with a keystroke (M-i). Then you can do other things with it, like copy, search, jump, replace, etc.


n
nbro

Use Control-Space to set a mark and move your cursor.

The transient-mark-mode will highlight selections for you. M-x transient-mark-mode.

You can setup Emacs to enable this mode by default using a customization. M-x customize-option RET transient-mark-mode.


P
Peter Mortensen

... and in case you are using Ubuntu and Ctrl + space is not working for you: you need to clear the Intelligent Input Bus (IBus) "next input method" key binding, as in

run ibus-setup and change the key binding for "next input method" to something else (or delete it entirely by clicking the "..." button and then the "Delete" button).

The quote is taken from an answer to a Stack Overflow question.


P
Peter Mortensen

To expand answer of Edin Salkovic, if you use CUA mode, you can use Ctrl + Enter to begin a visual block selection. There are plenty of shortcuts to control block selection described in the documentation of CUA.


P
Peter Mortensen

With Emacs 25, simply press Ctrl + Space and then move your cursor wherever you want to highlight/select the region of text which interests you. After that, you may need these commands:

Ctrl + W for cutting.

Alt + W for copying.

Ctrl + Y for pasting.