FUNDAMENTALS A Complete Guide for Beginners
Emacs (elaborated as Editor Macros) is a powerful free text editor offered by GNU. More than a text editor, it provides additional features of being an IDE for code development, managing productivity tasks, version controlling, etc. After a successful use of Emacs, a graceful exiting is required. Therefore, this article will discuss 4 different methods to show how to exit Emacs in Linux– another fantastic feature of Emacs. Moreover, it will display how to exit Emacs with or without saving files.
1. Using Terminal
Whether a user opens Emacs using the commands emacs
or emacs file_name
(to open file_name in Emacs), it takes control of the terminal session. Therefore, the terminal does not display the shell prompt in the window while Emacs is active: Now, you can terminate the shell session by pressing CTRL+C on the terminal. This, in turn, exits the Emacs editor immediately. However, first, you have to minimize Emacs: Now, pressing CTRL+C will make Emacs exit immediately:
Note: Using the above technique after editing a file will automatically save the file before exiting Emacs.
2. Using Keyboard Shortcut “CTRL+X CTRL+C”
There are numerous keyboard shortcuts in Emacs (also called keyboard bindings). Keyboard bindings are a combination of key sequences associated with different functions within Emacs. For instance, the keystroke combination of CTRL+X and then CTRL+C allows for a seamless exit from Emacs.
Now, remember, Emacs will prompt whether to save the file (if the user edits an extant file) if there’s an attempt to exit by pressing CTRL+X CTRL+C:The above image states that there’s a prompt to the user to save the file since there’s been an attempt to add the lines “done..”. Pressing y immediately saves and exits the file. However, pressing n prompts another message like below:Try by yourself to see how things can typically go in exiting Emacs using keyboard bindings (CTRL+X CTRL+C).
Note: To learn more about Emacs keyboard shortcuts, read this article: Emacs Cheat Sheet [Free PDF Download]
3. Using Keyboard Shortcut “M+X kill-emacs”
Another keyboard shortcut to exit Emacs is M+X kill-emacs. Here, M is known as the meta key in Emacs (ESC or ALT). So, after pressing “M+X”, type kill-emacs (built-in function to terminate Emacs) when prompted to kill the execution of the Emacs process. Then Emacs will immediately exit without saving the file. Look at the below image for a better understanding:Here, I have opened a file gotham.txt in Emacs using emacs gotham.txt
. Then I added a line. After that, I pressed M+X and typed kill-emacs to terminate and exit Emacs without saving the added line.
Opening the file again using the command emacs gotham.txt
provides a practical glimpse:
4. Using GUI
To use GUI (Graphical User Interface) is the easiest and beginner-friendly way to exit Emacs. This visually intuitive environment lets users exit Emacs (with or without saving the file) seamlessly without using the terminal, commands, and rigorous keyboard bindings.
So, to exit Emacs using GUI, just go to the end of the file menu and click Quit:
Choose whatever suits your preference when prompted to save the contents or not like the following image for the successful exit:However, to save the file before exiting Emacs, press CTRL+X and then CTRL+S. Moreover, you can save the file by clicking save from the Emacs file menu.
Additionally, you can exit the Emacs editor by clicking the cross mark (close button) at the upper right-most corner with the mouse:
Conclusion
This article has discussed 4 methods of how to exit the Emacs text editor in Linux. It has mentioned how to use the terminal and different keyboard bindings for a successful exit. Moreover, it has shed light on the visual interface methods using GUI to exit seamlessly. By reading this article, you will learn to exit Emacs gracefully, with or without saving the file contents whatever seems preferable.
People Also Ask
Can I exit Emacs without saving the file?
Yes, you can exit Emacs without saving the file. To exit Emacs without saving a file, go to the file menu bar and click Quit. It will prompt a window for confirmation. So, select Close Without Saving to exit without saving the edited contents of the file. Additionally, you can press CTRL+X CTRL+C, then type n, after that type yes, and finally hit ENTER to exit without saving the file.
How to save and quit in Emacs?
To save and quit in Emacs, press CTRL+X CTRL+C just after you have made edits to the file. This results in the saving and quitting of Emacs. Additionally, to save and quit step-by-step, first press the keyboard bindings CTRL+X CTRL+S. This will save the file if you have edited the file. finally, you press CTRL+X CTRL+C to quit Emacs.
Can I save a file in Emacs using GUI?
Yes, you can save a file in Emacs using GUI. To save a file using GUI in Emacs, just click the save button with the green down arrow icon.
How to exit Emacs from the terminal?
To exit emacs from the terminal, minimize Emacs and press CTRL+C in the terminal. This will terminate the shell session and make Emacs exit. However, this saves the newly edited file contents even after immediate exit via process termination.
What is the difference between killing Emacs and suspending Emacs?
The difference between killing Emacs and suspending Emacs is that suspending Emacs refers to stopping the text editor temporarily and editing later in the same Emacs job. On the other hand, killing Emacs is to kill the Emacs job and start working with a fresh Emacs job.
Is it possible to save the contents of a file to another file in Emacs?
Yes, it is possible to save the contents of a file to another file in Emacs. To save a file’s contents to another file, press CTRL+X CTRL+W. This will prompt you to write the content to a new file. So, just type the file name and hit ENTER. As a result, it will save the file contents of a file to the newly named file. Also, Emacs immediately opens the new file upon hitting ENTER which easily lets you verify.
How do you exit the Emacs text editor using GUI?
You exit the Emacs text editor using GUI (Graphical User Interface) by first clicking to Quit from the file menu bar. This immediately exits Emacs if you have not edited the file without prompting to save. Otherwise, a window pops up for you to act accordingly to exit Emacs with or without saving the file.
How to save a file in Emacs using the Keyboard shortcut?
To save a file in Emacs using the keyboard shortcut, press CTRL+X then CTRL+S. This will save the file contents and prompt a confirmation message.
Related Articles
<< Go Back to An Overview of Emacs Text Editor in Linux | Linux Text Editors | Learn Linux Basics