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.

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 –

emacs --version

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 –

emacs

or if you want to open a file using emacs then use –

emacs testfile

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.

emacs -nw

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

C-@ means use Ctrl+Shift+@

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.

 

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 C-@ 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).

Deleting text

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-/
  • C-_
  • 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

  1. forward search
  2. 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

 Conclusion

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.

man emacs

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.