Emacs

Reload the Current File in Emacs

When working with an Emacs file, you can make changes and revert to the previous state. Such a situation is what reloading a file means. It allows you to discard all changes you made in your buffer and have the initial state of the buffer. The reloaded file will open the version that is stored in your discard.

Reloading a file is a neat way of undoing all the changes you made to your file, and Emacs offers different ways of reloading the current file. This post covers two methods on how to reload the current file. First, we will talk about a revert method. Then, we will create a shortcut for implementing the “reload” command. Let’s get started!

Two Methods to Reload the Current File in Emacs

Emacs treats the reloading of the current file as a request to discard all unsaved changes. Doing so retrieves the version of the file that is stored on the disk and all the changes will be discarded.

Opening a file in Emacs implies loading it into an Emacs buffer. When you alter the file, the changes won’t affect the version that is stored on the disk until you save the file.

However, if you change your mind, you can always reload the current file to discard the changes and use the version that is stored on the disk. Emacs will prompt you when reloading the file whether to save or discard the changes.

Method 1: Using the Reload Command

Emacs has the “revert-buffer” command in which you run whenever you want to reload a file. For this example, we will work with the following file. We loaded it into Emacs and added one line:


Suppose we want to discard the changes and remove the added line. Reloading the file will do the trick. Press “M-x'” (Alt + x) on your Emacs and type “revert-buffer” in the command prompt.


Once you press the RET/Enter keyboard key, the command will execute and you will be prompted to confirm that you want to revert the buffer which discards all the unsaved changes. To reload the file, type “yes” and press the RET key.


Immediately after doing it, the file will be reloaded and all the unsaved changes we had will be discarded. We now have the file that was stored on the disk, and it doesn’t contain the last line that we added earlier. That’s how you quickly discard the changes in your Emacs file.


Method 2: Binding the Reload Command

Instead of typing the “revert-buffer” command whenever you want to reload the current file, you can bind the command to a key. That way, pressing the binded key does the same task as the “revert-buffer” command.

The first step is to open up your Emacs config file. The config file can be “.emacs” or “.emacs.d/init.el”.

Once you open it, we must bind the key to the “reload” command. Use the “global-set-key” attribute and specify what key to bind and to what command. We are using the “f8” key for this case and our bind statement is as follows:


Save the configuration file by pressing “c-x c-s”. To apply our changes, restart your Emacs or reopen it.

Now, we go back to our initial file that we loaded into an Emacs buffer. Instead of typing the “revert-buffer” command like in the first method, we only press the “f8” key here. Doing so brings up a prompt to confirm that we want to reload the file and discard all the changes.


Once you confirm that you want to reload the file, all the changes will be discarded, and you will have the file version that was stored on the disk. That’s how you reload the current file in Emacs.

Conclusion

Reloading a file in Emacs implies reverting to the version of the file that is stored on the disk. It’s a way of discarding all the unsaved changes on the file that is loaded into Emacs. You can reload a file in two methods. The first method is using the “revert-buffer” command and confirming the prompt. The second method is binding the “revert-buffer” command to a key and pressing the key whenever you want to reload the current file. Both options are discussed in this post.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.