Example of a. emacs configuration file

Source: Internet
Author: User
Tags autoload
FreeBSD development manual
Previous Page Chapter 2 programming tools Next Page
2.7 use Emacs as the development environment 2.7.1 Emacs

Unfortunately,UNIXUnlike other systems, the system has a type of "all you need, not more" that contains all, hugeProgramDevelopment environment.[1]However, you can build your own development environment. It may not be very beautiful or very integrated. However, you can build it as needed. It is free of charge. You will have all the source code.

The answer is Emacs. Nowadays, many people hate it and many like it. If you are one of the former, I am afraid this chapter will not interest you. In addition, you need a certain amount of memory to run emacs-I recommend 8 MB for the text interface, and at least 16 MB for the X to achieve reasonable performance.

Emacs is basically a highly configurable editor-in fact, Emacs is more like an operating system than an editor! Many developers and System Administrators spend all their time in Emacs and exit the editor only when logging out.

It is impossible to summarize all the things that Emacs can do here, but some features that developers may be interested in are listed here:

    • A powerful editor that allows you to search and replace strings and regular expressions (types. Jump to the beginning/end of the block structure, and so on.

    • Drop-down menu and online help.

    • Language-related syntax highlighting and indentation.

    • Completely configurable.

    • You can compile and Debug Programs in Emacs.

    • After a compilation error occurs, you can directly jump to the problematic line.Code.

    • Relatively friendlyInfoYou can read the GNU hypertext file. Of course, this includes Emacs documents.

    • FriendlyGDBAllows you to viewSource code.

    • You can view Usenet news and read emails while compiling the program.

There are still many ignored ones.

You can use the Emacs port on FreeBSD to install Emacs.

Once installed, you can run Emacs and enterC-h tRead the Emacs tutorial -- Hold downControl, And then pressH, ReleaseControlAnd then pressT. (Or, you can use the mouse labelHelpReselect menuEmacs tutorial).

Although Emacs has menus, it is best to learn about key combinations. It is much faster to press a series of buttons continuously during editing than to find and click the right button. In addition, when you communicate with an old Emacs user, you often encounter the following expressions:M-x replace-s RET Foo RET bar RET", So it is useful to know these things. In addition, under any circumstances, the menu of Emacs will never be able to store all the useful functions it actually has.

Fortunately, it is easy to learn key combinations. Because each item in the menu is marked with a key combination. My suggestion is to use a menu item first, for example, open a file until you understand the details and use this menu item with confidence, then try to use C-x C-f. If you have no difficulty at all, you can go to the next menu item to continue the exercise.

If you cannot remember what a special key combination can doHelpSelectDescribe keyAnd then enter the key combination-Emacs will tell you what it can do. You can also clickCommand aproposTo find a command that contains a specific word, followed by a key combination.

In addition, the expression just now means to press and holdMetaKey, pressXKey, releaseMetaKey, enterReplace-S(Replace-stringAnother feature of Emacs is the abbreviation of the command.ReturnKey, inputFoo(The string you want to replace), input bar (you want to replaceFooAnd then pressReturnKey. Emacs will search and replace as required.

You must be wonderingMetaWhat is the key. This is a lotUNIXWorkstation has special keys. Unfortunately, the PC does not have such a key. Generally, the key on the PC isALTKey (if you're not lucky, this key will be on your PCEscapeKey ).

Oh, to exit Emacs, TypeC-X c-c(Hold downControlKey, pressX, PressC, And then releaseControlKey ). If you still have an unsaved file, emacs will ask you if you want to save the file. (Ignore the common methods mentioned in the document to exit Emacs.C-z-- This key combination will put Emacs in the background, and this method is only useful on systems without a virtual console ).

2.7.2 configure Emacs

Emacs can do a lot of useful things; some are built-in, and some need to be configured.

Emacs does not use a private macro language to configure itself. Instead, it uses a lisp version specially adapted to the editor, called Emacs lisp. If you want to continue reading and want to learn a little about Common LISP, learning to use Emacs lisp is very useful. Emacs lisp has many Common Lisp features, although the former is quite small (so it is easier to master ).

The best way to learn Emacs lisp is to download Emacs tutorial.

However, emacs configuration does not require any actual lisp knowledge, because I have listed. EmacsThe example is enough for you to start your work smoothly. Copy this file to your home directory. If Emacs is already running, restart it. emacs reads the command from this file and then (hopefully) it can provide you with useful basic settings.

2.7.3 one . Emacs Configuration File example

Unfortunately, it takes a long time to explain it in detail; but there are two points worth noting.

  • To;Annotations at the beginning are ignored by Emacs.

  • In the first line-*-Emacs-lisp -*-Let's edit this in Emacs.. EmacsAnd enable the editing feature of Emacs lisp. Emacs generally tries to guess based on the file name, and it is very likely to guess the error.

  • In some modes,TabThe key is bound to an indent function. After you press the tab key, it can indent a line of code. If you wantTabInsert it as a character into your edited content and pressTabPress and hold the key at the same time.ControlKey.

  • This file supports C, C ++, Perl, lisp, and scheme syntax highlighting by identifying file name suffixes.

  • Emacs already has a predefined function calledNext-Error. In a compilation error output window, pressM-nAllows you to move from one compilation error to another. We also define a similar function,Previous-Error, This function isM-PThen, you can return to the previous compilation error. The best feature is to pressC-cThen, you can open the corresponding file according to the error and jump to the corresponding line of code.

  • We opened Emacs as the server running feature, so that when you do something outside Emacs and need to edit a file, you only need to enter

    % EmacsclientFilename

    Then you can edit the file in Emacs![2]

Example 2-1. One. EmacsConfiguration File example

;-*-Emacs-lisp-*-; this file is designed to be re-evaled; Use the variable first-time; to avoid any problems with this. (defvar first-time t "flag signifying this is the first time that. emacs has been evaled "); Meta (Global-set-key" \ m-"'set-mark-command) (Global-set-key "\ m-\ c-h" 'backward-kill-word) (Global-set-key "\ m-\ c-r" 'query-replace) (Global-set-key "\ m-R" 'replace-string) (Global-set-Key "\ M-g" 'Goto-line) (Global-set-key "\ m-h" 'help-command );; function keys (Global-set-Key [F1] 'Manual-entry) (Global-set-Key [F2] 'info) (Global-set-Key [F3] 'Repeat-complex-command) (Global-set-Key [F4] 'advertised-Undo) (Global-set-Key [F5] 'eval-current-buffer) (Global-set-Key [F6] 'buffer-menu) (Global-set-Key [F7] 'other-window) (Global-set-Key [F8] 'Find-file) (Global-set-Key [F9] 'Save-buffer) (Globa L-set-Key [F10] 'next-error) (Global-set-Key [F11] 'compile) (Global-set-Key [F12] 'grep) (Global-set-Key [C-f1] 'compile) (Global-set-Key [C-f2] 'grep) (Global-set-Key [C-f3] 'next-error) (Global-set-Key [C-f4] 'previous-error) (Global-set-Key [C-f5] 'display-faces) (Global-set-Key [C-f8] 'Dired) (Global-set-Key [C-f10] 'Kill-compilation); keypad bindings (Global-set-Key [Up] "\ c-P ") (Global-set-Key [Down] "\ C-n") (Global-set-Key [left] "\ c-B") (Global-set-Key [right] "\ c-F ") (Global-set-Key [home] "\ c-a") (Global-set-Key [end] "\ c-e ") (Global-set-Key [prior] "\ m-V") (Global-set-Key [next] "\ c-V ") (Global-set-Key [C-Up] "\ m-\ c-B ") (Global-set-Key [C-Down] "\ m-\ c-F") (Global-set-Key [C-Left] "\ m-B ") (Global-set-Key [C-right] "\ m-F") (Global-set-Key [C-Home] "\ m-<") (Global-set-Key [C-end] "\ m->") (Global-set-Key [C-Prior] "\ m-<") (Global-set-Key [C-next] "\ m-> ");; mouse (Global-set-Key [Mouse-3] 'imenu); MISC (Global-set-Key [C-Tab] "\ c-Q \ t "); control tab quotes a tab. (setq backup-by-copying-when-Mismatch T); treat 'y' or <CR> As Yes, 'n' as No. (fset 'Yes-or-no-p' y-or-n-p) (define-key query-replace-map [Return] 'act) (define-key query-replace-map [? \ C-M] 'act); load packages (require 'desktop) (require 'tar-mode );; pretty diff mode (autoload 'ediff-Buffers "ediff" "intelligent Emacs interface to diff" t) (autoload 'ediff-Files "ediff" "intelligent Emacs interface to diff" t) (autoload 'ediff-files-remote "ediff" "intelligent Emacs interface to diff") (if first-time (setq auto-mode-alist (append '(("\\. CPP $ ". c ++-mode )("\\. HPP $ ". c ++-mode )("\\. LSP $". LISP-mode )("\\. SCM $ ". scheme-mode )("\\. PL $ ". perl-mode) Auto-mode-alist )));; auto font lock mode (defvar font-lock-auto-mode-List (list 'C-mode' C ++-mode 'C ++-C-mode' emacs-lisp- mode 'lisp-mode' Perl-mode' scheme-mode) "List of modes to always start in font-lock-mode") (defvar font-lock-mode-keyword-alist '(C ++-C-mode. c-font-lock-keywords) (Perl-mode. perl-font-lock-keywords) "associations bet Ween modes and keywords ") (defun font-lock-auto-mode-select () "automatically select font-lock-mode if the current major mode is in font-lock-auto-mode-List" (if (memq major-mode font-lock-auto-mode- list) (progn (font-lock-mode t) (Global-set-Key [M-f1] 'font-lock-font-buffer); new dabbrev stuff; (require 'new-dabbrev) (setq dabbrev-always-check-other-Buffers t) (setq dabbrev-abbrev-Char-Regexp "\ s W \ | \ s _ ") (add-hook 'emacs-lisp-mode-hook' (lambda () (SET (make-local-variable 'dabbrev-case-fold-search) Nil) (SET (make-local-variable'dabbrev-case-replace) Nil ))) (add-hook 'C-mode-hook' (lambda () (SET (make-local-variable 'dabbrev-case-fold-search) nil) (SET (make-local-variable 'dabbrev-case-replace) Nil) (add-hook 'text-mode-hook' (lambda () (SET (make-local-variable 'dabbrev-case-fold-search) T) (SET (make-local-variable 'dabbrev-case-replace) T); C ++ and C mode... (defun my-C ++-mode-hook () (setq tab-width 4) (define-key C ++-mode-map "\ c-M" 'reindent-then-newline-and-indent) (define-key C ++-mode-map "\ c-ce" 'C-comment-edit) (setq C ++-auto-hungry-Initial-state 'None) (setq C ++-delete-function 'backward-delete-Char) (setq C ++-tab-always-indent t) (setq C-indent-Level 4) (setq C-continued-stateme NT-offset 4) (setq C ++-empty-Arglist-indent 4) (defun my-C-mode-hook () (setq tab-width 4) (define-key C-mode-map "\ c-M" 'reindent-then-newline-and-indent) (define-key C-mode-map "\ c-ce" 'C-comment-edit) (setq C-auto-hungry-Initial-state' none) (setq C-delete-function 'backward-delete-Char) (setq C-tab-always-indent t );; BSD-ish indentation style (setq C-indent-Level 4) (setq C-continued-statement-offset 4) (Setq C-brace-offset-4) (setq C-argdecl-indent 0) (setq C-label-offset-4 ));; perl mode (defun my-perl-mode-hook () (setq tab-width 4) (define-key C ++-mode-map "\ c-M" 'reindent-then-newline-and-indent) (setq Perl-indent-Level 4) (setq Perl-continued-statement-offset 4); Scheme mode... (defun my-scheme-mode-hook () (define-key scheme-mode-map "\ c-M" 'reindent-then-newline-and-indent ));; emacs-lisp mode. .. (Defun my-lisp-mode-hook () (define-key lisp-mode-map "\ c-M" 'reindent-then-newline-and-indent) (define-key lisp-mode-map "\ c-I" 'lisp-indent-line) (define-key lisp-mode-map "\ c-J" 'eval-print-last-sexp); add all of the hooks... (add-hook 'C ++-mode-hook 'My-C ++-mode-hook) (add-hook 'C-mode-hook 'my-C-mode-hook) (add-hook 'scheme-mode-hook 'My-scheme-mode-hook) (add-hook 'emacs-lisp-mode-hook' my-lisp -Mode-hook) (add-hook 'lisp-mode-hook' my-lisp-mode-hook) (add-hook 'perl-mode-hook 'My-perl-mode-hook); Complement to next-error (defun previous-error (N) "Visit previous compilation error message and corresponding source code. "(interactive" p ") (Next-error (-N); misc... (transient-mark-mode 1) (setq mark-even-if-inactive t) (setq visible-bell nil) (setq next-line-add-newlines nil) (setq compile-comma Nd "make") (setq suggest-key-bindings nil) (put 'eval-expression' disabled nil) (put 'narrow-to-region' disabled nil) (put 'set-goal-column' disabled nil) (if (> = emacs-Major-version 21) (setq show-trailing-whitespace t ));; elisp archive searching (autoload 'format-lisp-code-directory "lispdir" Nil t) (autoload 'lisp-Dir-Apropos "lispdir" Nil T) (autoload 'lisp-Dir-retrieve "lispdir" Nil t) (autoload 'lisp- Dir-verify "lispdir" Nil T); font lock mode (defun my-make-face (face color & optional bold) "Create a face from a color and optionally make it bold" (make-face) (copy-face 'default face) (set-face-foreground face color) (If bold (make-face-bold face) (if (EQ window-system 'x) (progn (my-make-face 'Blue "blue ") (My-make-face 'Red "red") (my-make-face 'green "dark green") (setq font-lock-comment-face' B Lue) (setq font-lock-string-face 'bold) (setq font-lock-type-face 'bold) (setq font-lock-keyword-face 'bold) (setq font-lock-function-name-face 'Red) (setq font-lock-doc-string-face' green) (add-hook 'Find-file-hooks' font-lock-auto-mode-select) (setq baud-rate 1000000) (Global-set-key "\ c-CMM" 'menu-bar-mode) (Global-set-key "\ c-CMS" 'scroll-bar-mode) (Global-set-Key [backspace] 'backward-delete-Char); (GL Obal-set-Key [delete] 'delete-Char) (Standard-display-European t) (Load-library "ISO-transl ")));; x11 or PC using direct screen writes (if window-system (progn; (Global-set-Key [M-f1] 'hilit-Repaint-command );; (Global-set-Key [M-f2] [? \ C-u M-f1]) (setq hilit-mode-enable-list '(not text-Mode C ++-mode emacs-lisp-mode lisp-modescheme-mode) hilit-auto-highlight nilhilit-auto-rehighlight 'visiblehilit-inhibit-hooks nilhilit-inhibit-rebinding t) (require 'hilit19) (require 'paren )) (setq baud-rate 2400); for slow serial connections); tty type terminal (if (and (not window-System) (Not (equal system-type 'Ms-DoS) (progn (if first-time (Progn (keyboard-translate? \ C-h? \ C -?) (Keyboard-translate? \ C -?? \ C-h); under Unix (if (not (equal system-type 'Ms-DoS )) (progn (if first-time (server-Start ))));; add any face changes here (add-hook 'TERM-setup-hook 'My-term-setup-hook) (defun my-term-setup-hook () (If (EQ window-system 'pc) (progn; (set-face-Background 'default "red "))));; restore the "desktop"-do this as late as possible (if first-time (progn (desktop-load-default) (desktop-read )));; indicate that this file has been read at least once (setq first-time nil); no need to debug anything now (setq debug-on-error nil );; all done (message "All done, % S % s" (User-login-name )". ")
2.7.4 extend the range of languages supported by Emacs

Now, if you just want to use. EmacsIt is easy to program with a set language (C, C ++, Perl, lisp, and scheme. However, if there is a new saying, "whizbang", and there are many exciting features, what will happen?

The first thing to do is to find out whether any file can tell Emacs about the language. This type of file usually uses. ElEnding with "Emacs lisp. For example, if whizbang is a port of FreeBSD, we can use the following command to locate these files:

 
% Find/usr/ports/lang/whizbang-name "*. El"-print

Then install these files to the system-level lisp directory of Emacs. In FreeBSD 2.1.0-release, this directory is/Usr/local/share/Emacs/Site-lisp.

For example, if the output of the positioning command is

 
/Usr/ports/lang/whizbang/work/MISC/whizbang. El

We can execute

 
# CP/usr/ports/lang/whizbang/work/MISC/whizbang. El/usr/local/share/Emacs/Site-lisp

Next, we need to determine what suffix the source file of whizbang end. We assume that all these source files are. WizEnd. We need. EmacsAdd one to enable EmacsWhizbang. El.

In. Emacs.Auto-mode-alist entryTo add a line for whizbang, for example:

 
... ("\\. LSP $ ". LISP-mode )("\\. wiz $ ". whizbang-mode )("\\. SCM $ ". scheme-mode )...

This means that when you edit. WizEmacs automatically entersWhizbang-Mode.

As shown below, you will findFont-lock-auto-mode-listThis one. AddWhizbang-Mode

 
;; Auto font lock mode (defvar font-lock-auto-mode-List (list 'C-mode' C ++-mode 'C ++-C-mode' emacs-lisp- mode 'whizbang-mode' lisp-mode'perl-mode' scheme-mode) "List of modes to always start in font-lock-mode ")

This means that when you edit. WizEmacs will automatically open the fileFont-lock-Mode(That is, syntax highlighting ).

This is all necessary steps. If you open. WizYou can addWhizbang-mode hook(ViewMy-scheme-mode-hookAddAuto-indentAs an example ).

Remarks
[1]

The ports collection contains some powerful and free ides, such as kdevelop.

[2]

Many Emacs users put theirEditorSet the environment variableEmacsclientTherefore, each time they need to edit a file, the above actions will be executed.

Previous Page Start page Next Page
Debugging Level 1 Additional reading

This document and other documents can be downloaded from here: ftp://ftp.FreeBSD.org/pub/FreeBSD/doc.

If there is a problem with FreeBSD, please read the document first, if the problem cannot be resolved and then contact <questions@FreeBSD.org>.
Please contact <doc@FreeBSD.org>.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.