8

I would like to grow a selection without moving point (the cursor) from its location. I know I can always use C-u C-spc to move to the previous location, but I would like to avoid that.

More specifically, I would like to do both of these operations:

  • Select all text in the buffer
  • Gradually add subsequent lines / paragraphs to the selection

while keeping the cursor where it is.

Does Emacs have any interactive functions for this? Are they bound to any keyboard shortcuts?

  • You can always go back to previous point location with C-u C-spc. – Malabarba Oct 8 '14 at 5:54
  • 1
    can you tell me please, what could be the interest of selecting text w/o moving the cursor? – Nsukami _ Oct 8 '14 at 12:34
12

There is a separate answer for each of the points you listed:

  1. The emacs default binding C-x h will select the whole buffer regardless of where the cursor is.

  2. The expand-region package will help achieve the second goal of gradually expanding the selection. expand-region will also eventually select the whole buffer as you keep on expanding the region. Have a look at its readme on github for more info. This package is available through MELPA.

Here is a brief introduction to this package from its github:

Expand region increases the selected region by semantic units. Just keep pressing the key until it selects what you want.

An example:

 (setq alphabet-start "abc def") 

With the cursor at the c, it starts by marking the entire word abc, then expand to the contents of the quotes abc def, then to the entire quote "abc def", then to the contents of the sexp setq alphabet-start "abc def" and finally to the entire sexp.

I'd like to add though that modifying the region selection in either case will still move the cursor.

  • +1, but note that C-x h does move cursor: to the end of the buffer and then the beginning of the buffer. You need C-u C-spc twice to get back to where you were. – ShreevatsaR Oct 9 '14 at 17:57
8

If the region grows then the cursor changes position. This is because the cursor is always at one end of the region.

If you select all of the buffer text (C-x h and then use C-x C-x to swap point and mark, so the cursor is at the end of the text), and then you type some more text, then you have accomplished what you want: the region has been extended to include the text you typed. (This assumes you do not have delete-selection-mode turned on.)

The region has also been deactivated, automatically, so the region text is no longer highlighted. To re-activate the region, do C-x C-x (which you can repeat, if you want to keep the cursor at the buffer end.

4

As mentioned, the expand-region package is great but I also feel its worth mentioning easy-kill. It's billed as a drop in replacement for kill-ring-save that turns the normal M-w "copy" into a dynamic copy command that also acts as a prefix for saving various things. For example, M-w would copy the active region, url, email or current line (in order of easy-kill-try-things) but followed by a "d" M-w d it would forcefully copy of the current defun.

There is also a method of expanding/shrinking the current selection, somewhat like expand-region, as well as a command called easy-mark for marking regions without changing your current point.

1

When you have copied something, you can add text to the selection later on with the use of C-M-w:

  • copy a line/region
  • do something else
  • you want to append sthg to the previously copied text: type C-M-w (append-next-kill) and then copy what you want (M-w).
1

As stated in other answers:

C-xh will select the entire buffer ; (mark-whole-buffer)

If you want to grow your selection the easiest way would be using the mark-* commands.

As a keybound example:

C-M-SPC (mark-sexp) will mark the sexp at point. If you repeat the command, it will add the following sexp to the selection. The cursor will remain at the current position, or move to the start of the object (paragraph/sexp) to ensure it is entirely marked.

  • Prelude maps (mark-whole-buffer) to <kbd>Cmd-a</kbd> on OSX to keep some similarity with the typical OSX functionality. – Aaron Lee Oct 8 '14 at 12:36
1

If you're ok with using the mouse, you can use the secondary selection. Press Meta, then click and drag the left mouse button (M-drag-mouse-1). This doesn't move the point as long as you're only copying text that's visible on the screen. You can copy the secondary selection with M-mouse-3 and yank it with M-mouse-2.

Apart from the secondary selection, you will need to move the cursor to select arbitrary text, because the cursor is one end of the selection (the other one is the mark).

If you want to work in a buffer without affecting the point, you can open the buffer in a second window (C-x 2 or C-x 3). Each window has its own point. When you've finished selecting and otherwise editing, close the window (C-x 0) and go back to the other one.

For more flexibility, you can clone the current buffer. Run M-x clone-indirect-buffer, or C-x 4 c to also create a new window showing that buffer. The clone has the same text as the original, but it has its own mark and point.

If you've already killed some text, you can append more text by typing C-M-w (append-next-kill) and then using a kill command. For example, C-M-w C-k appends the tail of the current line to the text that's already at the front of the kill ring, and deletes the copied text. C-M-w M-w appends the selection to the text at the front of the kill ring (as opposed to M-w which pushes the selected text as a new entry on the kill ring).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.