Timeline
Timeline
2025-01-09
init
This article introduces common selection commands in the Emacs editor and their functions, and focuses on the location and lookup methods of Emacs configuration files on Windows systems. The article points out that Emacs configuration files can be in three formats: .emacs, _emacs, or .emacs.d/init.el, but regardless of format, they must be placed in the correct HOME directory to be recognized. To address the issue that Windows has no native concept of a home directory, Emacs determines the HOME directory by checking, in order, the HOME environment variable, registry key values, the existence of C:\.emacs, and so on. The author recommends setting the HOME environment variable first, but notes that this variable is also used by other programs such as VIM and Firefox plugins; if you want to store only Emacs configuration there, you can add a HOME key value in the registry. In addition, the article briefly lists basic operation commands such as exiting Emacs and moving the cursor.
Common selection commands
| Instruction | Name | Effect |
|---|---|---|
| M - h | mark-paragraph | Select paragraph |
| C - x + C - p | mark-page | Select entire page |
| C - x h | mark-whole-buffer | Select entire editor window content |
| M - y | yank-pop | Use after C-y to paste previous items from 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 that are 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 variableHOME 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.
In other words, Emacs configuration files come in three formats: the .emacs file, the _emacs file, or the init.el file (for version 22 or higher) in the .emacs.d directory. However, regardless of the format, it must be placed in the correct directory for Emacs to find it, and that correct directory is the HOME directory.
Friends familiar with Linux generally know the home directory, simply represented by ~, or by the full path /home/
- If the HOME environment variable is set, its value is used as the home directory.
- If the registry key HKCU\SOFTWARE\GNU\Emacs\HOME exists, its value is used as the home directory.
- If the registry key HKLM\SOFTWARE\GNU\Emacs\HOME exists, its value is used as the home directory. (The difference from 2 is that 2 is the registry path for the current user only, while 3 is for all users.)
- If C:.emacs exists, use C:\ as the home directory.
- If none of the above exists, 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, 1 is a better practice, so it also has the highest priority and should be the recommended approach. Therefore, just create a HOME environment variable and set its value to the location where you want to place the Emacs configuration files.
Previously I set the HOME directory according to the above method, 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 it:
- VIM: VIM will put the _viminfo file in this directory;
- VIM’s neocomplcache plugin: it will place a .neocon directory in it;
- Firefox’s Pentadactyl plugin: it will put a pentadactyl directory in it.
Therefore, my personal suggestion is:
- If you want a common directory to store these configurations, just like the ~ directory on Linux, then setting the HOME environment variable is most suitable;
- If you want this directory to store only Emacs configuration, then don’t use the HOME environment variable; instead, add a HOME key value in the registry. (At least I personally prefer this approach.)
That is:
Before
HKEY_CURRENT_Under USER\SOFTWARE, create a new GNU key, under GNU create an Emacs key, then under Emacs create a string value with the key HOME and the value being the path to store.
emacs tutorials
C:Ctrl
M: Alt
Exit Emacs
C-x C-c
Quit a running command
C-g
Move to the next screen
C-v
Move to the previous screen
M-v
Redraw the screen, placing the line containing the cursor in the center of the screen.
C-l (here it is CTRL-L)
Basic cursor control
1234567 | 上一行 C-p : :向左移 C-b .... 目前光标位置 .... 向右移 C-f : : 下一行 C-n |
Each line of text ends with a ‘newline character’, which separates lines from each other. (Normally, the last line of a file has a newline, but Emacs does not require this.)
Previous line
C-p
previous
Next line
C-n
next
Move left one character
C-b
backward
Move right one character
C-f
forward
Move cursor forward one word
Here, ‘word’ means an English word for English text, and for Chinese it means moving to the next punctuation mark.
M-f
Move cursor backward one word
M-b
Move the cursor to the beginning of the line
C-a
ahead
Move the cursor to the end of the line
C-e
end
Move the cursor to the beginning of a sentence
M-a
Move the cursor to the end of a sentence
M-e
Move the cursor to the very beginning of all text
M-<(Actually it is alt+shift+,)
Move the cursor to the very end of all text
M->(Actually it is alt+shift+.)
Select multiple characters with the cursor
C-shift-r C-shift-f
Move block of lines
123 | (use-package drag-stuff :bind (("<M-up>" . drag-stuff-up) ("<M-down>" . drag-stuff-down))) |
M-up arrow
Swap order
Swap the two characters before and after the cursor
C-t
Swap the two words before and after the cursor
M-t
Copy
M-w
Cut
C-w
Paste
C-y
yank
Numeric arguments
Most Emacs commands accept numeric arguments, and for most commands, the function of these numeric arguments is toSpecify the number of times the command is repeated
First type C-u, then type the number as the argument, and finally type the command
For example: C-u 8 C-f moves forward 8 characters
Some Emacs commands use numeric arguments for other purposes
For example: C-v and M-v
When given an argument, they scroll by the specified number of lines instead of screens
That is, C-u 8 C-v moves the cursor down 8 lines
Window
Close extra windows
C-x 1
Keep only one window, that is, close all other windows, expand the kept window to the entire screen, and close all other windows
There is a series of commands that begin with CONTROL-x; many of these commands are related to windows, files, buffers, and so on
Insert
Insert multiple identical characters
C-u 8 *****
This will insert*********
Delete
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 ‘killing’ and ‘deleting’ is that killed text can be reinserted (at any position), while deleted text cannot be reinserted in the same way (though this can be done by undoing a delete command, as mentioned later). [In fact, killed text, although it appears to ‘disappear’, is actually recorded by Emacs and can therefore be retrieved; deleted text, although it may still be in memory, has been ‘abandoned’ by Emacs, so it cannot be retrieved.] Reinserting killed text is called ‘yanking’. In general, commands that may kill a lot of text record the killed text (they are set to be ‘yankable’), while commands that kill only a single character or only whitespace do not record the killed content (so naturally you cannot yank it).
Note that a single C-k kills the contents of a line, while a second C-k kills the newline and causes all subsequent lines to move up. C-k handles numeric arguments in a special way: it kills that many lines along with their newlines, rather than simply repeating C-k. For example, C-u 2 C-k kills two lines and their newlines; typing C-k twice obviously would not have this result.
Yanking (pasting)
The action of reinserting killed text to restore it is called ‘yanking’. (It’s as if you forcefully pull back something that was taken away from you.) You can yank at the place where you killed the text, or elsewhere, and you can yank the same text multiple times to get multiple copies of it. Many other editors call killing and yanking ‘cutting’ and ‘pasting’.
Yank the most recently killed text.
C-y
yanking
Yank the text from previous kills.
M-y
You need to press C-y first, then M-y.
Undo
C-/
or C-_ or C-x u
Search
Emacs can search forward or backward for a string (a ‘string’ means a sequence of consecutive characters).
The search command is acursor-moving command: after a successful search, the cursor stops at the location where the search target appears.
Search forward
C-s
In an incremental search, Emacs tries 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 beeps to tell you the search failed. At any point, you can use C-g to terminate the search. [You will find that C-g returns the cursor to the position where the search started, while
Search backward
C-r
Replace
M-%
When replacing, press y to confirm the replacement, n to skip the replacement at this occurrence, and ! to replace all.
files
Find a file
C-x C-f
Save the file
C-x C-s
This command stores the text in Emacs into a file. The first time you save, Emacs renames the original file as a backup. The renaming rule is usually to add a ‘~’ character after the original file name. This feature can be turned off.
buffer
Emacs stores each file being edited in a place called a “buffer”. Each time you find a file, Emacs opens a buffer for it. Use the following command to list all current buffers:
C-x C-b List buffers
Type C-x 1 to leave the buffer list
No matter how many buffers exist, there can only be one “current” buffer at any time, which is the one you are editing. If you want to edit another buffer, you must “switch” to it. As mentioned above, using C-x C-f is one way. But there is a simpler way: use C-x b. With this command, you must enter the name of the buffer.
In most cases, a buffer has the same name as the file it corresponds to (excluding the directory name), but this is not always true. The buffer list obtained with C-x C-b always shows the buffer name.
A buffer does not necessarily have a corresponding file. The buffer that displays the buffer list (called “Buffer List”) is like this. This TUTORIAL.cn buffer initially had no corresponding file, but now it does, because in the previous section you typed C-x C-s to save it as a file.
“MessagesThe buffer also has no corresponding file; this buffer stores 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 exist only in Emacs, that is, in its corresponding buffer. Also, changes to the second file will not affect the first file. This is useful, but it also means 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 is too troublesome. You need a simpler method, and Emacs has one ready for you:
C-x s Save multiple buffers
C-x s finds all buffers that have been modified but not yet saved, and then asks you one by one: do you want to save?
Extending the Command Set
Emacs commands are like the stars in the sky; there are countless numbers. It is obviously impossible to map them all to CONTROL and META key combinations. Emacs solves this problem with extended (eXtend) commands, which come in two styles:
12 | C-x 字符扩展。 C-x 之后输入另一个字符或者组合键。 M-x 命令名扩展。M-x 之后输入一个命令名 |
Sort lines
M-x sort-lines
Count words
Count the entire buffer
M-x count-words
Count the selected region
M-x count-words-region
Auto Save
If you have modified a file but your computer crashes before you have a chance to save it, the changes you made are likely to be lost. To avoid such misfortune, Emacs periodically writes the file you are editing to an “auto-save” file. The auto-save file’s name has a “#” character at both the beginning and the end. For example, if the file you are editing is called “hello.c”, its auto-save file is called “#hello.c#”. This file is deleted by Emacs after you save normally.
So, if the misfortune really happens, you can calmly open the original file (note: not the auto-save file) and then type M-x recover-file
Mode Line
The line just above the echo area is called the “mode line”. The mode line displays some information, such as:
-😗*- TUTORIAL.cn 63% L749 (Fundamental)
- The mode line shows the status of Emacs and some information about the text you are editing.
- You should know what the filename means, right? It’s the file you found.
- -NN%-- shows the position of the cursor in the entire file. If it is at the beginning of the file, it displays --Top-- instead of --00%–; if it is at the end of the file, it displays --Bot–. If the file is small enough to fit entirely on one screen, the status bar displays --All–.
- The ‘L’ and the number after it give the line number of the line where the cursor is.
- The asterisk (*) at the very beginning indicates that you have made changes to the text. A file that has just been opened certainly has not 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 editing mode currently in use. The default mode is Fundamental, which is the one you are using now. It is a ‘major mode’.
Mode
Major modes are extension commands that can be started with M-x; M-x fundamental-mode switches to Fundamental mode.
M-x text-mode
Use C-h m to view documentation for the current major mode, and type C-x 1 to close the documentation pane.
- Major modes are called ‘major’ because there are also ‘minor modes’. Minor modes do not replace major modes; they provide auxiliary functions. Each minor mode can be turned on and off independently, regardless of other minor modes and regardless of the major mode. So you can use no minor modes, or use just one, or use several at the same time.
- There is a minor mode called Auto Fill that is very useful, especially when editing natural language text. When Auto Fill is enabled, Emacs automatically breaks lines for you when you type past the line boundary.
- Use M-x auto-fill-mode
This turns on Auto Fill mode. Using the command again turns it off. In other words, if Auto Fill mode is not on, this command turns it on; if it is already on, this command turns it off. So we say this command toggles the mode. - The line boundary is usually set to 70 characters (here referring to English characters). You can reset it with the C-x f command and a numeric argument.
- If you make changes in the middle of a paragraph, Auto Fill mode will not re-wrap the entire paragraph for you; you need to use M-q to wrap it manually. Note that the cursor must be in the paragraph you want to wrap.
Window management
Buffer switching
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
- x ;; Execute the operation
- ? ;; View key help
Multiple panes (split screen)
Split the screen into two panes.
C-x 0
Close the current split.
C-x 1
Keep only the current split.
C-x 2
Split horizontally (top/bottom).
C-x 3
Split vertically (left/right).
Adjust split width.
Increase height C-x ^
Increase/decrease width C-x
Move the cursor to another pane.
C-x o
o stands for other.
When you are editing in one pane but using another pane as a reference, C-M-v is a very useful command. Without leaving the selected pane, you can use the C-M-v command to scroll the text in another pane. [For example, translation and proofreading are well suited to this method.]
In the upper pane, type C-x 1 to close the lower pane.
When there are many splits, it is very inefficient. With ace-window, you can quickly jump between windows. There are many similar plugins, but this one is the best solution.
M-o
12 | (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 Emacs’s command documentation. 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 typing C-h c, type a key combination, and Emacs will give a brief description of the command.
Type C-h c C-p.
The message displayed should be something like:
C-p runs the command previous-line
This message shows the function name corresponding to the C-p command. Commands are implemented by functions, so the function name itself can be seen as the simplest documentation—at least for the commands you have learned, their function names are enough to explain their functions.
For more information, try replacing C-h c with C-h k.
Type C-h k C-p.
The above command will open a new Emacs pane to display the function’s 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 do something else in the editing pane first, and then close the help pane.
C-h f explains a function. You need to type the function name.
Type C-h f previous-line
C-h v is used to display documentation for Emacs variables. Emacs variables can be used to ‘customize Emacs behavior’. Similarly, you need to type the variable’s name.
C-h a is command apropos. Type a keyword and Emacs will list all commands whose names contain that keyword. All of these commands can be launched with M-x. For some commands, command apropos will also list one or two key bindings.
Type C-h a file
Emacs will display a list of M-x commands in another pane, containing all commands whose names contain ‘file’. You can see key bindings like ‘C-x C-f’ displayed next to command names like ‘find-file’.
C-h i reads the manuals (that is, what is usually called Info). This command opens a special buffer called ‘infoInfo’, where you can read the manuals for software packages installed on the system. To read the Emacs manual, press m emacs
