Emacs Commands Comprehensive Overview

Text editors are fundamental tools that have become immensely popular in the programming industry. Text editors are software programs that allow users to write and edit text. They are specifically designed for editing the source code of programs and are often compared with Integrated Development Environments, a similar type of tool. Text editors do not care about any formatting or other elements of style required for writing. These programs simply provide a platform where you can edit your code. Hence, text editors are extremely lightweight and use much fewer resources than IDEs.

For many developers, a good text editor is all that is needed. Because this is such a significant tool for developers, it is important to choose an editor that provides you with a good interface and with features that allow you to easily program and work efficiently.

Emacs is well known among developers as one of the top text editors. Due to its versatility and swift performance, along with an extremely detailed documentation and an easy-to-use, user-friendly interface, Emacs has made quite the name for itself in the developer community. With loads of features with so many different functionalities tucked away beneath its simple-looking design, this clearly indicates the power and elegance of Emacs.

This article provides a comprehensive overview of the commands that come along with Emacs.

Important Keys

Before looking at some of the key commands of Emacs, first we will specify some terminology that is often used by Emacs users.

The most common and important shorthand that is used almost everywhere in Emacs is the C-x command. C-x refers to Ctrl + x. Another important shorthand of Emacs is the M-x command, which is often known as meta x. Meta, or M, refers to either the Alt key or the Esc key.

Summary:

Starting Emacs

Emacs has both a Graphical User Interface (GUI) and a terminal interface. To open Emacs through the GUI, open your list of applications and search for Emacs. Once found, click on it and Emacs will start. To open Emacs from the terminal, enter the following command in the terminal:

$ emacs

To open a specific file with Emacs from the terminal, enter the following command in the terminal, replacing the term “fileName” with the name of the file you wish to open:

$ emacs fileName

Getting Help

Emacs has detailed documentation that provides excellent explanations and examples of the program’s usage. To open the online manual in Emacs, hit the keys Ctrl + h, followed by r.

If you prefer following a tutorial and its guidelines to learn about Emacs, then you may access the built-in tutorial in Emacs by clicking on Ctrl + h, followed by t.

Another great resource is the info directory, a menu that gives access to useful information, such as the online documentation of Emacs. The info directory can be opened by hitting the keys Ctrl + h, followed by i.

To get an idea of what a keystroke does, hit the keys Ctrl + h, followed by k, and enter the keystroke that you want to learn about.

Summary:

Exiting Emacs

Emacs can be exited simply by pressing the keys Ctrl + x, followed by Ctrl +c. You can suspend Emacs by hitting the keys Ctrl + z.

Basic File Commands

To open or create a file, hit the keys Ctrl + x, followed by Ctrl + f, and then enter the filename. If a file exists with that name, it will open that file. However, if a file with that name does not exist, then Emacs will create a new file with that name.

To visit another file in a new window, hit the keys Ctrl + x, followed by Ctrl + v, and enter the filename of the new file.

To save a file, use the keys Ctrl + x, followed by Ctrl + s.  You can also save this file to another filename by hitting the keys Ctrl + x, followed by Ctrl + w, and entering the new filename.

You can also insert the contents of another file inside your current file at the current cursor position by clicking on Ctrl + x, followed by i, and entering the filename of the file from which the inserted contents will be copied.

You can completely undo all changes in a file since the last save by hitting the keys Alt + x and entering the command revert-buffer.

If a file closes and you were unable to save it, then Emacs allows you to recover the text from an auto-save file by hitting the keys Alt + x and entering the command recover-file.

If you were editing several files before closing, then you can recover the whole session by using the keys Alt + x and entering the command recover-session. The following image shows the list of sessions that I can recover on my system:

Summary

Cursor Movement Commands

In Emacs, users can move around the screen using keystrokes. The advantage of these keys is that they are so much faster than their alternatives (such as using a mouse).

To move by a character, hit the keys Ctrl + f to move forward one character and Ctrl + b to move backward one character.

To move by a word, hit the keys Alt + f to move forward by one word and Alt + b to move backward by one word.

To move by a line, hit the keys Ctrl + n to move to the next line and Ctrl + p to move to the previous line.

To move by a sentence, hit the keys Alt + e to move to the next sentence and Alt + a to move to the previous sentence.

To move by a paragraph, hit the keys Alt + Shift + ] (or Alt + }) to move to the next paragraph and Alt + Shift + [ (or Alt + {) to move to the previous paragraph.

To go to the beginning of a line, hit the keys Ctrl + a and to go to the end of a line, hit the keys Ctrl + e.

To go to the beginning of a buffer, hit the keys Alt + Shift + . (or Alt + <) and to go to the end of a buffer, hit the keys Alt + Shift + . (or Alt + >).

To scroll down by one page, hit the keys Ctrl + v, and to scroll up by one page, hit the keys Alt + v.

Turn on the option to display line numbers by hitting Alt + x and entering linum-mode. Now, you can go directly to the specified line number by hitting the keys Alt + g, followed by g, and entering the line number. For example:

Editing Commands

Emacs has a diverse set of commands when it comes to editing text.  One of the most useful commands offered by Emacs is the ability to repeat a command a specified number of times. This is done by hitting the keys Ctrl + u, followed by entering the number of times the command will be repeated and then entering the command to be repeated. For example, to move to the third line below from the current cursor location, you would enter the following command:

In Emacs, the term “mark” refers to the cursor’s position. Marks are used in Emacs to select sections of the text. You can first define the mark by hitting the keys Ctrl + Space. Next, move the cursor to the endpoint of the section that you want to select. For example, in the following image, I set my mark at the location of the red square and moved my cursor to the red circle.

In Emacs, the term “mark” refers to the cursor’s position. Marks are used in Emacs to select sections of the text. You can first define the mark by hitting the keys Ctrl + Space. Next, move the cursor to the endpoint of the section that you want to select. For example, in the following image, I set my mark at the location of the red square and moved my cursor to the red circle.

You can also select the entire buffer by hitting the keys Ctrl + x, followed by h.

In Emacs, the process of copying and pasting material is referred to as “killing” and “yanking” text, respectively. Emacs contains a region called the “kill-ring,” a list containing all previously killed, or cut, blocks of text.

As mentioned above, before applying these commands, you will first need to select the region using the keys Ctrl + Space. After selecting the region, copy the selected region by hitting the keys Alt + w. For cutting or killing the text, you can either use the keys Ctrl + k to kill a particular line, or the Ctrl + w command to kill the entire selected region. Alt + d can also be used to cut only a single word.

To paste or yank the text, press the keys Ctrl + y. This pastes the last killed item from the kill ring. Emacs, however, also allows you to cycle through the kill-ring list by using Alt + y.

Emacs also allows users to cancel or abort the running or entered commands by hitting the keys Ctrl + g.

Undo and redo functions are also available in Emacs. Ctrl + / can be used to undo commands, while Ctrl + g, followed by Ctrl + /, can be used to redo commands.

To increase the text size of the screen, hit the keys Ctrl + x, followed by Ctrl + + (plus). To decrease the text size, hit the keys Ctrl + x, followed by Ctrl + – (minus).

Search and Replace Commands

Emacs users have several search and replace commands at their disposal.

Users can perform an incremental search by hitting the keys Ctrl + s. To jump from one occurrence to another, continuously type Ctrl + s.

A backward incremental search can be done by hitting the keys Ctrl + r.

Perform a search using regular expressions by hitting the keys Ctrl + Alt + s.

A query replace can be done by hitting the keys Alt + Shift + 5.

See an example asking for the word to be replaced in the image below:

See an example asking for the word to replace with in the image below:

Replace regular expressions using the keys Ctrl + Alt + Shift + 5.

Buffer Commands

In Emacs, “buffers” are referred to as the contents of the file or directory that you are currently working on. Whenever you open some file or directory, the text or the directory list inside of it is held within a buffer. So, there can be more than one buffer opened.

To see the list of buffers opened, hit the keys Ctrl + x followed by Ctrl + b.

Switch from one buffer to another by hitting the keys Ctrl + x, followed by b.

Buffers can also be killed by hitting the keys Ctrl + x, followed by k.

If you press enter without specifying any buffer, then it will kill the default buffer, which in the above case would be scratch.

Built-in Special Features

Emacs has often been compared to an operating system, described as something more than a text editor and more like a fully-fledged lisp machine. These descriptions actually suit Emacs very well, since it is like a big box that comes fully packed with tools with many different functionalities.

Let us look at some of these:

Hit Alt + x and enter the respective commands to open up these features:

Open a terminal inside Emacs by entering the command shell.

Emacs also has the gdb debugger built in, which can be opened by entering gdb.

There is also a built-in calculator, as well as a calendar, in Emacs. These features can be accessed by entering calculator or calendar, respectively.

Calculator:

Calendar:

You can even access games in Emacs, such as Tetris, which can be opened by typing their names. For example, to open Tetris, simply enter Tetris.

Command:

Result:

To check and install Emacs extensions, enter the command package-list-packages.

Summary:

Modes of Emacs

Emacs also has several modes associated with it, each of which can have their own customizations and formats.

Conclusion

Emacs is a highly powerful text editor that relies heavily on its commands and shortcuts. Once users get the hang of this, the learning curve of Emacs falls drastically. The charm of Emacs also arises from it being developed in such a way that customers can customize it and use a wide array of advanced features.



from Linux Hint https://ift.tt/32lP5bV

Post a Comment

0 Comments