How To Install And Use GNU Emacs On Linux?
The Emacs or GNU emacs is one of the oldest and feature-rich text editor. Initially, it was developed for the Unix-like operating systems but later released for Windows and such other platforms.
Emacs is known for extensibility and customizability which means you can customize and extend the text editor to use it as Integrated Development Environment (IDE) for different programming languages such as C, Java, Python, etc.
This article explains how to install and use the emacs text editor on Linux using the terminal.
- 1 How to install Emacs on Linux
- 2 How to start Emacs text editor
- 3 Basic commands used with emacs
- 4 Conclusion
How to install Emacs on Linux
Ubuntu and other Debian based systems use the apt tool to install, update, and remove software packages in a system.
To install the emacs on Debian/Ubuntu/Linux Mint use the following commands –
First, update the OS package repository by using –
sudo apt update
and then use –
sudo apt install emacs -y
If you are using an rpm-based Linux distribution then you can use the given command to install emacs on it.
Use the following command to install emacs on RHEL/CentOS/Fedora –
sudo yum install emacs
After the installation, you can verify it by using –
How to start Emacs text editor
You can start editing in the emacs text editor in two modes GUI and command line or text mode. In GUI mode a separate emacs window will open while in text mode editor opens inside the terminal.
Launch emacs in GUI mode
Emacs will by default launch in GUI mode if you use the following command –
or if you want to open a file using emacs then use –
If the testfile exists in the system the emacs window will open with its content otherwise it will start with an empty editing buffer and waits for you to start typing.
Working in GUI mode is very similar to working with other text editors. It provides a menu you can navigate among them to perform a task.
Launch emacs in text mode
To open emacs in text mode use
-nw (no window) option with emacs command in the terminal.
or use the following command if you want to open a file –
emacs -nw testfile
Basic commands used with emacs
Emacs also uses the different combination of keys and commands for creating and editing or navigating into a file, Unlike vi text editor you need not switch between different modes. While working in text mode you may need to use command for working efficiently.
So here we will discuss some basic commands used in emacs.
The abbreviation used in this tutorial
The emacs command generally uses control key (Ctrl or CTL) and meta keys (labeled as Alt or Edit) with some other keys. Rather than using the full name, we will use them in the short form for example –
C-x means use Ctrl + x
C-space means press Ctrl + space keys
E-w means press Esc + w keys
M-k means use meta key i.e. Alt+k
Save file & exit from the editor
After creating or modifying a document in the editor you would want to save the modification and exit from the editor.
To save a file press C-x and then C-s (Ctrl+x and then Ctrl+s). Or when you press C-x and then C-c this will prompt you to save or discard saving the content of the file.
As prompted in the last line in the blue text, press y to save, n to discard, or q for quitting from the editor. Next, it can again prompt you for the confirmation enter yes or no and then press return key.
Moving cursor around the text
You can use the following combination of keys to move around the text in emacs.
|C-f||moving one character forward|
|C-b||moving one character back|
|C-n||moving to next line|
|C-p||moving to previous line|
|C-a||moving to the beginning of line|
|C-e||moving to the end of a line|
|M-f||moving forward one word|
|M-b||moving backward one word|
|M-a||moving to previous sentence|
|M-e||moving to next sentence|
|M-v||moving to previous screen|
|C-v||moving to next screen|
|M-<||Moving to the starting of the file|
|M->||Moving to the end of the file|
Copy, cut and paste
To copy or cut the text you need to mark the text region. First, move the cursor to the starting of the text and then press C-Space or [email protected] to set the mark. Now move the cursor to the end of the text and then use –
- E-w (Esc+w) for copying the marked text
- C-w for cutting the marked text
- Now move the cursor to the position where you want to paste this text and then press C-y (y stands for yank )
- To select the whole text of the main buffer press C-x h (Ctrl+x and then h).
The delete and backspace keys work as expected while editing text in emacs. You could also use the combination of the following keys to delete the text –
- To delete a word first move the cursor to the beginning of the word and then press M-d. For deleting multiple words then hold the meta key and keep pressing d this will start deleting word one by one.
- For deleting the whole line first position the cursor and then press the C-k.
- If you want to delete a sentence then press M-k after positioning the cursor at the beginning of the sentence.
Undo & redo
Undoing is available with three different combinations of keys –
- C-x u
For redoing the last undo you can use the C-g followed by C-_
Search and replace the text
Emacs provides two ways to search for the specific text in a file
- forward search
- The backward search
In the forward search, the search starts in the forward direction from the cursor position. Similarly in backward search, the search starts in the backward direction from the cursor position.
- Press the C-x for the forward search
- Press the C-r for the backward search
Look at the image given below it shows the forward search in emacs. When you press the C-x it will ask you to enter the word that is to be searched in the file.
To replace the occurrence of a specific word or sentence press the M-% (Alt+Shift+%). You will be prompted to enter the text that is to be searched, after entering the text press return key. Next, type the replacing string and press the enter. For each match, it will ask the whether you want to make the replacement you can press any one of the given –
- y to replace the current match found
- n to skip to next match
- q to exit without any replacements
- ! for global replace without any prompts
To know more about the usage of commands you could view the GNU emacs or the manual page of emacs by running the following command in your terminal.
You might also like
This post was last updated on August 3rd, 2020 at 03:09 pmIn this article, we will learn to add or remove users and giving them required permissions in Linux or
This post was last updated on August 2nd, 2020 at 08:38 pmData compression is a process of reducing the size of a data file. A compressed file is easier and
In a computer, MBR and GPT are two ways of storing the partitioning information on a disk. The MBR is a legacy while GPT is a newer disk partitioning style