Library Technology – Reviews, Tips, Giveaways, Freeware

Linux tips: Customizing Emacs using the .emacs file – Part III

Posted In Linux - By Techtiplib on Monday, July 2nd, 2012 With No Comments »

This is the 3rd article in the series Using Emacs. If you have followed the initial articles in this series, then you must be knowing how to use many of the features of Emacs. Emacs can be customized as much as one can possibly imagine (or rather it can be customized to an extent you couldn’t possibly imagine) . You can make Emacs respond to over 1000 different commands of yours. The power of Emacs becomes evident when you have used it for over a year and then you move to some other editor…you would sorely miss all the customizations that you have made in Emacs.

So let me explain how to make the customizations. Unlike normal commands in Emacs, all the customization is done outside Emacs. That means all the functionality that you would like to have in Emacs has to be set before you start Emacs. These customizations are stored in a file called .emacs which exists in your home directory.

Assuming your home directory is /home/david the exact location of the Emacs customization file would be /home/david/.emacs . Note the . (period) before the filename makes this file a hidden file. Use a ‘ ls -al ‘ to see this file in the directory listing. All your customizations would be placed inside this single file. When Emacs starts, it would look into this file and load all your customizations.

Believe me, once you start with this you would get addicted and you would keep on customizing Emacs to such an extent that you would no longer be satisfied with any other editor. Instead of the minimum 10 Tips per article, this time I have only 8. But they are excellent enough to make any beginner happy.

Note : For Tip No. 1 – Tip No. 26 refer to the other Articles in the Using Emacs Series.

Open your .emacs file using any text editor. Enter all the lines below as they are. The lines that begin with a ‘ ;; ‘ are comments and need not be included in your .emacs file. I have them in mine for clarity sake so that whenever I edit my .emacs I can clearly understand which command stands for what.

Tip No. 27 : Making <Delete> key work properly
In case this one was not already present in your .emacs file and you have problems with deleting text using the <Delete> key on your keyboard, then add the following lines.

;; Set up the keyboard so the <delete> key on both the regular keyboard
;; and the keypad delete the character under the cursor and to the right
;; under X, instead of the default, backspace behavior.
(global-set-key [delete] ‘delete-char)

Tip No. 28 : Using <Home> & <End> keys to move to beginning & end of lines
In case you do not like the default behavior of the <Home> and <End> keys where they take you to the beginning and end of the buffer (If you are used to the Windows behavior where they take you to the beginning and the end of the lines) then add the following. Now you shall get the keys to respond as desired.

;; [Home] & [End] key should take you to beginning and end of lines..
(global-set-key [home] ‘beginning-of-line)
(global-set-key [end] ‘end-of-line)

Tip No. 29 : Display Current Time in Emacs
This is a simple and excellent thing to do. This simply displays the current time in status bar at the bottom of the screen. When you are coding day 24-7 and lose track of time, this is the best way to remind yourself of it 😉

;; displays the time in the status bar
(display-time)

Tip No. 30 : Stop scrolling of screen at end of file
In case you have noticed, when you open a file (say it contains 20 lines), even when you reach the end of the file (the 20th line) , Emacs continues scrolling and keeps adding empty lines at the end as long as you press the <Down Arrow> Key. To avoid this and make Emacs stop the scrolling of the screen as soon as you reach the last line, add the following to your .emacs file.

;; Emacs will not automatically add new lines
(setq next-line-add-newlines nil)

Tip No. 31 : Syntax highlighting
This is probably the feature that newcomers love the most. You have heard of this and you want to get it working as soon as you can.. here it is.. this would enable coloring of the text as per the mode that Emacs works in. Thus in C mode you would get all the reserved words to be highlighted. Besides in C mode you would see a lot of other things (such as comments, etc.) getting various colors. The highlighting of the syntax makes the code easier to understand.

;; syntax highlighting by default
(global-font-lock-mode 1)

Tip No. 32 : Change the irritating yes/no questions
If you have used Emacs for even a couple of days you must be already irritated by the questions that Emacs asks to which the answer has to be a ‘yes‘ or a ‘no‘. Sometimes it asks a ‘y‘ or a ‘n‘ , but many times you are required to type the entire words ‘yes‘ or ‘no‘ . Adding the following line would change this default behavior and make Emacs get rid of the ‘ yes/no ‘ question permanently. Now you would only be asked the ‘ y/n ‘ question and you could simply take the required action by a single keystroke.

;; Changes all yes/no questions to y/n type
(fset ‘yes-or-no-p ‘y-or-n-p)

Tip No. 33 : Change nature of scrolling
In case you are not happy with the way Emacs scrolls through a buffer (since when it reaches the end of the screen, the buffer moves half screen upwards and the new lines appear at the middle of the screen), you could make Emacs scroll a line at a time. Thus as you reach the end of screen the buffer would move only one line upwards and continue in the same fashion rather than the abrupt 1/2 screen movement.

;; Scroll down with the cursor,move down the buffer one
;; line at a time, instead of in larger amounts.
(setq scroll-step 1)

Tip No. 34 : Stop making those ~ backups
Emacs would make a backup file whenever it edits any file. Thus you would always find a file with a similar name but ending with a ‘~‘ in the same directory. In case you edit a.txt there would be a a.txt~ in the same directory. To stop this behavior add the following lines.

;; do not make backup files
(setq make-backup-files nil)

That’s all for now. Watch out for the article in which I shall be discussing customizations for C programmers using Emacs. I could have added them here, but I decided to list them in a separate article.

Via codecoffee.com

More contents in: , ,

About - Hey, this blog belongs to me! I am the founder of TechTipLib and managing editor right now. And I love to hear what do you think about this article, leave comment below! Thank you so much...