Why install the Emacs text editor in your server when you have
vi? Both of those latter options are good alternatives, and they are very lightweight, while Emacs comes with a lot of extra content you may never use. In this article, you will take a look at some of the powerful file-editing, fixing, and auto saving features tha come pre-packaged with Emacs, to make your system administrative duties easier to manage. This article will focus primarily on file recovery features.
- Emacs Auto Save Features
- Auto Revert
- Revert a File (Revert Buffer)
- Fixing Typos While Editing
Emacs Auto Save Features
The good thing about auto save is that it’s automatic. Auto save features are enabled in Emacs by default, and they are customizable. More than that, the auto save functionality behaves in interesting ways. It can see the kind of edits you are making and change what it is saving.
How Auto Save Works
As you edit a file, Emacs saves a backup copy appended (and prepended) with
# marks. For example, the auto save version of “file” will look like this:
So if your server were to crash in the course of editing your file, you can recover your work from the auto save file. Emacs will even warn you when auto save data is present alongside your file, and it will prompt you to restore the file (if you desire).
Intelligent Auto Saving
Auto save works differently depending on what kinds of edits you make. Normally, auto save will save the content at regular intervals, so you don’t lose the work you just did. However, if you delete a large portion of text in the buffer auto save will disable itself temporarily. This way, you can more easily restore what was lost during your edits.
How To Customize Auto Saving
So now that you understand a little more about how auto saving works, you may be wondering, “How can I change how often auto save runs?”
Auto save works based on character count. Emacs will watch how many characters you’ve typed and save at a regular interval, controlled by the variable called
auto-save-interval, naturally. The default value for
auto-save-variable is set at 300 characters. You can customize this variable in your configuration file by adding the line:
(setq auto-save-interval <number-of-characters>)
Or, for example, 500 characters will look like this:
(setq auto-save-interval 500)
You can also customize this value using the “Custom” system:
M-x customize-variable <RET> auto-save-interval
Set the value to whatever character count you want, above 20, and make sure to click Apply and save when finished.
Have you ever edited a file without realizing that it had changed on the disk while you were visiting it? This can happen if you ever viewing log files or virtually any file that can be edited by another program. This is why Emacs offers you “Auto Revert” mode.
With “Auto Revert” mode enabled (it’s not enabled by default), a file will automatically update, while you are visiting, if the file changes on disk.
You can enable
auto-revert-mode with M-x:
If you “tailing” a log, and you know that changes will be added to the bottom of the file, you are best advised to use
auto-revert-tail-mode instead of regular
auto-revert-mode. The latter will generate a message when the file changes, while tail mode will update more efficiently.
Revert a File (Revert Buffer)
If you have made edits to a file and want to return to the unsaved state there’s no need to close Emacs without saving and reopen it. You can simply “revert” the buffer to what is saved on disk. Of course, be careful when using this feature, as any time you ignore changes you’ve made you may lose data you wanted to keep. There is no key binding for the
revert-buffer function, but like all functions you can run it with M-x:
If a file has unsaved changes they will be wiped out and the buffer will be reverted to the state it was at when you opened it.
Fixing Typos While Editing
In the course of your system maintenance, the best-case scenario is being able to fix a mistake without resorting to a full restoration from an auto save file. Below you will find a few different editor functions you can run to fix typos or undo changes on the spot.
How do you do a basic “undo” in Emacs? A quick way to undo a change, or multiple changes as far back as memory holds, you can press C-/ key repeatedly to move back in edits. This is effectively the officially “undo” button in Emacs.
Kill a Line (Delete a Whole Line)
Often, you may want to get rid of an entire line of a file without interfering with other lines. Instead of just pressing backspace over and over again, you can use the C-k key. It’s important to be accurate when making sweeping changes like deleting a whole line of your file. In some files, extra white space or comment lines can cause serious failures. This is why the “kill-visual-line” function is so helpful; it obliterates a line, even if the line visually wraps around your screen.
Well done, you now know more about editing, auto saving, and reverting files you are working on with Emacs.