Ethical Hacking Institute Course in Pune-India
Extreme Hacking | Sadik Shaikh | Cyber Suraksha Abhiyan

I’ve recently started learning Emacs, and in this post, I will be describing how to edit a text file in Emacs and some mostly used shortcuts. This post is aimed for those who’ve either never used Emacs or were lazy enough to even read the introductory tutorial — just like me.

So, What is Emacs?

GNU Emacs, or generally just Emacs, is an extensible, customizable, self-documenting real-time display editor according to its man page. It’s more than just a text editor. It can be used for viewing emails, browsing the web, compiling and running programs, testing software (using gdb), and managing files. For the sake of this post, we will use it as a text editor.

Is editing that difficult in Emacs?

No, not at all. You can edit your files by only using your mouse and keyboard, without any shortcuts. Just like Notepad. Apparently, it’s not much efficient. You need shortcuts for some frequent tasks and Emacs has zillions of them.

Let’s do some editing…

Open a file in Emacs

Okay. Press Ctrl-x Ctrl-f. Then type the file name. You can see the file name as you type at the bottom line of the screen (known as minibuffer). Tab completion also works there.

Alternatively, one can also open a file using a drop down file menu, or open file icon (top left corner of Emacs frame), or simply by double-clicking the file.

Basic cursor movement

Emacs support cursor movement through arrow keys but it is always better to have your fingers on the standard position in your keyboard. Here’s a list of key bindings for basic movement around the text.

Ctrl-p: Move to the previous line

Ctrl-n: Move to next line

Ctrl-f: Move forward by one character

Ctrl-b: Move backward by one character

Ctrl-e: Move to end of line

Ctrl-a: Move to beginning of line

There are Meta cousins of most these shortcuts. Like Ctrl-f moves cursor forward by one character, Meta-f moves the cursor forward by one word. Meta is displayed as Alt on most of the keyboards.

Some terminology before next section

In Emacs, the notion of buffers is very common. In general, once you’ve opened a file, it is said to be residing in a buffer. Any changes you make to that file, exist in the buffer. Unless you’ve saved those changes, they don’t appear on the disk. Emacs support multiple buffer editing simultaneously. That is, you can load multiple files into Emacs buffers and switch between them without losing any changes you’ve made.

The notion of windows is different in Emacs than other commonly used software. In Emacs, one can split the screen in multiple windows, each having its own buffer. Note that, both windows share the same screen, same file menus, and same close and minimize buttons. In the general sense, both exist on the same window.

What other software refer to as a window, Emacs refers to them as frames. That is, each frame has their different file menus, and close buttons.

Multiple window editing in Emacs

Multiple window editing is one of the features I like the most in Emacs. There are several multi-window editing shortcuts in Emacs. I’ll try to strip that down to bare minimum. Just editing two files simultaneously.

First, open a file using Ctrl-x Ctrl-f, then file name. Then press Ctrl-x 2. This would split the window into two, one below the other. Initially, both windows display the same buffer. To switch between the windows, press Ctrl-x o. Open another file in the second window and edit both of them simultaneously. Alternatively, Emacs can display multiple windows in parallel. To do this press Ctrl-x 3.

To kill the second window, i.e display only one window on the frame, press Ctrl-X 1. When you kill a window, buffer associated with that is not destroyed. In other words, any changes you’ve made to the second window are not lost. That buffer is just hidden. To switch between buffers read the next section.

Switching between buffers

A beginner might not find the idea of multiple buffers quite intuitive. Well, I didn’t. I used to open one file, make changes to it, save it, close it, open another one. Even for reference, you need to save and close first, or open another text editor window (i.e frame in Emacs context), to view another file. Believe me, multiple buffers can make tasks much easier and efficient.

Switching between opened buffers is pretty simple in Emacs. Press Ctrl-x b, then type in the buffers name. One can use tab completion for such tasks. Alternatively, press Ctrl-x Ctrl-b. This would open another window displaying the list of buffers. To close this window, you know what to do. You might see some previously existing buffers like TUTORIAL, *Messages*, etc. Their use will be apparent when you open them.

File saving in Emacs

This is perhaps the most important task. Emacs is intelligent enough to not lose your changes when you quit without saving, or after a power failure. When you’ve not saved your changes and Emacs is closed, due to whatever reason, a new file named as #filename.txt# is created by default. This file contains all your unsaved changes. Still, it’s better to avoid taking risks when not needed, and save your files before quitting.

To save the changes made in the buffer, press Ctrl-x Ctrl-s. Alternatively, press Ctrl-x Ctrl-w, to save file with another name.

Miscellaneous:

Some more shortcuts and Emacs’ features that you might find helpful.

Cancelling a command

If you’ve stuck in the minibuffer area, due to a partially entered command or due to any other reason, press Ctrl-g to quit that operation. Alternatively, hitting the Esc key three times (Esc Esc Esc) also results in cancelling a partial operation.

Searching

To search a keyword or a phrase, press Ctrl-s and type in the keyword. The keyword as you type will be displayed in the minibuffer (bottommost line). Emacs does an incremental search for the keyword, that is, it starts searching as soon you type the first character. To find the next occurrence of the keyword, press Ctrl-s again.

Quitting at once

One can quit Emacs by hitting that close (X) button displayed at the top (wherever your DE displays that). As we’re talking only command line in this post, pressing Ctrl-x Ctrl-c does the same thing. Though it displays a warning in the minibuffer if you’ve unsaved buffers. Pressing y or n there would suffice.

Scrolling

Line scrolling is described above, i.e pressing Ctrl-n or Ctrl-p. For page scrolling, press Ctrl-v or Meta-v.

Concluding Note

I’ve tried to describe some most frequently used shortcuts and basic file editing in Emacs. What I’ve described is only a fraction of what you can do with Emacs. Still this would be enough for getting started with Emacs. I’ll be posting more about Emacs as I learn further and find features that are worth posting.

If you’ve started learning Emacs and found a liking in it, I highly recommend reading the introductory tutorial shipped with Emacs.

Emacs Cheat Sheet

Take a print out of this cheat sheet (Emacs Cheat Sheet) and try to perform operations using only key combinations. Avoid using your mouse as much as possible and you’ll see how much efficient text editing becomes.

www.extremehacking.org
Cyber Suraksha AbhiyanCEHv9, CHFI, ECSAv9, CAST, ENSA, CCNA, CCNA SECURITY,MCITP,RHCE,CHECKPOINT, ASA FIREWALL,VMWARE,CLOUD,ANDROID,IPHONE,NETWORKING HARDWARE,TRAINING INSTITUTE IN PUNECertified Ethical Hacking,Center For Advanced Security Training in India, ceh v9 course in Pune-India, ceh certification in pune-India, ceh v9 training in Pune-IndiaEthical Hacking Course in Pune-India