Jump around

Last time I shared my preferred choice for buffer switching, so let’s keep jumping around. The House of Pain reference is a bit tacky, I know, but 90s nostalgia is still flying pretty high from what I’ve heard.

Quickly moving in a buffer is something every Emacs knight do multiple times a day. I distinguish in-buffer jumps in two categories:

  • jumps that encompass all the buffer content
  • jumps limited to what is currently visible

The first movements are covered by Oleh Krehel’s Swiper, but I am more interested in the second kind of jumps this time. There are plenty of solutions both bundled in Emacs (e.g., pop/mark commands) and in the wild. My favourite is avy, again from Oleh Krehel.

(use-package avy-jump                   ; Jump to characters in buffers
  :ensure avy
  :bind (("C-j" . avy-goto-char-in-line)
         ("M-j" . avy-goto-char)))

As it always happens with Oleh’s packages, avy is well-documented, easy to extend, and full of useful commands. I only added bindings for the ones I use the most, and I really use these two all the time.

avy-goto-char allows me to move anywhere following the character I am after. It’s quicker than typing the whole candidate in Swiper, and obviously better than manually moving point1 with C-p/C-n or, worse, the arrow keys.

avy-goto-char-in-line behaves in much the same way, but it is limited to the current line. If you remember iy-go-to-char mentioned years ago on Emacs Rocks, this is Oleh’s own variant.

Both commands push point to the mark-ring, so the default Emacs key binding C-u C-SPC takes you back to your original position.

However, there is an even quicker alternative when I want to jump between occurrences of the symbol where point currently is, which turns out to be pretty handy when coding.

(use-package symbol-overlay             ; Highlight symbols
  :ensure t
  :bind (:map symbol-overlay-mode-map
              ("M-h" . symbol-overlay-put)
              ("M-n" . symbol-overlay-jump-next)
              ("M-p" . symbol-overlay-jump-prev))
  :hook ((conf-mode . symbol-overlay-mode)
         (html-mode . symbol-overlay-mode)
         (prog-mode . symbol-overlay-mode)
         (yaml-mode . symbol-overlay-mode)))

symbol-overlay not only highlights all the occurrences of the current symbol, but it lets you move quickly among them with symbol-overlay-jump-prev and symbol-overlay-jump-next without asking for a character to look for.

Moreover, symbol-overlay works on all the buffer content, so in this particular case is quicker than Swiper too.

  1. Cursor in the selected window. (See: 1.1 Point