Tips For Linux

>> Using Emacs - Part II

This is the 2nd article in the Emacs series. I expect this series to continue over atleast 10 articles since there is so much about Emacs that one can discuss. The 1st article explained the basics of Emacs. You were shown how to open, close, save files and a few other tricks as well. This time I shall show you how to do much more. You would be taught effective navigation skills which would help you play around with files in a very fast and efficient manner.

I have decided to keep the no. of tips in each article in this series within the 10-15 range. A few of the tips below are a must for everyone and a few wont be really useful for beginners, but would definitely help sometime in future.

IMPORTANT : The notation used for the various keystrokes are as described below. Use this table to figure out what to press in case you cant figure out the notation used in any of the articles in this series.

This means press Ctrl button and without leaving that button press Y button
This means press Ctrl button and then without leaving that button press Y button and leave Y button and then press the K button. (Remember Ctrl has to be pressed throughout)
<Ctrl>-K + K
This means press Ctrl button and then without leaving that button press K button. Then leave both these buttons and then press the K button.

To view Tips No.1 - No.14 , refer to Article No. 12

Tip No. 15 : Moving to the beginning / end of sentence
While editing text in Emacs, to go to different sections of the text quickly you could use the following key strokes.

<Alt>-A would take the cursor to the beginning of the sentence (within which your cursor is present)
<Alt>-E would take the cursor to the end of the sentence (once again the sentence within which your cursor is present)

The above 2 commands are extremely useful when using the text mode of Emacs..not so useful in any of the programming modes since the beginning and ending of sentences are not clear in programming languages.

Tip No. 16 : Moving to the beginning / end of paragraph
Using the following key strokes

<Alt>-{ would take the cursor to the beginning of the current paragraph
<Alt>-} would take the cursor to the end of the current paragraph

Tip No. 17 : Moving to the beginning / end of buffer

<Alt>-< would move to the beginning of the currently open buffer (Note use the Alt key and the < key)
<Alt>-> would move to the end of the currently open buffer (Note use the Alt key and the > key)

Tip No. 18 : Deleting words
To delete words you could use the <Delete> key on the keyboard. But in case your keyboard doesn't have a <Delete> key, in case it is some really old model or in case your <Delete> key doesn't work, then you could use the following key strokes

<Ctrl>-D would delete a letter at the current cursor position
<Alt>-D would delete the part of the word from the current cursor position to the end of the word

This command may not work as you expect, since the characters that signify the end of a word depend on the mode that Emacs is currently in (Text Mode, HTML Mode, C Mode, etc.) Thus in Text mode a space may signify the end of a word but in C mode even a ' ( ' or a ' ) ' may indicate the end of a word.

To delete a complete word move the cursor to the first character of the word and then press <Alt>-D

Tip No. 19 : To yank previously deleted texts / Cycle through deleted texts
You must be knowing that to yank previously deleted text you have to press <Ctrl>-Y. But it is also possible to yank even the previously deleted text (the one deleted before the most recent one also)

Press <Ctrl>-Y once to yank the latest deleted stuff and then type <Alt>-Y repeatedly to yank older and older text that you had killed. The deleted text won't be all pasted again, but as you press <Alt>-Y the yanked text would be continuously replaced with the older killed text. So its not like in case you press <Alt>-Y five times would be pasting the last five killed texts. You would actually be pasting the fifth last killed text. If you do not press <Ctrl>-Y first Emacs would warn you that the last command was not a Yank command.

Remember that this command wouldn't work as expected every time. What text is yanked depends on how you killed the text. Whether you deleted a word or a sentence or many sentences consecutively. So I suggest you get familiar with this command yourself rather than me trying to explain all the possibilities. Use it a couple of times and you will easily get the hang of it.

Tip No. 20 : To save the current buffer as a new file
In order to Save the contents of the currently open buffer as a new file or overwrite another existing file.

<Ctrl>-XW would prompt for a new file name. You could enter an existing filename or a new filename. This command is basically equivalent to the Save-As command that exists in other editors.

Tip No. 21 : Replace contents of current buffer with a new file
To replace the current buffer with a new file use the following keystrokes
<Ctrl>-XV would prompt for a filename that you would like to open instead of the current buffer. Remember that this closes the current buffer and then opens the new file in a new buffer. It helps incase you know that you have finished work with a particular file, you could carry out the 2 tasks of closing the current file and opening a new file with a single command only.

Besides the fact that its a substitute for 2 commands, this feature may seem to be unnecessary when you can open as many files as you want in as many buffers as you like. But believe me as you start programming a lot you would be shifting between 2 buffers to modify files here and there. In that case, simple and fast switching between buffers is possible when few buffers are open. In that case when you have finished modifying a file, you would prefer replacing the buffer with a new file rather than opening a new buffer.

Tip No. 22 : Insert file into current buffer
To insert a file into the current buffer use the following
<Ctrl>-X + I would ask for a filename. On entering a filename that file would be inserted into the current buffer.
This would basically reproduce the entire contents of the given file into the current buffer at the current cursor position.

Tip No. 23 : Open a file as Read only file
To open a file in a buffer as a read only file use
You could do so in case you would prefer to not accidentally edit some file but would still like to copy some part of it into another buffer. Rather than risking opening an important file in read-write mode, you could open it in read only mode.

Tip No. 24 : Cancel any command midway
In case you have already typed half of a command and then you realize that it is the wrong command, you may want to cancel that half written command. You cannot continue with the new command directly since half of the previous command has already been typed and Emacs assumes that whatever you type would be a part of that command itself. To cancel any half written command use

The string Quit would be displayed in the minibuffer indicating that the half typed command was canceled. You can now start typing the new command.

Tip No. 25 : How to Undo your actions
To undo any action that you have performed in Emacs use the following keystrokes
<Ctrl>-X + U

The above keystrokes would undo the last action that you performed. You could alternatively use Ctrl+ _ (to do this remember to press the Ctrl and Shift and the minus sign button together) to undo the last action.

Note : The last action need not be the last keystroke as such. I mean Emacs has a tendency to group similar consecutive keystrokes. Thus in case you typed 10 letters consecutively and then press <Ctrl>-X + U , all the 10 letters would disappear and not the 10th letter only. Since the typing of 10 letters consecutively without any other action in between is treated as one action only by Emacs.

Tip No. 26 : Jump to a particular line directly
To go to a particular line within the current buffer directly without scrolling to that location use
<Alt>-X and then the type the string goto-line followed by <Enter> then the line no. such as 30
Basically type this
<Alt>-X goto-line <Enter> 30

When you press <Enter> Emacs would display the message GOTO LINE NO in the minibuffer. Then type 30 and press <Enter> once again. Your cursor would be placed on line no. 30 in the current buffer.

That's it for this article. A total of 12 tips which are quite useful. Yeah I know none of them were really impressive as such, but the next article will definitely show you some kewl tricks with Emacs.

Remember that learning Emacs is a lengthy process and don't try to rush through it. As you code more and more, you would automatically find the need of more and more functionality. At that time go through these tips again and you will find them to be useful.

Tips For Linux © All Copyrights Reserved