How can I select or highlight a block in Emacs?

Viewed 106197

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?

8 Answers

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).

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.

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.

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.
Related