Emacs Notes

Emacs is a very feature-laden editor. As such, it can be hard to remember all of the various features that are very useful, but perhaps, not frequently used. I thought I would create a list of some features I have learned and did not want to forget.

Show a list of all currently opened buffers

C-x C-b

When there are dozens of buffers open, it can be difficult to switch to the correct one. This command will list them all, and allow you to go directly to the buffer you wish to edit.

Display current function name

M-x which-function-mode

This will add the name of the function that the cursor is currently in to the status bar at the bottom of the page. I have found this pretty useful when exploring unknown code, or long documents with hundreds of functions. I don’t enable this all the time though because I have found it can sow the responsiveness in larger documents.

Show invisible characters

M-x whitespace-mode

Sometimes its nice to see if a document is using spaces or tabs. Enabling white-space mode makes it clear the type of indentation being used, as well as making other whitespace sins apparent. This is particularly useful when you work on a file that’s been passed around multiple editors and is not consistently indented.

Along with this, you can easily remove whitespace present on the end of the lines by selection a region and using the following command:

M-x delete-trailing-whitespace

Easy alignment

M-x align-regexp

This is actually a command I do use frequently. I like to have things lined up as I feel that it improves legibility. This command, when run on selected text, will align the text to the matching user input. For example, I can use this to align multiple lines on an equal sign.

Selection, Etc.

C-x space

Mark. This begins a selection by marking where you cursor is and highlight to where you move your cursor thereafter

M-w

Copy. This will copy the marked region

C-w

Cut. This will cut the marked region

C-y

Yank. This will paste the kill buffer contents at the location of the cursor

Rectangular Commands

C-x r k

Rectangular cut/kill. Make a selection across multiple lines, then run this command. It will kill the rectangular region of text that was selected

C-x r y

Rectangular yank/paste. This command will paste the rectangular area of text killed by the command above.

C-x r t

Make a selection across multiple lines, then run this command. As you type, it will insert the text on each line you selected in the column that you began/ended with. If you start and end in different columns, it will replace the text in those columns on each line.

Navigational Hot Keys

Moving within the buffer can be tedious without some hotkeys to speed things up.

M->

Go to end of document

M-<

Go to beginning of document

M-g M-g

This brings up the ‘goto line’ prompt for quickly going to a specific line

M-g tab

Similarly, this brings up the ‘move to column’ prompt

C-v

Page down

M-v

Page up

C-l

Center cursor/cursor position in window

C-s

Search for term. Use the combination repeatedly to find the next instance of the search term

C-r

Search in reverse. As with the previous command, repeatedly using the combination will take you to previous instances of the search term.

Formatting Hotkeys

M-c

Make word capitalized from cursor position to end of word

M-l

Make word lowercase from cursor position to end of word

M-u

Make word uppercase from cursor position to end of word

Window Navigation

C-x 1

Return to single view

C-x 2

Split pane horizontally

C-x 3

Split pane vertically

C-x o

Move cursor to other pane/view

C-x left

Switch buffer to preceding buffer

C-x right

Switch buffer to next buffer

File Handling

C-x C-f

Find a file to open/edit

/ssh:user@ipaddress:/path/to/file/on/remote/machine

Also of note, when opening a file, you can use the above syntax to open a file on a remote machine.

C-x k

Kill buffer (close file)

C-x C-q

If a file is write protected, but you want to edit the buffer anyway…

Keyboard Macros

Keyboard macros allow you to create a set of commands that you can repeat in order to speed up repetitive tasks.

F3

This will begin your macro recording. Every keystroke and command until you do will be recorded until you stop recording

F4

This will stop the macro recording. Once you have stopped recording, every time you press F4 it will repeat your whole macro.

mg; The Emacs Like Editor

Sometimes you don’t want to install an 80 MB text editor on your machine. Sometimes you want something lightweight, but just don’t want to use ‘vim’. For those times, you have ‘mg’ (micro gnu). It’s a very small binary (200k on my current install) that gives a similar experience to Emacs. It has the familiar interface, keybindings and most of the essential features. Obviously the exotic commands are missing, but the fundamentals are there. I’d much rather be stuck using this than vim, vi, or nano.

Closing thoughts

While not a comprehensive list, these are some of the commands I wish I had known when starting out with Emacs. There are so many, and while I like Emacs a lot, my main gripe with it is the lack of discoverability with commands. As a last tip, the following command will show you all of the available key shortcuts:

C-h b

Happy editing!

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.