Emacs
Commonly used selection commands
Command | Name | Effect |
---|---|---|
M - h | mark-paragraph | Select a paragraph |
C - x + C - p | mark-page | Select the entire page |
C - x h | mark-whole-buffer | Select the entire content of the editing window |
M - y | yank-pop | After C - y, paste previous items in the clipboard |
Location of Emacs configuration files on Windows
On Windows, the .emacs file may be called _emacs for backward compatibility with DOS and FAT filesystems where filenames could not start with a dot. Some users prefer to continue using such a name because Explorer cannot create a file with a name starting with a dot, even though the filesystem and most other programs can handle it. In Emacs 22 and later, the init file may also be called .emacs.d/init.el. Many of the other files created by Lisp packages are now stored in the .emacs.d directory too, so this keeps all your Emacs-related files in one place.
All the files mentioned above should go in your HOME directory. The HOME directory is determined by following the steps below:
- If the environment variable HOME is set, use the directory it indicates.
- If the registry entry HKCU\SOFTWARE\GNU\Emacs\HOME is set, use the directory it indicates.
- If the registry entry HKLM\SOFTWARE\GNU\Emacs\HOME is set, use the directory it indicates. Not recommended, as it results in users sharing the same HOME directory.
- If C:.emacs exists, then use C:. This is for backward compatibility, as previous versions defaulted to C:\ if HOME was not set.
- Use the user’s AppData directory, usually a directory called Application Data under the user’s profile directory, the location of which varies according to Windows version and whether the computer is part of a domain.
That is to say, there are three formats for Emacs configuration files: the .emacs file, the _emacs file, or the init.el file in the .emacs.d directory (for version 22 or higher). However, no matter which format is used, Emacs can only find the configuration file if it is placed in the correct directory, which is the HOME directory.
Friends familiar with Linux generally know the home directory, which is simply represented by ~ or the full path /home/
- If the HOME environment variable is set, use its value as the home directory ~.
- If the registry key HKCU\SOFTWARE\GNU\Emacs\HOME exists, use its value as the home directory ~.
- If the registry key HKLM\SOFTWARE\GNU\Emacs\HOME exists, use its value as the home directory ~ (the difference from 2 is that 2 is only for the current user’s registry path, while 3 is for all users).
- If C:.emacs exists, use C:\ as the home directory ~.
- If none of the above exist, use
\Users \AppData\Roaming as the home directory ~ (for XP and earlier Windows users, you need to look in the Documents and Settings directory).
From the above strategies, method 1 is a better practice, so it has the highest priority and should be the recommended way. Therefore, create a new HOME environment variable and set its value to the place where you want to place the Emacs configuration files.
Previously, I set the HOME directory as described above, but now I regret it because I found that not only Emacs uses the HOME environment variable. At least the following programs or plugins also use this variable:
- VIM, which places the _viminfo file in this directory.
- The neocomplcache plugin for VIM, which places a .neocon directory in it.
- The pentadactyl plugin for Firefox, which places a pentadactyl directory in it.
Therefore, personal suggestions are as follows:
- If you want a common directory to store these configurations, just like the ~ directory on Linux, then setting the HOME environment variable is the most suitable option.
- If you want this directory to store only Emacs configurations, then don’t use the HOME environment variable. Instead, add a HOME key value in the registry. (Personally, I prefer this method.)
That is:
Create a GNU item under HKEY_LOCAL_MACHINE\SOFTWARE, create an Emacs item under the GNU item, and then create a string value in Emacs with the key HOME and the value set to the path where you want to store the files.
Emacs tutorials
C: Ctrl
M: Alt
Exit Emacs
C-x C-c
Exit a running command
C-g
Move to the next screen
C-v
Move to the previous screen
M-v
Redraw the screen and center the line where the cursor is located on the screen
C-l (here it is CTRL-L)
Basic cursor control
1 | Previous line C-p |
Each line of text ends with a “newline character”, which distinguishes one line from another. (Normally, the last line of a file has a newline character, but Emacs does not enforce this.)
Move to the previous line
Ctrl + p
Previous
Move to the next line
Ctrl + n
Next
Move one character to the left
Ctrl + b
Backward
Move one character to the right
Ctrl + f
Forward
Move the cursor forward one word
Here, a word refers to an English word in English text and means moving to the next punctuation mark in Chinese text.
Alt + f
Move the cursor backward one word
Alt + b
Move the cursor to the beginning of the line
Ctrl + a
Beginning of the line
Move the cursor to the end of the line
Ctrl + e
End of the line
Move the cursor to the beginning of a sentence
Alt + a
Move the cursor to the end of a sentence
Alt + e
Move the cursor to the very beginning of all the text
Alt + Shift + ,
Move the cursor to the very end of all the text
Alt + Shift + .
Select multiple characters with the cursor
Ctrl + Shift + r Ctrl + Shift + f
Move line blocks
1 | (use-package drag-stuff |
M - Up Arrow
Swap Order
Swap two characters before and after the cursor
C - t
Swap two words before and after the cursor
M - t
Copy
M - w
Cut
C - w
Paste
C - y
yank
Parameter Control
Most Emacs commands accept numerical parameters. For most commands, these numerical parameters are used to specify the number of times the command should be repeated.
First, enter C - u, then enter a number as a parameter, and finally enter the command.
For example: C - u 8 C - f will move the cursor forward by 8 characters.
Some Emacs commands use numerical parameters for other purposes.
For example: C - v and M - v.
When given a parameter, they will scroll by the specified number of lines instead of screens.
That is, C - u 8 C - v will move the cursor down by 8 lines.
Panes
Close redundant panes
C - x 1
Keep only one pane, which means closing all other panes, expanding the remaining pane to the entire screen, and closing all other panes at the same time.
There is a series of commands starting with CONTROL - x. Many of these commands are related to panes, files, buffers, etc.
Insertion
Insert multiple identical characters
C - u 8 *
This will insert *.
Deletion
Delete the character after the cursor
C - d
delete
Equivalent to the delete key on the keyboard.
Delete the word after the cursor
M - d
Delete the characters from the cursor to the end of the line
C - k
kill
Delete the characters from the cursor to the end of the sentence
M - k
Note that the difference between “kill” and “delete” is that the killed text can be re - inserted (anywhere), while the deleted text cannot be re - inserted in the same way (although you can undo a delete command, which will be mentioned later). [In fact, although the killed text seems to “disappear”, it is actually recorded by Emacs and can be retrieved; while the deleted text may still be in memory, but it has been “discarded” by Emacs and cannot be retrieved.] Re - inserting the killed text is called “yanking”. Generally, commands that may delete a large amount of text will record the deleted text (they are set to be “yankable”), while commands that only delete one character or only delete whitespace will not record the deleted content (naturally, you cannot yank it).
Note that a single C - k will remove the content of a line, and the second C - k will remove the newline character and move all subsequent lines up. The way C - k handles numerical parameters is very special. It will remove the specified number of lines along with their newline characters, rather than simply repeating C - k. For example, C - u 2C - k will remove two lines and their newline characters; obviously, just entering C - k twice will not achieve this result.
Yank (Paste)
The action of re - inserting the removed text is called “yanking”. (It’s like pulling back something that was taken away from you forcefully.) You can yank at the place where you deleted the text, or at other places, and you can also yank the same text multiple times to get multiple copies. Many other editors call “kill” and “yank” “cut” and “paste”.
Yank the most recently removed content
C - y
yanking
Yank the previously removed content
M - y
You need to press C - y first and then M - y.
Undo
C - /
Or C - _ or C - x u
Search
Emacs can search for strings (a “string” refers to a sequence of consecutive characters) forward or backward.
The search command is a command to move the cursor: after a successful search, the cursor will stay at the position where the search target appears.
Search forward
C - s
During an incremental search, Emacs will try to jump to the position where the search target appears. To jump to the next match, press C - s again. If the target cannot be found, Emacs will beep to tell you that the search has failed. Throughout the process, you can use C - g to terminate the search. [You will find that C - g will move the cursor back to the starting position of the search, while
Search backward
C - r
Replace
M - %
When replacing, press y to confirm the replacement, n to skip the current replacement, and! to replace all.
Files
Find a file
C - x C - f
Save a file
C - x C - s
This command saves the text in Emacs to a file. When saving a file for the first time, Emacs will rename the original file for backup. The renaming rule is usually to add a “~” character after the original file name. You can turn off this feature.
Buffers
Emacs stores each file being edited in a place called a “buffer”. Every time you find a file, Emacs creates a buffer inside. You can list all the current buffers with the following command:
C - x C - b List buffers
Enter C - x 1 to leave the buffer list.
Regardless of how many buffers there are, there can only be one “current” buffer at any time, which is the one you are currently editing. If you want to edit another buffer, you must “switch” to it. As mentioned above, using C - x C - f is one way. However, there is a simpler way, which is to use C - x b. With this command, you must enter the name of the buffer.
In most cases, the buffer has the same name as its corresponding file (excluding the directory name), but this is not absolute. The buffer list obtained by C - x C - b always shows the buffer names.
A buffer does not necessarily have a corresponding file. The buffer that displays the buffer list (called “Buffer List“) is such an example. The TUTORIAL.cn buffer initially did not have a corresponding file, but now it does because in the previous section you entered C - x C - s to save it as a file.
The “Messages“ buffer also does not have a corresponding file. This buffer stores all the messages that appear at the bottom of Emacs.
If you make some changes to a file and then switch to another file, this action will not save the previous file for you. The changes to the first file still only exist in Emacs, that is, in its corresponding buffer. Moreover, the changes to the second file will not affect the first file. This is very useful, but it also means that you need a convenient way to save the buffer of the first file. Switching back to that buffer and then using C - x C - s to save it is too troublesome. You need a simpler method, and Emacs has already prepared one for you:
C - x s Save multiple buffers
C - x s will find all the buffers that have been modified but not saved, and then ask you one by one: Do you want to save them?
Command Set Extension
The number of Emacs commands is as countless as the stars in the sky. It is obviously impossible to map them all to the CONTROL and META key combinations. Emacs uses extension (eXtend) commands to solve this problem. There are two styles of extension commands:
1 | C - x Character extension. Enter another character or key combination after C - x. |
Line Sorting
M-x sort-lines
Word Counting
Count the entire buffer
M-x count-words-region
Count the selected region
M-x count-words
Automatic Saving
If you have modified a file but your computer crashes before you can save it, the changes you made are likely to be lost. To avoid such an unfortunate situation, Emacs periodically writes the file you are editing into an “automatic save” file. The file name of the automatic save file has a “#” character at both the beginning and the end. For example, if the file you are editing is called “hello.c”, then its automatic save file is called “#hello.c#”. This file will be deleted by Emacs after a regular save.
So, if such an unfortunate event does occur, you can calmly open the original file (note that it’s not the automatic save file) and then enter M-x recover-file <Return>
to restore your automatic save file. When prompted for confirmation, enter yes <Return>
.
Status Bar
The line directly above the echo area is called the “status bar”. The status bar displays some information, such as:
-:**- TUTORIAL.cn 63% L749 (Fundamental)
- The status bar shows the status of Emacs and some information about the text you are editing.
- You should know what the file name means, right? It’s the file you found.
- -NN%— indicates the position of the cursor in the entire text. If it’s at the beginning of the file, it shows —Top— instead of —00%—; if it’s at the end of the file, it shows —Bot—. If the file is small enough to be displayed entirely on one screen, the status bar will show —All—.
- The “L” followed by a number gives the line number where the cursor is located.
- The asterisk (*) at the very beginning indicates that you have made changes to the text. A newly opened file definitely hasn’t been modified, so the status bar shows a dash (-) instead of an asterisk.
- The content in the parentheses on the status bar tells you the current editing mode. The default mode is Fundamental, which is the one you are currently using. It is a “major mode”.
Modes
Major modes are all extended commands that can be launched with M-x. You can switch to the Fundamental mode by using M-x fundamental-mode
.
M-x text-mode <Return>
: Switch to text mode.
You can view the documentation of the current major mode by using C-h m
. Enter C-x 1
to close the documentation pane.
- Major modes are called “major” because there are also “minor modes” at the same time. Minor modes cannot replace major modes but provide some auxiliary functions. Each minor mode can be independently enabled and disabled, regardless of other minor modes and the major mode. So you can choose not to use minor modes, or use only one or multiple minor modes simultaneously.
- A minor mode called “Auto Fill” is very useful, especially when editing natural language text. After enabling Auto Fill, Emacs will automatically insert a line break when you type beyond the line boundary.
- Use
M-x auto-fill-mode <Return>
to start the Auto Fill mode. Using this command again will turn off the Auto Fill mode. That is to say, if the Auto Fill mode is not enabled, this command will enable it; if it is already enabled, this command will disable it. Therefore, we say that this command can be used to “toggle” the mode. - The line boundary is usually set to 70 characters (here referring to English characters). You can use the
C-x f
command with a numeric parameter to reset it. - If you make some changes in the middle of a paragraph, the Auto Fill mode will not re-wrap the entire paragraph for you. You need to manually re-wrap it using
M-q
. Note that the cursor must be within the paragraph you want to re-wrap.
Window Management
Switch buffers
C-x b
Kill the current buffer
C-x k
Manage buffers in bulk
C-x C-b ;; Enter the buffer list
- d ;; Mark for deletion
- u ;; Unmark the current line
- U ;; Unmark all lines
- x ;; Execute the operation
- ? ;; View key help
Multiple Panes (Split Screen)
Divide the screen into two panes
C-x 0
Close the current split screen
C-x 1
Keep only the current split screen
C-x 2
Split the screen vertically
C-x 3
Split the screen horizontally
Adjust the width of split screens
Increase the height: C-x ^
Increase/decrease the width: C-x {C-x }
Move the cursor to another pane
C-x o
Here, “o” means “other”.
When you are editing in one pane and using another pane as a reference, the C-M-v
command is very useful. You can use the C-M-v
command to scroll the text in another pane without leaving the selected pane. [For example, translation and proofreading are very suitable to be done in this way.]
(In the upper pane) Enter C-x 1
to close the lower pane.
When there are many split screens, the efficiency is very low. You can use the ace-window
plugin to quickly jump between windows. There are many similar plugins, but this one is the best solution.
M-o1
2(use-package ace-window
:bind (("M-o" . 'ace-window)))
Open a file in a new pane
C-x 4 C-f
Multiple windows
Create a new window
M-x make-frame
Close the selected window
M-x delete-frame
Get more help
Emacs provides some commands to view the documentation of Emacs commands. These commands all start with CONTROL-h, and this character is therefore called the “Help character”.
The most basic help function is C-h c. After entering C-h c and then a key combination, Emacs will give a brief description of this command.
Enter C-h c C-p.
The displayed message should be like this:
C-p runs the command previous-line
This message shows the function name corresponding to the C-p command. The function of a command is implemented by a function, so the function name itself can also be regarded as the simplest documentation - at least for the commands you have learned, their function names are sufficient to explain their functions.
If you want more information, try replacing C-h c with C-h k.
Enter C-h k C-p.
The above command will open a new Emacs pane to display the function name and its documentation. After you finish reading, you can use C-x 1 to close this help pane. Of course, you don’t need to do this immediately. You can first do something else in the editing pane and then close the help pane.
C-h f explains a function. You need to enter the function name.
Enter C-h f previous-line
C-h v is used to display the documentation of Emacs variables. Emacs variables can be used to “customize the behavior of Emacs”. Similarly, you need to enter the name of the variable.
C-h a is for Command Apropos. Enter a keyword and Emacs will list all commands whose names contain this keyword. All these commands can be launched with M-x. For some commands, the Command Apropos will also list one or two key combinations.
Enter C-h a file
Emacs will display a list of M-x commands in another pane. This list contains all commands whose names contain “file”. You can see key combinations like “C-x C-f” displayed next to command names like “find-file”.
C-h i is for reading the manual (usually referred to as Info). This command will open a special buffer called “info“, where you can read the usage manuals of the software packages installed in the system. To read the Emacs usage manual, press m emacs