Timeline
Timeline
2025-01-09
init
This article introduces the commonly used selection commands in the Emacs editor and their functions, and focuses on discussing the location and methods of finding Emacs configuration files in Windows systems. The article points out that Emacs configuration files can use three formats: .emacs, _emacs, or .emacs.d/init.el, but they must be placed in the correct HOME directory to be recognized. To address the issue that Windows does not have a native home directory concept, Emacs determines the HOME directory in the following order: the HOME environment variable, registry key values, and the existence of C:.emacs. 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 configurations, you can add a HOME key value in the registry. Additionally, 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 editing 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 in the .emacs.d directory (version 22 or higher). However, regardless of the format, it must be placed in the correct directory for Emacs to find it, and this correct directory is the HOME directory.
Friends familiar with Linux generally know the home directory, simply represented by ~, or by the full path as /home/
- If the HOME environment variable is set, then 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, 1 is a better practice, so it has the highest priority and should be the recommended method. Therefore, create a new 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, but at least the following programs or plugins also use this variable:
- VIM, VIM will put the _viminfo file in this directory;
- VIM’s neocomplcache plugin, it will place a .neocon directory into it;
- Firefox’s pentadactyl plugin, it will put a pentadactyl directory into it.
So, my personal suggestion:
- If you want a common directory to store these configurations, just like the ~ directory in Linux, then setting the HOME environment variable is the most suitable;
- If you want this directory to only store Emacs configurations, then it’s better not to use the HOME environment variable; go add a HOME key in the registry. (At least I personally prefer this method)
Namely:
In
HKEY_CURRENT_Create a new GNU key under USER\SOFTWARE, create a new Emacs key under the GNU key, and then create a new string value under Emacs with the key name HOME and the value being the path where you want to store it.
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 where the cursor is at 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 distinguishes one line from another. (Usually, the last line of a file will have a newline character, but Emacs does not enforce this.)
Previous line
C-p
previous
Next line
C-n
next
Move one character to the left
C-b
backward
Move one character to the right
C-f
forward
Move the cursor forward one word
Here, a “word” means an English word for English, and moving to the next punctuation mark for Chinese.
M-f
Move the 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 the sentence
M-a
Move the cursor to the end of the sentence
M-e
Move the cursor to the very beginning of the text
M-<(Actually alt+shift+,)
Move the cursor to the very end of the text
M->(Actually alt+shift+.)
Select multiple characters with the cursor
C-shift-r C-shift-f
Move line block
123 | (use-package drag-stuff :bind (("<M-up>" . drag-stuff-up) ("<M-down>" . drag-stuff-down))) |
M-Up arrow
Swap order
Swap two characters around the cursor
C-t
Swap two words around the cursor
M-t
Copy
M-w
Cut
C-w
Paste
C-y
yank
Numeric argument
Most Emacs commands accept numeric arguments, and for most commands, the role of these numeric arguments is toSpecify the number of times to repeat the command
First type C-u, then type a number as the argument, and finally type the command
For example: C-u 8 C-f will move forward 8 characters
Some Emacs commands use numeric arguments for other purposes
For example: C-v and M-v
When given a numeric argument, they will scroll by the specified number of lines rather than by screens.
That is, C-u 8 C-v will move the cursor down 8 lines
Window
Close extra windows
C-x 1
Keep only one window, that is, close all other windows, expand the remaining window to fill the entire screen, while closing all other windows
There is a series of commands that start with CONTROL-x, many of which are related to windows, files, buffers, etc.
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 characters from the cursor to the end of the line
C-k
kill
Delete characters from the cursor to the end of the sentence
M-k
Note that the difference between “kill” and “delete” is that killed text can be reinserted (at any position), while deleted text cannot be reinserted in the same way (though it can be done by undoing a delete command, which will be mentioned later). [In fact, although killed text seems to “disappear”, it is actually recorded by Emacs, so it can be retrieved; while deleted text may still be in memory, but has been “discarded” by Emacs, so it cannot be retrieved.] Reinserting killed text is called “yanking”. Generally speaking, commands that may eliminate a lot of text will record the eliminated text (they are set to be “yankable”), while commands that only eliminate a single character or only eliminate whitespace will not record the eliminated content (naturally, you cannot yank it back either).
Note that a single C-k removes the contents of a line, while a second C-k removes the newline character and moves all subsequent lines up. C-k handles numeric arguments in a special way: it removes as many lines as specified by the argument along with their trailing newline characters, rather than just repeating C-k. For example, C-u 2 C-k removes two lines and their newline characters; simply typing C-k twice obviously does not yield this result.
Yank (Paste)
The action of reinserting killed text is called “yanking”. (It’s like violently pulling back something that someone took away from you.) You can yank where you deleted the text, or somewhere else, and you can yank the same text multiple times to get multiple copies. Many other editors call killing and yanking “cut” and “paste”.
Yank the most recently killed text
C-y
yanking
Yank previously killed text
M-y
Requires pressing C-y first, then M-y
Undo
C-/
or C-_ or C-x u
Search
Emacs can search forward or backward for a string (“string” refers to a group of consecutive characters).
The search command is acommand that moves the cursor: after a successful search, the cursor will stay 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 hit, press C-s again. If the target cannot be found, Emacs will beep to tell you the search has failed. During the whole process, 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-%
During replacement, press y to confirm the replacement, n to skip the replacement at this point, and ! to replace all
File
Find a file
C-x C-f
Save the file
C-x C-s
This command stores the text in Emacs into a file. When saving for the first time, Emacs renames the original file for backup. The renaming rule is usually to add a “~” character after the original file name. This feature can be turned off.
Buffer
Emacs places each file being edited in a place called a “buffer”. Every time it finds a file, Emacs creates a buffer for it internally. You can list all current buffers with the following command:
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 before, using C-x C-f is one way. But there is an even simpler way, which is to use C-x b. With this command, you must type the name of the buffer.
In most cases, a buffer has the same name as its corresponding file (excluding the directory name), but this is not absolute. 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 the “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 and saved it as a file.
“Messages" 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 does not save the previous file for you. The changes to the first file still exist only in Emacs, that is, in its corresponding buffer. Moreover, 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 saving it with C-x C-s is too troublesome. You need a more convenient method, and Emacs has already prepared it for you:
C-x s Save multiple buffers
C-x s will find all buffers that have been modified but not yet saved, and then ask you one by one: Do you want to save it?
Command set extension
Emacs commands are like stars in the sky, too numerous to count. It is obviously impossible to map them all to CONTROL and META key combinations. Emacs solves this problem with eXtend commands, which come in two styles:
12 | C-x 字符扩展。 C-x 之后输入另一个字符或者组合键。 M-x 命令名扩展。M-x 之后输入一个命令名 |
Sort lines
M-x sort-lines
Word count
Count whole buffer
M-x count-words
Count selected region
M-x count-words-region
Auto-save
If you have modified a file, but your computer crashes before you can save it, your changes are likely to be lost. To prevent such misfortune from happening, Emacs periodically writes the file being edited to an “auto-save” file. The auto-save file’s name has a “#” character at the beginning and end, for example, if the file you are editing is called “hello.c”, its auto-save file is called “#hello.c#”. This file will be deleted by Emacs after a normal save.
So, if unfortunately it really happens, you can calmly open the original file (note: not the auto-save file) and then type M-x recover-file
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 some information about the state of Emacs and 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 text. If it is at the beginning of the file, --Top-- is displayed instead of --00%–; if it is at the end of the file, --Bot-- is displayed. If the file is small enough to be displayed entirely on one screen, the status bar will show --All–.
- The “L” and the number following it give the line number where the cursor is located.
- The asterisk (*) at the very beginning indicates that you have made changes to the text. A file that has just been opened certainly hasn’t been modified, so the status bar shows a dash (-) instead of an asterisk.
- The content in 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 all extended commands that can be started with M-x; M-x fundamental-mode will switch to Fundamental mode.
M-x text-mode
Use C-h m to view the documentation for the current major mode, and type C-x 1 to close the documentation pane.
- The reason major modes are called “major” modes is that “minor modes” also exist. Minor modes do not replace major modes, but provide auxiliary functions. Each minor mode can be turned on and off independently, regardless of other minor modes or the major mode. So you can use no minor modes, just one, or multiple minor modes at the same time.
- There is a minor mode called Auto Fill that is very useful, especially when editing natural language text. With Auto Fill enabled, Emacs will automatically wrap the line for you when you type past the line boundary.
- Use M-x auto-fill-mode
to enable Auto Fill mode. Use this command again, and Auto Fill mode will be disabled. In other words, if Auto Fill mode is not enabled, this command will enable it; if it is already enabled, this command will disable it. So we say this command can be used to “toggle” the mode. - The line boundary is usually set to 70 characters [referring to English characters here], and you can reset it using the C-x f command with a numeric argument.
- If you make some 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 Current Buffer
C-x k
Batch Buffer Management
C-x C-b ;; Enter Buffer list
- d ;; Mark for deletion
- u ;; Unmark current line
- U ;; Unmark all
- x ;; Execute operations
- ? ;; View key bindings help
Multiple Panes (Split Screen)
Split 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 screen vertically
C-x 3
Split screen horizontally
Adjust split screen width
Increase height C-x ^
Increase/decrease width C-x
Move the cursor to the other 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 the other pane. [For example, translation and proofreading are very suitable for this method.]
(In the upper pane) type C-x 1 to close the lower pane
Efficiency is very low when there are many split screens. ace-window allows quick jumps 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 file in 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 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 and then a key combination, Emacs will give a brief description of the command.
Type C-h c C-p.
The displayed message should look 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 completed by a function, so the function name itself can be regarded 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 opens a new Emacs window to display the function’s name and its documentation. After reading it, you can close this help window with C-x 1. Of course, you don’t need to do this immediately; you can certainly do something else in the editing window first, and then close the help window.
C-h f describes a function. You need to enter the function name.
Type 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 variable name.
C-h a Command Apropos. Enter a keyword and Emacs will list all commands whose names contain this keyword. All these commands can be invoked 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 window, which includes all commands with “file” in their names. You can see key bindings like “C-x C-f” displayed next to command names like “find-file”.
C-h i reads the manual (commonly known as Info). This command opens a special buffer called “info”, where you can read the usage manuals of packages installed on the system. To read the Emacs manual, type m emacs
