10

In Vim, Ctrl-I takes the cursor back to its previous location (before the last jump). Ctrl-O is its complement, moving forward through the jumplist. gg jumps to the top of the file, so Ctrl-I then jumps back.

The equivalent to Ctrl-I in Emacs would be C-u C-space. However, the obvious way to get to the top of the file, Home, doesn't set the mark, so there's no way to return to the line you came from that way.

Is there a better set of commands for getting to the top and bottom of the file (and perhaps other places) that does respect the mark ring? Or is there a better way to reverse Home?

Community
  • 1
  • 1
Peeja
  • 13,683
  • 11
  • 58
  • 77
  • Possible duplicate of [In Emacs, how to go back to previous line position after using semantic Jump to Symbol?](http://stackoverflow.com/questions/4918707/in-emacs-how-to-go-back-to-previous-line-position-after-using-semantic-jump-to) – Thomas Oct 22 '15 at 15:40
  • Hmm. It looks like what's described there may be the closest that Emacs gets, but it doesn't cover my example. That is, pressing Home and moving to the top of the file doesn't push onto the mark ring, so that kind of movement can't be reversed with C-u C-space. – Peeja Oct 22 '15 at 16:25
  • Hey, nobody mentioned evil-mode. It's a good vim layer and `C-i` and `C-o` work the same and I like it very much :) http://wikemacs.org/index.php/Evil – Ehvince Oct 23 '15 at 00:06
  • @Peeja, FWIW, if I jump to the top of the file (using `M-<`), I can go back to the previous position with `C-u C-SPC` without any problems. I don't think there is a way to toggle between those two positions in Emacs, though, but I guess if I wanted to jump back, I'd just use `M-<` again? – Thomas Oct 23 '15 at 10:25
  • @Thomas You're right! I was doing something wrong; not sure what. As for going forward again, there's another answered question that covers it: http://stackoverflow.com/questions/3393834/how-to-move-forward-and-backward-in-emacs-mark-ring – Peeja Oct 23 '15 at 14:34

2 Answers2

10

C-SPC M-< to go to the top. C-u C-SPC (or C-x C-x C-g) to get back again.

(C-x C-x g does not change the mark-ring. C-x C-x swaps point and mark, and activates the region. C-g then deactivates the region.)

Drew
  • 29,895
  • 7
  • 74
  • 104
  • So I have *manually* mark places I want to be able to return to? I'm starting to miss Vim… – Peeja Oct 22 '15 at 19:27
  • You need to *somehow* tell the editor what you consider the last place was, no? How do you tell Vim where you want to return to? As you use an editor, the cursor position changes all the time. What defines the "last" place, where you might want to return to? – Drew Oct 22 '15 at 19:55
  • It just kind of works. All the "jumpy" commands add your position to the jumplist. Search is a jump. `gg` is a jump. Any time you end up in a different buffer, that's a jump. – Peeja Oct 22 '15 at 21:41
  • In that case, you can determine similar or equivalent Emacs "jumpy" commands, and either advise them to push the mark or define replacements for them that push the mark (and bind those replacements to the same keys). This is all very vague so far - there's not much that anyone can say concretely that can help at that level. Emacs is not Vim. It is probably a mistake to try to make it emulate Vim completely, but if you are very specific then people can probably help you to some extent. – Drew Oct 23 '15 at 01:22
  • 7
    On my Emacs, `M-<` *does* set the mark. According to its documentation, it should do so whenever "Transient Mark mode is disabled", which should translate to "there is no active region", unless you've changed some settings. – Aaron Harris Oct 23 '15 at 04:14
  • @AaronHarris: "*Transient Mark mode is disabled*" means `transient-mark-mode` is `nil`, which is **not** the case by default (since Emacs 23). If it is `nil` then there is no *notion* of an active region (there is always a region, but it is neither active nor inactive). But if it is non-`nil` (the default) then the region can be active *or* inactive. That is, it does **not** follow that non-`nil` `transient-mark-mode` means "*there is no active region*", i.e., that the region is inactive. – Drew Oct 23 '15 at 05:01
  • Regardless, numerous commands which are liable to move the cursor some significant (or just arbitrary) distance from where it was previously (`M-<`, `M->`, `C-s`, etc...) do automatically push the original position to the mark ring so that you can trivially jump back by popping the mark ring. – phils Oct 23 '15 at 07:06
  • Drew: Perhaps that's a documentation bug with `beginning-` and `end-of-buffer`. The code itself uses `region-active-p` which is "`t` if Transient Mark mode is enabled *and* the mark is active." – phils Oct 23 '15 at 07:08
  • @AaronHarris It does work! I'm not sure what I was doing wrong before. Thanks! – Peeja Oct 23 '15 at 14:23
  • @Drew Ah, I'm happy to advise things myself to push the mark, if that's a sensible thing to do. I'll take my cue from `beginning-of-buffer`'s implementation and do that. Thanks! – Peeja Oct 23 '15 at 14:25
  • @phils: I see. Please consider filing a doc bug, in that case. – Drew Oct 23 '15 at 17:48
0

My muscle memory for this is M-g g 0 <return>

ngoldbaum
  • 5,430
  • 3
  • 28
  • 35