GNU Emacs: The one true text editor
In this tutorial to text editor Emacs, I expose us to shortcut chords, and explains the art of mastering them…
For many years, people have argued over whether the M25 motorway circles London, or whether it is London that circles the M25. This is a difficult question indeed, but here's an even more difficult one: which OS do you use? As this is a Linux tutorial, chances are you answered "Linux" without batting your eyelids, or perhaps in a fit of Stallman-esque Free Software frenzy you answered "GNU/Linux".
This is probably true. However, this month you are going to learn about the biggest application in all of Linux. This is a program that most say achieved full operating system status years ago, and is already well ahead in its plans for world domination. Yes, ladies and gentlemen, today I am covering Emacs.
It's better with Meta
Emacs is the elite hacker's text editor of choice, partly because it is so flexible that you truly can spend your entire working life without straying outside of it, but also because it has a steep learning curve that gives you boasting rights over fellow geeks.
To get started, type 'emacs' from your terminal. If you are running X, this will automatically open a graphical version of Emacs, which lets you use your mouse for extra control. If you are running without X, this will open the terminal version of Emacs; you can also get here from within X by running 'emacs –nw'. Using the X version of Emacs is recommended until you get on your feet, at which point you can pick and choose what suits you best.
Emacs makes extensive use of keyboard shortcuts, but you can work with the mouse too. The short control keys are 'Control' and 'Alt' (usually referred to as 'Meta'). For example, pressing 'Ctrl-x' then 'Ctrl-c' is the command to quit Emacs – you need to press and hold 'Control', then press 'X', then press 'C'. Using two keys together like this is called a shortcut 'chord' and is used often in Emacs – so often, in fact, that the 'Ctrl' and 'Meta' keys are usually referred to as just 'C' and 'M', so the quit shortcut is 'C-x C-c'.
Other important shortcuts to help you get started are 'C-x C-f' to load a file, and 'C-x C-s' to save a file. Both times you will be asked to type in the name of the file at the bottom of the screen. When you have multiple files open, you can switch between them using the Buffers menu, so named because files, once opened, are referred to as 'buffers'. Alternatively, you can type 'C-x b' (that is, press and hold 'Ctrl', then press 'X', then release both keys and press 'B' by itself), then type the name of the file you want to switch to (without the path). Emacs will prompt you with a default option, which will be the previous buffer you looked at – hit 'Enter' to use its default. Once you have files open, you can just type normally to edit the text, then save with 'C-x C-s'.
Search and replace
The basic search and replace command in Emacs is activated with 'C-s', and then by typing what you want to search for. This searches forwards only. To search backwards, use 'C-r'. To repeat the search, just type the command twice, so 'C-s C-s' or 'C-r C-r'.
That's the easy part over with, because search and replace requires the Meta key. To run a simple replace command, press 'M-x' (hold down 'Alt' then press 'X'), type 'replace-string' and hit 'Enter'. Emacs will prompt you for the string to search for (hit 'Enter' again after typing it in), then it will prompt you for the string to replace it with (again, hit 'Enter' when you've typed it in). This runs a 'replace all' search, with no further input from you. If you want more control, try 'M-x query-replace', which prompts you for the search string and the replace string as before, but then asks you to confirm each change it finds – press 'y' to accept a change, or 'n' to reject.
If you're feeling clever, you can use the shortcut to the shortcut (do you see why Emacs is so hard now?), by typing 'M-%' to run a query replace. In practice, the '%' sign is reached by holding down 'Shift' and pressing '5', so you need to hold down 'Alt', hold down 'Shift' and press 5.
If you make a change with your search and replace (all too often we've used 'replace-string' when we really meant 'query-replace'), you can undo your last action with 'C-x u'. An 'action' is one complete change, which means one change if you used 'query-replace', or many changes if you used 'replace-string'. Alternatively, there is an Undo menu item under the Edit menu, along with various search and replace items, which mean you needn't worry too much about the shortcuts if you really don't want to!
Cut, copy, and paste
As you are learning with the X version of Emacs, all three of these operations can be performed using the mouse. As with other text editors, you can just select text and use either Cut, Copy, or Paste from the Edit menu. Of course, if you want to learn Emacs properly, you need to learn the shortcuts. And this is made more complicated by the fact that, because Emacs works in a console, you need to be able to select text without a mouse.
The first thing to do is position the cursor where you want to start copying. Now press 'C-space', space being the Spacebar on your keyboard. Moving the cursor now will highlight characters in blue, showing you your selection. When you have the text you want, press 'M-w' to copy the text to the clipboard, or use 'C-w' to cut the text to the clipboard (so you copy it, then delete it). Now position the cursor where you want to paste, and press 'C-y' for 'paste', which is short for 'yank'. Cunningly, the word 'yank' means 'paste' in Emacs, but 'copy' in Vim – isn't choice great?
The help files
The help files for Emacs are built into the editor, and you can access them either through the menu bar or through yet more shortcuts. Fortunately, for all of us, the help files uniformly consist of the shortcut 'C-h' and another key. If you have it installed, the best place to start is at the Emacs tutorial ('C-h t'). It gives lots of text examples to get you into the Emacs frame of mind. If you're already working smoothly, the Emacs FAQ ('C-h F') will be a better place to start as it loads the info page up with questions about general use. Given that Emacs has been around for a long time, lots of questions have been asked. So you should almost certainly be able to find your answer here!
There are a few commands that bring up information on Emacs commands and usage. For example, 'C-h a' lets you enter an Emacs command to find out what it does. So 'C-h a' then 'replace-string' and 'Enter' will tell you that the replace-string function finds and replaces text. You can get information on commands by using 'C-h c', which prompts you to enter another command. For example, 'C-h c' then 'C-h a' prints 'C-h a runs the command apropos-command'.
One final command that may be of use is 'C-h b', which lists all the keystrokes currently assigned to an action, as well as what they do (there are quite a few of these). You can get information on what each command does by moving the cursor, with your keys or the mouse, over the underlined command name, and hitting 'Enter'.
Emacs, the kitchen sink
Stressed out by shortcuts? Speak to the psychiatrist!
When I said that Emacs is considered an OS by many, I meant it. Just a brief scan around some of the menu items should have shown you how much there is to offer. Emacs' flexibility is possible because it has the Lisp programming language deeply embedded inside it, and so people are free to create very complex add-ons for it.
For example, under the Tools menu you'll see controls to read Usenet and email, apply patches and compare files, run debuggers and play games. The games are marvellously simple – Snake, Tetris and Adventure (a text-based RPG), being some on offer. But, perhaps the most interesting of all is the Emacs psychiatrist, which is (tellingly) located under the Help menu. Here you can offload your emotions, rationalise your fears, and plead your beliefs while the Emacs doctor pretends to be listening to you. Just don't come to it with high expectations! A much more interesting diversion is pressing 'M-x' then running psychoanalyze-pinhead, which uses quotes from Zippy the Pinhead input for Emacs' psychiatrist – just hit 'C-c' to stop the madness.
Emacs has something for everyone. For example, if you spent the few years of your Linux life learning the Vim editor (don't worry – we all make mistakes!), you can transfer that learning to Emacs through 'Viper' mode. 'Viper' stands for 'Viper Is a Package for Emacs Rebels', making it clear that although Vim users and Emacs users strongly disagree about choice of editor, there is at least some cross over.
Type 'M-x viper-mode' to enable Viper mode. Emacs will ask if you want to suppress the start-up message (type 'no' so that you see the info screen each time), then Viper will prompt you for a 'Viper level'. Put simply, Viper level 1 works almost exactly like Vim, level 2 adds a few Emacs features, level 3 adds more, level 4 is starting to feel half like Emacs, and level 5 is an equal blend of both Vim and Emacs. Start with level 1 if you value your sanity. We recommend you don't make the change permanent, so that you can easily select other levels as your competency improves. Once in Viper mode, hit 'Escape', then press ':q' to quit, as in Vim.
Posted on Utopian.io - Rewarding Open Source Contributors
Your contribution cannot be approved yet because it is not as informative as other contributions. See the Utopian Rules. Please edit your contribution and add try to improve the length and detail of your contribution (or add more images/mockups/screenshots), to reapply for approval.
You may edit your post here, as shown below:
You can contact us on Discord.
[utopian-moderator]
How did you overcome the coursor movement keys? Those are the worst i ve ever seen
Your contribution cannot be approved because it does not follow the Utopian Rules.
You can contact us on Discord.
[utopian-moderator]