Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Emacs editing environment, Part 6: Customize your Emacs world

Get going with this famous open source editor

Michael Stutz, Author, Consultant
Photo of Michael Stutz
Michael Stutz is author of The Linux Cookbook, which he also designed and typeset using only open source software. His research interests include digital publishing and the future of the book. He has used various UNIX operating systems for 20 years.

Summary:  This tutorial, the sixth in a series, walks you through some of the useful ways you can customize and configure the Emacs environment. Learn how to change everything about the Emacs environment to your liking, from the behavior of minor modes to the default key bindings. Along the way, figure out how to set variables, make all your customizations automatic with a startup file, save and recall any window and frame customizations that you make, and use the easy customizer that comes built into Emacs.

View more content in this series

Date:  02 Oct 2007
Level:  Intermediate

Activity:  23267 views
Comments:  

Set Emacs variables

The quickest and easiest way to customize some aspect of your Emacs world is to set an Emacs variable. Variables affect the way various aspects of Emacs work; they are Emacs Lisp symbols, are referenced by their name, and usually have a default value. Some variables take numeric values or strings, whereas others are true or false: If a value of a variable is set to nil, its value is false; the value t denotes true.

You can change the value of any variable at any time in an Emacs session. Do this to set a local variable for a particular buffer, to make a change to a global variable that applies only to the current session, or to test the behaviors of different values.

For instance, in the second tutorial in this series (see Resources), you learned about the fill-paragraph function (bound to M-q), which fills the paragraph at point. The width this function fills to is contained in the fill-column variable and, like any Emacs variable, you can display its contents and change it.

To begin, start Emacs with a buffer corresponding to a new file:

$ emacs bee
                

Now, insert a line of text in your new buffer:

The busy bee has no time for sorrow.

Fill the current paragraph (the line you just inserted) by typing M-q to run the fill-paragraph function.

When you type this, it looks like nothing happened—that's because the default width, in characters, that Emacs uses for filling text in a buffer is longer than the paragraph you just typed. You can verify that by looking at the contents of the fill-width variable.

Display the value of a variable

Use the describe-variable function to get the value of a variable. When you run it, Emacs opens a new window that contains the given current value of the variable. A description of the variable, if available, is also provided. This function is bound to the C-h v keystroke.

Try getting the value of the fill-width variable by typing C-h v fill-width.

Once you do this, you should see a new window in your Emacs session that contains the text of Listing 1.


Listing 1. Output of Emacs describe-variable function showing the value of the fill-width variable
                    

fill-column's value is 70

Documentation:
*Column beyond which automatic line-wrapping should happen.
Automatically becomes buffer-local when set in any fashion.

You can customize this variable.

From this output, you can tell that the fill-paragraph function won't break to a new line until 70 characters are reached, so the short paragraph you've typed isn't long enough to wrap.

Set a variable

Like any Emacs variable, you can change the value of fill-column. Use the set-variable function to do that—when you run it, it asks for the name of a variable and the value you want to give it.

Try setting the value of fill-column to 10, as in Listing 2.


Listing 2. Change the value of the fill-column variable using the Emacs set-variable function
                    
                    M-x set-variable Enter
                    
Set variable: fill-column Enter
                    
Set fill-column to value: 10 Enter
                    
                

Now, fill the paragraph again: Make sure the cursor is at the end of the line you just typed, and then type M-q to run the fill-paragraph function.

Notice how the paragraph is filled so that no lines are longer than 10 characters, as in Listing 3.


Listing 3. Sample paragraph wrapped after the fill-width variable is set to 10
                    
The busy
bee has no
time for
sorrow.

Try it again after setting the value of fill-column to 4, using the process given in Listing 2.

After filling the paragraph with another M-q, you see that the words have wrapped differently, as in Listing 4.


Listing 4. Sample paragraph wrapped after the fill-width variable is set to 4
                    

The
busy
bee
has
no
time
for
sorrow.

Because fill-column is a local variable, any changes you make to it apply only to the current buffer. Every buffer in an Emacs session has its own fill-column variable; each of these can be set to a different value.

Try selecting all the text you've inserted and pasting it to a new buffer:

C-space M-< M-w C-x b cee Enter C-y

Once you type this, you should have a copy of the newly filled paragraph from the bee buffer copied over to a new, unsaved buffer named cee.

Type M-q to fill it. It fills back to a single line and looks like the single line you typed at the beginning.

By asking Emacs to show you the contents of the variables, you can also verify that the values of fill-column are different in both buffers.

Type C-h v fill-column Enter to show the value of this buffer's fill-column variable, which should be 70.

After you do this, ask Emacs to show you the value of the bee buffer's fill-column variable: type C-x b Enter C-h v fill-column Enter to verify that it's still set to 4. As shown in Figure 1, the *Help* buffer that's displayed also tells you that the local value of this variable differs from its global value.


Figure 1. Showing the value of the Emacs fill-column variable after it was changed locally inside a buffer
Showing the value of the Emacs fill-column variable after it was changed locally inside a buffer

Use and know common Emacs variables

Emacs has many variables that affect and alter its behavior. Table 1 lists some of the more commonly modified variables and describes their function.


Table 1. Summary of common Emacs variables
VariableDescription
auto-mode-alist If set to nil, automatic selection of major mode based on file name extension is turned off. Its default value is a list of file name extensions and corresponding modes.
auto-save-default If not set to nil, Emacs automatically saves a changed buffer to its corresponding file at preset intervals. Its default value is t.
auto-save-interval Contains the number of character changes after which Auto-save mode, if true, is invoked; the default value is 300.
calendar-latitude Contains the latitude value for the location of the user's workstation, in degrees; the default value is nil.
calendar-longitude Contains the longitude value for the location of the user's workstation, in degrees; the default value is nil.
calendar-location-name Contains the value for the location name (such as city, state, and country) for the location of the user's workstation; the default value is nil.
colon-double-space If not set to nil, commands for filling text insert two spaces after a colon instead of one. The default value is nil.
command-line-args Contains the list of arguments used in the command line that executed the current Emacs session.
command-line-default-directory Contains the path name of the directory from which the current Emacs session was executed.
compare-ignore-case If not set to nil, Emacs ignores differences in uppercase and lowercase letters when running the compare-windows function, as described in fifth installment of this series (see Resources). The default value is nil.
confirm-kill-emacs If set to nil, Emacs doesn't ask for a confirmation when exiting; otherwise, the exit verification might be customized as an Emacs Lisp function such as y-or-n-p (see the Make short answers possible section). The default value is nil.
default-justification Sets the default justification style. The value can be one of left, right, center, full, or none. The default value is left.
default-major-mode Selects the default major mode for new files or buffers. The default value is fundamental-mode.
display-time-24hr-format If set to t, Emacs displays time in 24-hour military format, instead of the standard 12-hour format with AM or PM suffix. The default value is nil.
display-time-day-and-date If not set to nil, Emacs displays time with the current day of the week, current month, and current day of the month, instead of just the hour and minute. The default value is nil.
fill-column Contains the number for the column on each line where text begins to be filled to the next line. The default value is 70.
initial-major-mode Specifies the major mode to use for the *scratch* buffer on startup. The default value is lisp-interaction-mode.
inverse-video If not set to nil, Emacs inverts the display colors, if possible. The default value is nil.
kill-ring Contains the contents of the Emacs kill ring, as described in third installment of this series (see Resources).
kill-ring-max Sets the number of allowable entries in the Emacs kill ring. The default value is 60.
kill-whole-line If not set to nil, the kill-line function (bound to C-k) kills the current line and its trailing newline character, if the function is executed at the very beginning of the line. The default value is nil.
make-backup-files If not set to nil, Emacs saves a backup of a buffer before any changes are made to a file of the same name but with a tilde character (~) appended to the end.
mark-ring Contains the contents of the current mark ring of the buffer, as described in third installment of this series (see Resources).
mark-ring-max Contains the number of allowable entries in the mark ring. The default value is 16.
mouse-avoidance-mode Contains a value describing the type of mouse-avoidance mode, as described in fifth installment of this series (see Resources). The default value is nil.
next-line-add-newline If not set to nil, Emacs adds a new line whenever the down arrow is pressed at the end of the buffer. The default value is nil (in more recent versions of Emacs).
scroll-bar-mode Contains the value for the side of the Emacs frame on which to place the scroll bar: right or left. If set to nil, the scroll bar is turned off. The default value is left.
scroll-step Contains the number of lines to move through the buffer lines with the scroll-down and scroll-up functions (which are bound to the PgDn and PgUp keys by default). If set to 0, Emacs centers point in the middle of the window when scrolling.
show-trailing-whitespace If not set to nil, Emacs makes any whitespace at the end of lines in the current buffer visible. The default value is nil.
visible-bell If not set to nil, Emacs makes the frame blink instead of ringing the audible system bell. The default value is nil.
x-cut-buffer-max Sets the maximum number of characters from the kill ring that are also stored in the X Window System cut buffer. The default value is 20000.

2 of 10 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=AIX and UNIX, Open source
ArticleID=259318
TutorialTitle=Emacs editing environment, Part 6: Customize your Emacs world
publish-date=10022007
author1-email=stutz@dsl.org
author1-email-cc=mmccrary@us.ibm.com