Avy Emacs Tutorial: Moving around Emacs Quickly

Avy is a must-have if you’re using Emacs. The Avy package allows you to quickly navigate to any visible point within an Emacs buffer, frame, or window. The best part is it’s extremely simple to use, and doesn’t take more than 5 minutes to get up and running with it. Here’s a video:

Avy is available via Melpa, so with Melpa installed, run:

M-x package-install Enter: Avy

Avy’s Github Page

avy-goto-char:

avy-goto-char is a simple command where you type ONE character, and all matching characters will be given a character (or two) for you to type. The character(s) you enter is the location your cursor will move to. You can set it up with any keybinding, but I like “C-:” (Control+Shift+:). So you can put the following in your .emacs file, and restart emacs to get started:

(global-set-key (kbd "C-:") 'avy-goto-char)

Then you type C-: then the character to move to, then the character(s) that is given to you by Avy, and Boom your there!

avy-goto-char-timer

This works the same way as avy-goto-char, only you can type as much or as little as you want, and after an amount of time specified by you, the command will time out and automatically go to the location if there’s only one option, otherwise the timeout will move to the decision tree allowing you to choose the location. Some people prefer the avy-goto-char-timer because it reduces the amount of decisions available, and allows you to type as much or as little as you want, rather than one character only.

You can set avy-goto-char-timer like this:

(global-set-key (kbd "C-:") 'avy-goto-char-timer)
(setq avy-timeout-seconds 4.0) ;; set timeout to 4.0 seconds. Default is 0.8.

Use Avy with ALL Frames, Windows & Buffers

Avy does not work with multiple Emacs frames out of the box, but it’s super easy to fix that. Just add this to your .emacs config, and whammo, Frames, Windows, and Buffers will all work with Avy!

(setq avy-all-windows 'all-frames)

Moving Code Around Quickly

avy-move-region allows you to select TWO lines and move the region between those two lines to the line above your cursor. This can be much quicker than manually selecting the region, cutting it, and pasting it to the new location. I’m too lazy to come up with a keybinding, but here’s the code for your .emacs config minus the keybinding.

(global-set-key (kbd "Command of your choice") 'avy-move-region)

Now when you type the command for avy-move-region, you’ll enter two selections, then all of the sudden the code is moved where your cursor is! I’m still experimenting with avy-move-region. Sometimes you can’t see the whole chunk of code you want to move, or for whatever reason it’s not convenient, but it can be a powerful tool at times, so I think it’s worth at least knowing about.

avy-goto-line

avy-goto-line is a great way to get to lines quickly. There’s probably plenty of options for going to lines, but avy-goto-line shows you a decision tree, and allows you to get to the line you want with a visual aid by giving you a character to type for each visible line.

(global-set-key (kbd "Command of your choice") 'avy-goto-line)

Another great thing about Avy is you can use it to quickly select large chunks of text and be both precise and efficient at the same time. I’d love to ramble, but I think that’s it! What other packages do you use to move around in Emacs? Have any tips you’d like to share?

Enjoy Avy, and keep hacking!

You must have an account and be logged in to solve challenges and lessons!