[2024-feb-29] Sad news: Eric Layton aka Nocturnal Slacker aka vtel57 passed away on Feb 26th, shortly after hospitalization. He was one of our Wiki's most prominent admins. He will be missed.
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
slackbook:emacs [2012/08/20 15:53 (UTC)] – created kookiemonster | slackbook:emacs [2012/09/16 16:50 (UTC)] – [Emacs Cheat Sheet] **Emacs Cheat Sheet** ^Command^Result| |<key>C-f</key>|Move the cursor one character to the right (forward)| |<key>C-b</key>|Move the cursor one character to the left (backward)| |<key>C-n</key>|Move the cursor one line down (next)| |< mfillpot | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Emacs ====== | ====== Emacs ====== | ||
+ | ===== What is Emacs? ===== | ||
- | While [[slackbook: | + | **//vi//** and its clones |
+ | and powerful editors. | ||
+ | particularly extensible. | ||
+ | successful | ||
+ | **//vi//** variant that shrugs this trend, | ||
+ | being both extremely extensible | ||
+ | users prefer | ||
+ | many people | ||
+ | **//Emacs//**. | ||
- | Also unlike vi, which is an (excellent) editor and nothing more, emacs is a program with near endless capabilities. emacs is (for the most part) written in Lisp, which is a very powerful programming language that has the peculiar property that every program written in it is automatically a Lisp compiler of its own. This means that the user can extend emacs, and in fact write completely new programs "in emacs" | ||
- | As a result, emacs is not just an editor anymore. There are many add-on packages for emacs available | + | Emacs takes extensibility up to eleven. |
+ | the rest of **// | ||
+ | Lisp variant, nearly all of which is exposed to you, so that you | ||
+ | may configure it or even extend it at will (many good Emacs Lisp | ||
+ | tutorials can be found on the Internet). People have written | ||
+ | all sorts of extensions in Emacs Lisp, from syntax highlighting | ||
+ | for an obscure language, to a built-in terminal. In fact, | ||
+ | there's even a **//vi//** emulation mode | ||
+ | within **// | ||
+ | can still get the modal editing that comes with vi, while having | ||
+ | access to the power of the **// | ||
+ | core. | ||
- | There are two main versions of emacs: GNU Emacs (which is the version that comes with Slackware) and XEmacs. The latter is not a version for Emacs running under X. In fact, both emacs and XEmacs run on the console as well as under X. XEmacs was once started as a project to tidy up the emacs code. Currently, both versions are being actively developed, and there is in fact much interaction between the two development teams. For the present chapter, it is immaterial whether you use emacs or XEmacs, the differences between them are not relevant to the normal user. | ||
+ | Like **//vi//**, there are many variants of | ||
+ | **// | ||
+ | the one most commonly used (and the only one in Slackware) is | ||
+ | GNU Emacs. | ||
+ | always mean GNU Emacs. | ||
+ | |||
+ | |||
+ | Unlike **//vi//**, | ||
+ | **// | ||
+ | traditional editor by default. | ||
+ | shortcuts can be performed without repeatedly changing modes. | ||
+ | You can open up a file and start typing away without having to | ||
+ | learn what the modes do, or forgetting which one you are | ||
+ | currently using. | ||
+ | ===== Starting Out ===== | ||
+ | |||
+ | **// | ||
+ | running the **// | ||
+ | terminal. | ||
+ | you first start it in a console without arguments, you will see | ||
+ | something that resembles this: | ||
+ | |||
+ | | ||
+ | [[emacs-startup.png|Starting Out]] | ||
+ | |||
+ | If you are in X windows, **// | ||
+ | start a GUI instead of running in your console. | ||
+ | case and you don't want a GUI, you can invoke it with the flag | ||
+ | //' | ||
+ | |||
+ | |||
+ | While here, you can browse around using the | ||
+ | keyboard arrow keys. | ||
+ | Underlined elements are links, and you can activate them by | ||
+ | moving over them and pressing < | ||
+ | mentioned is very good, and can help you get your bearings | ||
+ | should you have any problems. | ||
+ | sequences such as < | ||
+ | < | ||
+ | key while holding down the < | ||
+ | with **M-`**, meaning to hold the the | ||
+ | **Meta** key (usually < | ||
+ | press the backtick < | ||
+ | the < | ||
+ | holding down the < | ||
+ | is also one of the more important commands in | ||
+ | **// | ||
+ | |||
+ | |||
+ | Alternatively, | ||
+ | file name as an argument, it will open that file, just like | ||
+ | **// | ||
+ | presented with the contents of the file in question. | ||
+ | navigate the document using traditional arrow keys and type in | ||
+ | information at will without any issues. | ||
+ | |||
+ | |||
+ | Say you make some edits, and you now want to save your file. | ||
+ | The following key sequence will do that: < | ||
+ | If you made a new file, you will be prompted for this in what is | ||
+ | called the //" | ||
+ | the bottom of the screen. | ||
+ | then hit < | ||
+ | save the file, you can press < | ||
+ | operations that ask for input. | ||
+ | usually available for operations that use the minibuffer. | ||
+ | |||
+ | |||
+ | Should you want to open a new file within your same | ||
+ | **// | ||
+ | minibuffer. | ||
+ | whether it exists or not. If it doesn' | ||
+ | will be created for it (the file will be created upon saving | ||
+ | with < | ||
+ | expected. | ||
+ | switch back to it using < | ||
+ | the file's name (or more technically, | ||
+ | hitting < | ||
+ | ===== How to Move Around ===== | ||
+ | |||
+ | Like **//vi//**, | ||
+ | **// | ||
+ | keys on your keyboard. | ||
+ | **//vi//**, using the arrow keys to navigate | ||
+ | files is also supported. | ||
+ | **//vi//** movement keys are more ergonomic, | ||
+ | **// | ||
+ | However, it is still very possible to operate using the main | ||
+ | **// | ||
+ | efficiently. | ||
+ | |||
+ | | ||
+ | **Emacs Cursor Movement** | ||
+ | ^Command^Result| | ||
+ | |< | ||
+ | |< | ||
+ | |< | ||
+ | |< | ||
+ | |||
+ | Of course, like with **//vi//** it is also | ||
+ | possible to repeat these commands with a numeric argument. | ||
+ | type in **M-1** **M-0** < | ||
+ | < | ||
+ | **M-5** < | ||
+ | will move five characters to the right. | ||
+ | ===== Getting Help ===== | ||
+ | |||
+ | **// | ||
+ | documentation, | ||
+ | //" | ||
+ | mechanisms for providing users with documentation while you are | ||
+ | using it. | ||
+ | |||
+ | Here are some useful functions that display documentation (they | ||
+ | all start with < | ||
+ | |||
+ | |||
+ | **Accessing Emacs Documentation** | ||
+ | ^Command^Result| | ||
+ | |< | ||
+ | |< | ||
+ | |< | ||
+ | |< | ||
+ | |||
+ | | ||
+ | < | ||
+ | ===== Calling Functions ===== | ||
+ | |||
+ | As noted earlier, **// | ||
+ | large number of functions to for interactive use. Some of | ||
+ | these, like those opening and saving files, are mapped to keys. | ||
+ | Others (like the ones for moving to the beginning and end of | ||
+ | lines) are not. To call them, you have to invoke them. Say we | ||
+ | want to call the function //" | ||
+ | this: | ||
+ | |||
+ | |||
+ | **M-x** end-of-line < | ||
+ | |||
+ | |||
+ | And the cursor would move to the end of the line, as the function name | ||
+ | suggests. | ||
+ | ===== Emacs Cheat Sheet ===== | ||
+ | |||
+ | While Emacs can be simple to use, its scope can easily be | ||
+ | overwhelming. | ||
+ | aspects have been simplified, most notably regarding text | ||
+ | selection. | ||
+ | **// | ||
+ | tutorials. | ||
+ | searches. | ||
+ | |||
+ | |||
+ | **Emacs Cheat Sheet** | ||
+ | ^Command^Result| | ||
+ | |< | ||
+ | |< | ||
+ | |< | ||
+ | |< | ||
+ | |< | ||
+ | |< | ||
+ | |< | ||
+ | |< | ||
+ | |M-< | ||
+ | |< | ||
+ | |M-< | ||
+ | |M-< | ||
+ | |< | ||
+ | |M-< | ||
+ | |M-< | ||
+ | |M-< | ||
+ | |M-< | ||
+ | |< | ||
+ | |< | ||
+ | |< | ||
+ | |M-< | ||
+ | |C-< | ||
+ | |< | ||
+ | |< | ||
+ | |< | ||
+ | |< | ||
+ | |M-< | ||
+ | |< | ||
+ | |< | ||
+ | |< | ||
===== Starting Emacs ===== | ===== Starting Emacs ===== | ||
Line 23: | Line 226: | ||
**Command Keys** | **Command Keys** | ||
- | As mentioned above, emacs uses **CTRL** | + | As mentioned above, emacs uses < |
- | < | + | < |
</ | </ | ||
- | Many emacs commands consist of sequences of keys and key combinations. For example, | + | Many emacs commands consist of sequences of keys and key combinations. For example, |
===== Buffers ===== | ===== Buffers ===== | ||
Line 36: | Line 239: | ||
There is one other special buffer you need to know about, and that is the minibuffer. This buffer consists of only one line, and is always on the screen: it is the very last line of the emacs window, below the status bar for the current buffer. The minibuffer is where emacs shows messages for the user, and it is also the place where commands that require some user input are executed. For example, when you open a file, emacs will ask for its name in the minibuffer. | There is one other special buffer you need to know about, and that is the minibuffer. This buffer consists of only one line, and is always on the screen: it is the very last line of the emacs window, below the status bar for the current buffer. The minibuffer is where emacs shows messages for the user, and it is also the place where commands that require some user input are executed. For example, when you open a file, emacs will ask for its name in the minibuffer. | ||
- | Switching from one buffer to another can be done with the command C-x b. This will prompt you for the name of a buffer (a buffer' | + | Switching from one buffer to another can be done with the command |
If you want to switch to another buffer than the default offered by Emacs, just type its name. Note that you can use so-called Tab-completion here: type the first few letters of the buffer' | If you want to switch to another buffer than the default offered by Emacs, just type its name. Note that you can use so-called Tab-completion here: type the first few letters of the buffer' | ||
- | You can get a list of open buffers by hitting C-x C-b. This command will usually split the screen in two, displaying the buffer you were working in in the top half, and a new buffer called *Buffer List* in the bottom half. This buffer contains a list of all the buffers, their sizes and modes, and the files, if any, that those buffers are visiting (as it is called in emacs). You can get rid of this split screen by typing C-x 1. | + | You can get a list of open buffers by hitting |
===== Modes ===== | ===== Modes ===== | ||
- | |||
Every buffer in emacs has an associated mode. This mode is very different from the idea of modes in vi: a mode tells you what kind of buffer you are in. For example, there is text-mode for normal text files, but there are also modes such as c-mode for editing C programs, sh-mode for editing shell scripts, latex-mode for editing LaTeX files, mail-mode for editing email and news messages, etc. A mode provides special customizations and functionality that is useful for the kind of file you are editing. It is even possible for a mode to redefine keys and key commands. For example, in Text mode, the Tab key simply jumps to the next tab stop, but in many programming language modes, the Tab key indents the current line according to the depth of the block that line is in. | Every buffer in emacs has an associated mode. This mode is very different from the idea of modes in vi: a mode tells you what kind of buffer you are in. For example, there is text-mode for normal text files, but there are also modes such as c-mode for editing C programs, sh-mode for editing shell scripts, latex-mode for editing LaTeX files, mail-mode for editing email and news messages, etc. A mode provides special customizations and functionality that is useful for the kind of file you are editing. It is even possible for a mode to redefine keys and key commands. For example, in Text mode, the Tab key simply jumps to the next tab stop, but in many programming language modes, the Tab key indents the current line according to the depth of the block that line is in. | ||
- | The modes mentioned above are called //major// modes. Each buffer has exactly one major mode. Additionally, | + | The modes mentioned above are called //major// modes. Each buffer has exactly one major mode. Additionally, |
==== Opening Files ==== | ==== Opening Files ==== | ||
- | To open a file in emacs, type C-x C-f | + | To open a file in emacs, type <key>C-x</ |
Emacs will ask you for the name of the file, filling in some default path for you (which is usually ~/ ). After you type the filename (you can use Tab completion) and hit ENTER, emacs will open the file in a new buffer and display that buffer on the screen. | Emacs will ask you for the name of the file, filling in some default path for you (which is usually ~/ ). After you type the filename (you can use Tab completion) and hit ENTER, emacs will open the file in a new buffer and display that buffer on the screen. | ||
Line 57: | Line 259: | ||
< | < | ||
- | In order to create a new file in emacs, you cannot just go typing right away. You first have to create a buffer for it, and come up with a filename. You do this by typing C-x C-f and typing a filename, just as if you were opening an existing file. Emacs will notice that the file you typed doesn' | + | In order to create a new file in emacs, you cannot just go typing right away. You first have to create a buffer for it, and come up with a filename. You do this by typing |
- | When you type C-x C-f and then enter a directory name instead of a filename, emacs will create a new buffer in which you will find a list of all the files in that directory. You can move the cursor to the file that you are looking for and type , and emacs will open it. | + | When you type <key>C-x</ |
< | < | ||
- | When you have typed C-x C-f and suddenly change your mind, you can type C-g to cancel the action. C-g works almost everywhere where you want to cancel an action or command that you've started but don't want to finish. | + | When you have typed <key>C-x</ |
===== Basic Editing ===== | ===== Basic Editing ===== | ||
- | When you have opened a file, you can of course move around in it with the cursor. The cursor keys and **PgUp**, **PgDn** do what you'd expect. | + | When you have opened a file, you can of course move around in it with the cursor. The cursor keys and <key>PgUp</ |
Basic emacs Editing Commands | Basic emacs Editing Commands | ||
^ Command ^ Result ^ | ^ Command ^ Result ^ | ||
- | |C-b|go one character back| | + | |<key>C-b</ |
- | |C-f|go one character forward| | + | |<key>C-f</ |
- | |C-n|go one line down| | + | |<key>C-n</ |
- | |C-p|go one line up| | + | |<key>C-p</ |
- | |C-a|go to the beginning of the line| | + | |<key>C-a</ |
- | |C-e|go to the end of the line| | + | |<key>C-e</ |
- | |M-b|go one word back| | + | |<key>M-b</ |
- | |M-f|go one word forward| | + | |<key>M-f</ |
- | |M-}|go one paragraph forward| | + | |<key>M-}</ |
- | |M-{|go one paragraph backward| | + | |<key>M-{</ |
- | |M-a|go one sentence backward| | + | |<key>M-a</ |
- | |M-e|go one sentence forward| | + | |<key>M-e</ |
- | |C-d|delete the character under the cursor| | + | |<key>C-d</ |
- | |M-d|delete until the end of the current word| | + | |<key>M-d</ |
- | |C-v|go down one screen (i.e., **PgDn**)| | + | |<key>C-v</ |
- | |M-v|go up one screen (i.e., **PgUp**)| | + | |<key>M-v</ |
- | |M-<|go to the beginning of the buffer| | + | |<key>M-< |
- | |M->|go to the end of the buffer| | + | |<key>M- ></ |
- | |C-_|undo the last change (can be repeated); note that you actually have to type SHIFT+CTRL+hyphen for this.| | + | |<key>C-_</ |
- | |C-k|delete to end of line| | + | |<key>C-k</ |
- | |C-s|forward search| | + | |<key>C-s</ |
- | |C-r|reverse search| | + | |<key>C-r</ |
- | Note that many Meta commands are parallel to the **CTRL** | + | Note that many Meta commands are parallel to the < |
- | Also note that **M**-< and **M**-> require you to type **SHIFT**+**ALT**+comma and **SHIFT**+**ALT**+dot respectively, | + | Also note that <key>M-< |
- | Note that C-k deletes (kills, as it is commonly called) all the text after the cursor to the end of the line, but doesn' | + | Note that <key>C-k</ |
===== Saving Files ===== | ===== Saving Files ===== | ||
- | In order to save a file, you type **C**-x **C**-s | + | In order to save a file, you type <key>C-x</ |
- | Emacs will not ask you for a filename, the buffer will just be saved to the file it was loaded from. If you want to save your text to another file, type **C**-x **C**-w | + | Emacs will not ask you for a filename, the buffer will just be saved to the file it was loaded from. If you want to save your text to another file, type <key>C-x</ |
When you save the file for the first time in this session, emacs will normally save the old version of your file to a backup file, which has the same name appended with a tilde: so if you're editing a file cars.txt, emacs will create a backup cars.txt~. | When you save the file for the first time in this session, emacs will normally save the old version of your file to a backup file, which has the same name appended with a tilde: so if you're editing a file cars.txt, emacs will create a backup cars.txt~. | ||
- | This backup file is a copy of the file that you opened. While you are working, emacs will also regularly create an auto-save copy of the work you are doing, to a file named with hash signs: #cars.txt#. This backup is deleted when you save the file with C-x C-s. | + | This backup file is a copy of the file that you opened. While you are working, emacs will also regularly create an auto-save copy of the work you are doing, to a file named with hash signs: #cars.txt#. This backup is deleted when you save the file with <key>C-x</ |
- | When you are done editing a file, you can kill the buffer that holds it by typing C-x k | + | When you are done editing a file, you can kill the buffer that holds it by typing |
- | Emacs will then ask you which buffer you want to kill, with the current buffer as default, which you can select by hitting | + | Emacs will then ask you which buffer you want to kill, with the current buffer as default, which you can select by hitting |
- | If you'd prefer to save the file with a new name so that you don't overwrite the original version, use **C**-x **C**-w instead. You may then give a new name for the file, and the buffer will be updated to reflect the contents of this new file, leaving the old file unchanged. | + | If you'd prefer to save the file with a new name so that you don't overwrite the original version, use <key>C-x</ |
===== Quitting Emacs ===== | ===== Quitting Emacs ===== | ||
- | When you are done with emacs altogether, you can type **C**-x **C**-c | + | When you are done with emacs altogether, you can type <key>C-x</ |
This quits emacs. If you have any unsaved files, emacs will tell you so, and ask if you want to save them each in turn. If you answer no to any of these, emacs will ask for one final confirmation and then quit. | This quits emacs. If you have any unsaved files, emacs will tell you so, and ask if you want to save them each in turn. If you answer no to any of these, emacs will ask for one final confirmation and then quit. | ||
+ | ====== Chapter Navigation ====== | ||
+ | |||
+ | **Previous Chapter: [[slackbook: | ||
+ | |||
+ | **Next Chapter: [[slackbook: | ||
+ | ======Sources====== | ||
+ | * Original source: http:// |