How to select or highlight a block in Emacs?

I want to select or highlight a block in Emacs, not using a mouse, but doing it from the keyboard, like VIM Visual Mode. What is the easiest way to do this from the keyboard?

+73
emacs highlight selection
Mar 18 '09 at 10:17
source share
9 answers

Take a look at the rectangle area in emacs.

In short, you start the selection, as usual, with Control - Space , then destroy the area with Control - x r k and insert (or yank) the killed block with Control - x r y .

+58
Mar 18 '09 at 10:36
source share

If I understand the question correctly, this does not apply to initially rectangular areas.

C-Spc places the mark at the current position.

At any place where your cursor is located, the text between the last mark and the current position is “selected” (you can select this by activating transient-mark-mode , but this also means that the marks must be deleted if you do not want to highlight).

You can work in this region with teams such as:

Cw ., Kill the region. This removes and places the region in the annihilation ring.
Cy ., Carving. This inserts the last fragment from the annihilation ring.
My ., Loop ring. Immediately after Cy this replaces the yanked portion with other fragments in the annihilation ring.
Mw ., Save the area in the kill ring. Like Cw , but does not delete.

This is just the main use. Signs are also used for other purposes. I recommend the tutorial ( Ch t ).

+63
Mar 18 '09 at 12:20
source share

Emacs 24.4 now has a rectangular label mode. Cx to call it.

+22
Oct 29 '14 at 15:35
source share

See the article: "Work with rectangular selections" , especially the comments section. See also the CUA Mode Documentation section entitled “CUA Rectangle Support”. There is also a good video on vimeo .

+16
Mar 18 '09 at 10:55
source share

Although C-SPC is the usual way to start celebrating something, no matter where you are, there are often faster / simpler ways that don't imply an explicit transition to the start / end point ...

Built-in Shortcuts

  • Mh is an important paragraph highlighting tool. "Paragraph" often means a block of code.

  • C-Mh and CM-@ - for sexps and defuns respectively. This works for multiple languages, not just for jokes.

  • Hold down the Shift key - another nice way to highlight while moving. For example, MSf forward a whole word. This is shift-select-mode , and it is enabled by default in Emacs 24+. On some (non-chiclet) keyboards, you can hold the CS- one little finger.

You can click any of them several times to increase the selection.

(NOTE: often use Cg to unmark during experiments.)

Additions

There are several additional packages that improve the selection of regions and things. All of them are perfectly combined with each other and are suitable for different use cases. Use them all!

  • Expand-Region : Expand region increases the selection by semantic units. Just keep pressing the key until it selects what you want. C-= recommended binding for him. Click a few times to get what you need.

  • Easy-Kill : Use Mw and mnemonics to select various types of things, such as words, sexps, lists, etc.

  • Zop-to-Char : Like zap-to-char, but provides a good selection and other menu driven actions.

  • diff-hl : highlight uncommitted modified regions. Use diff-hl-mark-hunk to select / mark a piece.

+7
Apr 14 '16 at 19:07
source share

Use Control - Space to set the label and move the cursor.

transient-mark-mode will highlight the choice for you. M - x transient-mark-mode .

You can configure Emacs to enable this mode by default with the settings. M - x customize-option RET transient-mark-mode .

+6
Mar 18 '09 at 10:38
source share

... and if you use Ubuntu and C-spc does not work for you: you need to clear the IBus keyboard "next input method", as in

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

Quote from the answer to the SO question.

+1
Apr 10 '15 at 9:40
source share

To expand Edin Salkovich's answer, if you use CUA mode, you can use to start selecting a visual unit. There are many shortcuts to control block selection described in the CUA documentation .

0
Jun 11 2018-12-12T00:
source share

In Emacs 25, just press C - Space , and then move the cursor to where you want to select / select the area of ​​text that interests you. After that, you may need the following commands:

  • C - w for cutting.
  • M - w to copy.
  • C - y to insert.
0
Aug 10 '18 at 13:54
source share



All Articles