42

I have used the mark to select regions of a buffer. For example, I know the sequence C-<space> C-s foo can be used to select all text between the current point and the point after a search for foo.

I recently discovered the existence of the mark ring, so I can C-<space> C-<space> to leave a mark, move the point in the buffer, and then C-u C-<space> to jump back.

Are there any suggestions, guides, or tips to effectively navigating using the mark ring?

Drew
  • 75,699
  • 9
  • 109
  • 225
Patrick Steele
  • 1,105
  • 9
  • 10
  • 2
    Thanks for asking the question. One thought that occurs to me is to make a faster binding for "adding a mark", e.g. `(global-set-key (kbd "s-m") '(lambda () (interactive) (push-mark)))`. – Joe Corneli Jun 15 '15 at 19:43
  • C-Space C-Space does not work in SpaceEmas on My MacBook?, I try it, it does nothing – 1234 Dec 18 '19 at 19:34

6 Answers6

27

Helm has an interface for browsing both local and global mark-rings: helm-all-mark-rings.

See the mini-guide for a brief description and a screenshot.

And if you prefer Ivy/Swiper/Counsel, see counsel-mark-ring.

Davor Cubranic
  • 700
  • 4
  • 9
16

When you set set-mark-command-repeat-pop to t via

(setq set-mark-command-repeat-pop t)

you can keep pressing C-SPC after the first invocation of C-u C-SPC to jump to previous locations stored in the mark ring.

itsjeyd
  • 14,586
  • 3
  • 58
  • 87
14

I find it easier to use registers to mark locations: C-x r <space> and then a letter to mark, C-x r j and a letter to jump back. I can maintain a number of marks, very useful when alternating between multiple buffers

Alain
  • 363
  • 2
  • 7
  • 3
    I always find myself pausing to decide the best letter to assign my register to... – nispio Oct 29 '14 at 21:24
  • 2
    @nispio some time ago I've implemented extension git@github.com:atykhonov/iregister.el.git One of the idea was: allow to skip decision which register to use. May be the extension will not be useful for you but you can borrow the idea in which your custom script could just use any free register and then it will allow to browse the used registers and restore. – Andrii Tykhonov Jan 25 '15 at 05:33
11

C-x C-x for exchange-mark-and-point has similar use to C-u C-space. It lets you bounce between a point and the last mark. It's a little weird for bouncing with transient-mark-mode as it activates the mark and selects the region.

dgtized
  • 4,169
  • 20
  • 41
  • `C-x C-x`with transient-mark-mode is excellent for selecting the last thing you either selected (useful with iedit mode) or yanked. – Rune Kaagaard Oct 12 '17 at 07:16
11

Apart from the other tips you have got here I thought I should mention that C-s sets the mark for you, so there is no need to do it explicitly. Also, many commands that "move a potentially long way" (beginning-of-buffer and end-of-buffer for example) also sets the mark where you started. Together with the other ways to work with the mark (pop, exchange point and mark, etc) it is a quite powerful way to work with a buffer, once you understand how it works. Also note that if you yank text into a buffer, point and mark surrounds the yanked text.

Mathias Dahl
  • 554
  • 2
  • 7
7

(Your question is pretty open-ended, so it might get closed as being primarily opinion-based.)

What I use: Icicles multi-command icicle-goto-marker (bound to C-- C-SPC) to trip around the marks in any buffer (mark-ring), and icicle-goto-global-marker (C-- C-x C-SPC) to trip among the global marks (global-mark-ring).

The lines of text where the markers are located are used as completion candidates. You can type some text (e.g., substring, regexp) to narrow the candidates to the lines that match. You can cycle among the matching lines, visiting any that you like. If you also use library crosshairs.el then position of a marker you visit is highlighted temporarily with crosshairs, so you can quickly see where it is.

Here is part of the doc string for icicle-goto-marker:

Go to a marker in this buffer, choosing it by the line that includes it.
If `crosshairs.el' is loaded, then the target position is highlighted.

By default, candidates are sorted in marker order, that is, with
respect to their buffer positions.  Use `C-M-,' or `C-,' to change the
sort order.

During completion you can use these keys:

`C-RET'   - Goto marker named by current completion candidate
`C-down'  - Goto marker named by next completion candidate
`C-up'    - Goto marker named by previous completion candidate
`C-next'  - Goto marker named by next apropos-completion candidate
`C-prior' - Goto marker named by previous apropos-completion candidate
`C-end'   - Goto marker named by next prefix-completion candidate
`C-home'  - Goto marker named by previous prefix-completion candidate
`<S-delete>' - Delete marker named by current completion candidate

Use `mouse-2', `RET', or `S-RET' to choose a candidate as the final
destination, or `C-g' to quit.
Drew
  • 75,699
  • 9
  • 109
  • 225