You see all these coders who live in the terminal, never touching the mouse, flying through everything with just the keyboard. I wanted to try that too, so I thought I would spend some time playing around with Emacs. The interesting thing about Emacs is that you can do almost everything this way. Of course, this also means that you have to remember quite a lot of keyboard shortcuts!
I followed this video tutorial to get started. Here are some notes on the things I found useful, mainly for my own reference.
Installing Emacs
On Ubuntu, you can install Emacs using the following commands.
sudo apt-get update
sudo apt-get install emacs
To launch Emacs:
emacs
If you want to use Emacs directly within the terminal:
emacs -nw
Some useful keyboard shortcuts
Before getting started, C refers to the Ctrl key and M (Meta) usually refers to the Alt key. For example, C-x C-f means pressing Ctrl+x followed by Ctrl+f.
Opening documents
| Shortcut | Action |
|---|---|
C-x C-f |
Open or create a file |
C-x b |
Switch to another buffer |
C-x C-+ |
Zoom in |
C-x C-- |
Zoom out |
C-g |
Cancel the current command |
C-x C-c |
Quit Emacs |
When opening a file, you can either type the full path to the file or simply open a directory and find the file from there. If you get stuck in a directory buffer, use C-x b to switch back to another buffer. Took me a while to figure this out!
Basic navigation
Apparently, you don’t really need the arrow keys.
| Shortcut | Action |
|---|---|
C-n |
Next line |
C-p |
Previous line |
C-f |
Forward one character |
C-b |
Backward one character |
M-f |
Forward one word |
M-b |
Backward one word |
C-a |
Beginning of line |
C-e |
End of line |
M-a |
Beginning of sentence |
M-e |
End of sentence |
M-{ |
Backward one paragraph |
M-} |
Forward one paragraph |
M-< |
Beginning of document |
M-> |
End of document |
M-g g |
Go to a specific line number |
You can also repeat a movement several times by pressing ESC, followed by a number and the corresponding shortcut. For example, ESC 5 C-n moves five lines down.
Cut, copy and paste
Emacs uses somewhat different terminology for these operations. Cutting is referred to as killing, and pasting is referred to as yanking.
| Shortcut | Action |
|---|---|
C-SPC |
Start selecting text |
C-w |
Cut selected text |
M-w |
Copy selected text |
C-y |
Paste |
C-k |
Cut to the end of the line |
C-x Backspace |
Cut backward to the beginning of the sentence |
M-k |
Cut forward to the end of the sentence |
M-z |
Cut up to and including a specified character |
M-d |
Cut forward one word |
C-x u |
Undo |
One nice thing about Emacs is the kill ring. Unlike a simple clipboard, it keeps track of multiple pieces of previously killed or copied text.
Use C-y to paste and then M-y to cycle through the previous entries in the kill ring. You can also inspect the kill ring using C-h v followed by kill-ring. This opens in a separate window; use C-x 1 to close it and keep only your document.
Buffers
A buffer is essentially where Emacs holds the contents of a document or other information.
| Shortcut | Action |
|---|---|
C-x b |
Switch to another buffer |
C-x C-b |
List all buffers |
C-x k |
Kill a buffer |
Changing the case of words
Quite handy when editing text.
| Shortcut | Action |
|---|---|
M-c |
Capitalize a word |
M-u |
Convert to uppercase |
M-l |
Convert to lowercase |
Spell check
Use M-$ to check the spelling of the current word.
If you are using Ubuntu, you can install Aspell with the English dictionary using:
sudo apt-get install aspell aspell-en
For Arch Linux users:
sudo pacman -S aspell-en
Search and replace
| Shortcut | Action |
|---|---|
C-s |
Search forward |
C-r |
Search backward |
M-% |
Query replace |
One thing to keep in mind when using query replace (M-%) is that it searches forward from the current cursor position. So, if you want to replace something throughout the document, go to the beginning of the document first using M-<.
Press y to replace, n to skip, or RET to exit the query replace.
Splitting windows
You can have multiple documents open side by side within Emacs.
| Shortcut | Action |
|---|---|
C-x 2 |
Split into top and bottom windows |
C-x 3 |
Split into side-by-side windows |
C-x 0 |
Close the current window |
C-x 1 |
Keep only the current window |
C-x 4 f |
Open a file in another window |
C-x o |
Move to another window |
Save and exit
| Shortcut | Action |
|---|---|
C-x C-s |
Save the document |
C-x C-c |
Quit Emacs |
A small note on Org mode
One of the interesting things about Emacs is Org mode, which can be used for taking notes, organizing tasks, and writing documents.
To write inline code in Org mode, use the tilde (~) around the code snippet, similar to how we use backticks in Markdown.
You can use ~print("Hello World")~ to print a message.
There is obviously a lot more one can do with Emacs. For now, remembering these keyboard shortcuts itself seems to be quite a task! I wanted to try something new, but I don’t know if it is going to stick. Let’s see how it goes. Maybe I will never use it again.