M-s and M-g prefixes in Emacs

<2025-09-07 Sun>

Let's talk about two semantic and useful prefixes in GNU Emacs - M-s and M-g. Semantics is there - M-s is for "search", M-g is for "go".

M-g is for Go

M-g l
go to line
M-g c
go to char
M-g g
go go go
M-g M-g
also "go go go", for convenience, hold Alt and press g g twice.
M-g w
go to word, asks for the first character of a word and jumps there.
M-g C-h
I ran out of remembering keybindings, so this one is list all of them – add C-h after any prefix and Emacs will show you the help. No need to use which-key unless necessary, you're in control - you'll memorise things only looking first into memory, if missing - take action to learn (and thus remember). which-key is a false friend for new Emacser, beware!

Checking the keymap, we see many aliases like M-g M-g, so M-g M-c, M-g M-l, M-g M-n and so on, they are for mnemonical convenience.

Most of movements run avy, which can do anything, it's an awesome way of arbitrary movements and beyond.

M-s is for Search

M-s is an entrypoint to search-related things. 95% of the time I use M-s d (deadgrep) and M-s o (occur).

M-s d runs deadgrep, a frontend for ripgrep for searching in the current project. I use it all the time, and nowadays with rise of LLM, making your project greppable is must have to enable AI to do sensible changes in the project.

M-s o is mighty M-x occur, but adjusted to "do what I mean" semantics – if selection is active, it runs "occur" on it, otherwise prompts for input.

(use-package emacs
  :config
  (defun occur-dwim (&optional _)
    "Run occur with Active Region if any, otherwise regular occur."
    (interactive)
    (if (use-region-p)
        (occur (buffer-substring-no-properties (region-beginning) (region-end)))
      (call-interactively 'occur)))
  (keymap-global-set "M-s o" #'occur-dwim))

M-s h is highlighing prefix, which deserves a separate post. So see you later, cheers!

Home ⋅ Date: <2025-09-07 Sun> ⋅ Tags: Agenda