Friday 25 May 2012

Getting Started With emacs

Emacs is the text editor that I use day to day for the majority of my tasks, including programming, note taking and scripting.

It's simple, fast and powerful. Here I'm going to attempt to give you a quickstart guide to hopefully give you a leg up over the learning wall that is emacs.
Yes, I accept it's not the most intuitive thing to begin with but bare with it - you'll get used to it and never look back. Gone are the days of moving your hands back and forth to and from the mouse whilst editing text.

Emacs has so many features and addons that an oft quoted phrase is "Emacs is a great operating system, shame about the text editor" - obviously tongue in cheek.

There are other text editors available. But let's not discuss them. Emacs does also have a good little built in tutorial but I thought I would write this one so you can browse at your leisure. It also lets me document the commands and functionality I use most.

Ok so assuming you have successfully installed and setup emacs (not going to cover that - we'll assume you're proficient with synaptic or w/e) the first thing you are greeted with is a fairly drab looking UI:

So this basically shows the main text editing area with a line along the bottom indicating all sorts of modes that you might be in and then followed by the socalled 'minibuffer' - this is an important area (as you'll see imminently). Otherwise, it looks pretty much like your regular text editor right? The main area in the middle is called the main buffer.

So you want to start typing? Easy, click file->visit new file which will then prompt you to choose a name for it - choose one and then click OK. Easy.

Ok maybe that was too easy and a bit cheaty. That has opened opened up a new file of your choosing...but how do you do that without the mouse? When you clicked file you might have notice that next to the visit new file text it said C-x C-f. That's emacs speak for Ctrl-x Ctrl-f. Try it now. When you type Ctrl and x together followed by Ctrl-f together you'll see that the minibuffer is activated (the empty area at the bottom) prompting you to 'find file'. You can use this like a regular command prompt almost and tab-complete your way to the location your pre-existing or non-existent file that you wish to edit/create.

Thats the basics - you can open your file. That's taught you the complete basics. From there all you have to know is that M-x means alt-x (or esc-x on some systems?).

Yes it's a bit fiddly and makes your hands hurt after a while but trust me, it gets easier.
The table below lists some of my most favoured emacs commands that I use on a daily basis. The all follow the same form - either ctrl-x (c-x) or alt-x (m-x) followed by some other modifier.

The all important one to remember is c-g. That cancels whatever you might be in the middle of. e.g. You're in the middle of opening a file and you realise you need to modify the buffer (file that is currently open) so you c-g out of it to get back to the buffer. phew.

Useful Commands

C-x C-fOpen a buffer
C-x C-sSave the file
C-x C-wSave the file with a new name
C-homeMove to the start of the buffer
C-endGo to the end of the buffer
C-spaceBegin highlighting
M-wCopy
C-wCut
C-yPaste
M-x replace-stringReplace string (find & replace) use the minibuffer to guide you through a string replacement (yes you literally type in replace-string)
M-x query-replaceReplace string with prompts for each find
C-sSearch for a given string using the minibuffer
M-x reverse-regionReverse the lines in a region
C-x C-cClose emacs
C-x 3Split the screen vertically
C-x 2Split the screen horizontally
C-x 1Make this buffer fill the window
C-x 0Close the active window
C-x C-bSelect a buffer by name
M-x C++-modeChange highlighting C++-mode (you can insert your current language, e.g. python-mode). It generally does this automatically depending on the file extension.

Another handy thing to do is to left click with the ctrl modifier pushed down, this will give you a handy little menu to choose between the currently open buffers.

That's the basics...I will most likely post again with details of the customisations I use for smooth scrolling and pretty colour schemes.

No comments:

Post a Comment

Leave a comment!

Can we just autofill city and state? Please!

Coming from a country that is not the US where zip/postal codes are hyper specific, it always drives me nuts when you are filling in a form ...