[zhuan]Simple Emacs Configuration
Simple Emacs Configuration
C source code indent
(setq-default c-basic-offset 4)
Change Major mode
M-x obj-mode / M-x c-mode / M-x java-mode
Using Abbreviations
Emacs can automatically correct your spelling mistake as you type (such as correcting "thier" with "their"), or expand your own abbreviations for full word (such as replacing "Indie" with "Independent"). Emacs can do this when you enable the "Abbrev" minor mode.
Add the following code to your ~/.emacs file to enable the Abbrev minor mode, to load in abbreviations from ~/.abbrev_defs and to save changes you make to the abbreviations table when you exit Emacs.
(setq-default abbrev-mode t)
(read-abbrev-file "~/.abbrev_defs")
(setq save-abbrevs t)
To display a list of the current abbreviations Emacs uses, enter the command list-abbrevs.
Highlight Current Line
To make Emacs highlight the line the curosr is currently on, add the following to your ~/.emacs :
;; In every buffer, the line which contains the cursor will be fully
;; highlighted
(global-hl-line-mode 1)
Set Indent Size
To set the standard indent size to some value other than default add the following to your ~/.emacs :
Line-by-Line Scrolling
By default Emacs will scroll the buffer by several lines whenever the cursor goes above or below the current view. The cursor is also returned to the middle-line of the current view.
This can be confusing to work with since the cursor appears to jump around. If you prefer to have the cursor remain at the top or bottom of the screen as scrolling takes place then use:
;; This makes the buffer scroll by only a single line when the up or
;; down cursor keys push the cursor (tool-bar-mode) outside the
;; buffer. The standard emacs behaviour is to reposition the cursor in
;; the center of the screen, but this can make the scrolling confusing
(setq scroll-step 1)
Turn Off Tab Character
To stop Emacs from entering the tab character into your files (when you press the "tab" key) add the following to your ~/.emacs :
;;
;; Emacs normally uses both tabs and spaces to indent lines. If you
;; prefer, all indentation can be made from spaces only. To request this,
;; set `indent-tabs-mode' to `nil'. This is a per-buffer variable;
;; altering the variable affects only the current buffer, but it can be
;; disabled for all buffers.
;;
;; Use (setq ...) to set value locally to a buffer
;; Use (setq-default ...) to set value globally
;;
Enable Wheel-Mouse Scrolling
By default Emacs does not respond to actions of a scroll button on a wheel mouse; however, it can be made to do so with a simple configuration entry:
(mouse-wheel-mode t)
Prevent Backup File Creation
By default Emacs will automatically create backups of your open files (these are the files with the ~ character appended to the filename). Add the following to your ~/.emacs to prevent these backup files from being created :
Saving Backup Files to a Specific Directory
Backup files can occassionally be usful, so rather than completely disabelling them, Emacs can be configured to place them in a specified directory. Do this by adding the following to your ~/.emacs files:
;; Enable backup files.
Enable Line and Column Numbering
Emacs can display the current line and column number on which the cursor currently resides. The numbers appear in the mode-line :
;; Show line-number in the mode line
Set Fill Column
The fill column influences how Emacs justifies paragraphs. For best results choose a value less than 80:
;; Enable backup files.
Enable Auto Fill mode
Auto fill is useful when editing text files. Lines are automatically wrapped when the cursor goes beyond the column limit :
;; Auto-fill-mode the the automatic wrapping of lines and insertion of
;; newlines when the cursor goes over the column limit.
;; This should actually turn on auto-fill-mode by default in all major
;; modes. The other way to do this is to turn on the fill for specific modes
;; via hooks.
Treat New Buffers as Text
Specify that new buffers should be treated as text files:
Set Basic Colours
Emacs does allow the various colours it uses for highlighting code to be configured by the user. However a quick way to set the basic colours used f or all buffers is:
(set-mouse-color "goldenrod")
(set-backgroud-color "#E3EDCD")---青草绿
Delete the Current Line
In order to provide Emacs with a key for deleting the current line an appropriate delete-line function has to be first defined, and then a key-sequence binding defined to invoke it :
;; First define a variable which will store the previous column position
(defvar previous-column nil "Save the column position")
;; character is deleted. The column which the cursor was positioned at is then
;; restored. Because the kill-line function is used, the contents deleted can
;; be later restored by usibackward-delete-char-untabifyng the yank commands.
(defun nuke-line()
"Kill an entire line, including the trailing newline character"
(interactive)
;; natural feel to the deletion
(setq previous-column (current-column))
(end-of-line)
;; kill-line. All that happens in this case is that the new-line character
;; is deleted.
(if (= (current-column) 0)
(delete-char 1)
;; in length. First remove the line by moving to its start and then
;; killing, followed by deletion of the newline character, and then
;; finally restoration of the column position.
(progn
(beginning-of-line)
(kill-line)
(delete-char 1)
(move-to-column previous-column))))
(global-set-key [f8] 'nuke-line)
;; Note that emacs chooses, by default, the filename
;; "~/.abbrev_defs", so don't try to be too clever
;; by changing its name