Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Next: , Previous: Init Syntax, Up: Init File


57.6.2 Init File Examples

Here are some examples of doing certain commonly desired things with Lisp expressions:

  • Make <TAB> in C mode just insert a tab if point is in the middle of a line.
              (setq c-tab-always-indent nil)
         

    Here we have a variable whose value is normally t for `true' and the alternative is nil for `false'.

  • Make searches case sensitive by default (in all buffers that do not override this).
              (setq-default case-fold-search nil)
         

    This sets the default value, which is effective in all buffers that do not have local values for the variable. Setting case-fold-search with setq affects only the current buffer's local value, which is not what you probably want to do in an init file.

  • Specify your own email address, if Emacs can't figure it out correctly.
              (setq user-mail-address "[email protected]")
         

    Various Emacs packages that need your own email address use the value of user-mail-address.

  • Make Text mode the default mode for new buffers.
              (setq default-major-mode 'text-mode)
         

    Note that text-mode is used because it is the command for entering Text mode. The single-quote before it makes the symbol a constant; otherwise, text-mode would be treated as a variable name.

  • Set up defaults for the Latin-1 character set which supports most of the languages of Western Europe.
              (set-language-environment "Latin-1")
         
  • Turn off Line Number mode, a global minor mode.
              (line-number-mode 0)
         
  • Turn on Auto Fill mode automatically in Text mode and related modes.
              (add-hook 'text-mode-hook
                '(lambda () (auto-fill-mode 1)))
         

    This shows how to add a hook function to a normal hook variable (see Hooks). The function we supply is a list starting with lambda, with a single-quote in front of it to make it a list constant rather than an expression.

    It's beyond the scope of this manual to explain Lisp functions, but for this example it is enough to know that the effect is to execute (auto-fill-mode 1) when Text mode is entered. You can replace that with any other expression that you like, or with several expressions in a row.

    Emacs comes with a function named turn-on-auto-fill whose definition is (lambda () (auto-fill-mode 1)). Thus, a simpler way to write the above example is as follows:

              (add-hook 'text-mode-hook 'turn-on-auto-fill)
         
  • Load the installed Lisp library named foo (actually a file foo.elc or foo.el in a standard Emacs directory).
              (load "foo")
         

    When the argument to load is a relative file name, not starting with ‘/’ or ‘~’, load searches the directories in load-path (see Lisp Libraries).

  • Load the compiled Lisp file foo.elc from your home directory.
              (load "~/foo.elc")
         

    Here an absolute file name is used, so no searching is done.

  • Tell Emacs to find the definition for the function myfunction by loading a Lisp library named mypackage (i.e. a file mypackage.elc or mypackage.el):
              (autoload 'myfunction "mypackage" "Do what I say." t)
         

    Here the string "Do what I say." is the function's documentation string. You specify it in the autoload definition so it will be available for help commands even when the package is not loaded. The last argument, t, indicates that this function is interactive; that is, it can be invoked interactively by typing M-x myfunction <RET> or by binding it to a key. If the function is not interactive, omit the t or use nil.

  • Rebind the key C-x l to run the function make-symbolic-link (see Init Rebinding).
              (global-set-key "\C-xl" 'make-symbolic-link)
         

    or

              (define-key global-map "\C-xl" 'make-symbolic-link)
         

    Note once again the single-quote used to refer to the symbol make-symbolic-link instead of its value as a variable.

  • Do the same thing for Lisp mode only.
              (define-key lisp-mode-map "\C-xl" 'make-symbolic-link)
         
  • Redefine all keys which now run next-line in Fundamental mode so that they run forward-line instead.

              (substitute-key-definition 'next-line 'forward-line
                                         global-map)
         
  • Make C-x C-v undefined.
              (global-unset-key "\C-x\C-v")
         

    One reason to undefine a key is so that you can make it a prefix. Simply defining C-x C-v anything will make C-x C-v a prefix, but C-x C-v must first be freed of its usual non-prefix definition.

  • Make ‘$’ have the syntax of punctuation in Text mode. Note the use of a character constant for ‘$’.
              (modify-syntax-entry ?\$ "." text-mode-syntax-table)
         
  • Enable the use of the command narrow-to-region without confirmation.
              (put 'narrow-to-region 'disabled nil)
         
  • Adjusting the configuration to various platforms and Emacs versions.

    Users typically want Emacs to behave the same on all systems, so the same init file is right for all platforms. However, sometimes it happens that a function you use for customizing Emacs is not available on some platforms or in older Emacs versions. To deal with that situation, put the customization inside a conditional that tests whether the function or facility is available, like this:

              (if (fboundp 'blink-cursor-mode)
                  (blink-cursor-mode 0))
              
              (if (boundp 'coding-category-utf-8)
                  (set-coding-priority '(coding-category-utf-8)))
         

    You can also simply disregard the errors that occur if the function is not defined.

              (condition case ()
                  (set-face-background 'region "grey75")
                (error nil))
         

    A setq on a variable which does not exist is generally harmless, so those do not need a conditional.


 
 
  Published under the terms of the GNU General Public License Design by Interspire