GNU Emacs. The article, which I never found...

Good day, Reader!

In this article I want to talk in detail about configuring the text editor GNU Emacs.

Operating system the GNU Emacs programmable text editor for programmers, written in a programmable programming language.

To extend Emacs uses a dialect of LispEmacs Lisp.
Strictly speaking, Emacs can not be called just a text editor. Emacs is an interpreter Emacs Lisp, a designer, a text editor, sharpened it for You. A small part of a program implemented in C (about 30% — responsible for basic interaction with OS, I / o, rendering Windows), and all basic functionality in Emacs Lisp (hereinafter, elisp). Such architecture features of GNU Emacs from other professional text editors — it's ridiculously expandable.

The functionality of this editor is so huge and varied, that the novice in this environment is extremely difficult to get comfortable. Why should only the setup process — it can take years or whole life. That's why I decided to write this article — a detailed guide for the initial setup of Emacs, for those who:

the
    the
  • want to start using GNU Emacs, but does not know how;
  • the
  • writes on languages support in IDE is poor or none at all;
  • the
  • writes in different languages, "the programmer-polyglot";
  • the
  • wants to have a universal development environment at work and at home, multiple computers at once;
  • the
  • wants to have a functional and powerful environment for plain/text notes (organizer), management information, project management, organization knowledge base, etc. — org-mode;
  • the
  • want to automate the process of handling large numbers of text files;
  • the
  • wants to be undemanding to hardware resources, a cross-platform environment to work with any text information;
  • the
  • wants Wednesday which without special efforts, on the fly, customize yourself, its specific purpose, to expand, independently adding a new functionality;
  • the
  • loves the versatility and expandability;
  • the
  • writing for the web, for scripting languages: Python, Ruby, Perl, etc.
  • the
  • wants to join the eternal, Emacs is one of the most long-lived Open Source projects of the community;
  • the
  • etc.

It is worth saying that in the modern IT industry increasingly be a variety of ambitious projects related to the development of "text editors XXI century", murderers substitutions Emacs and/or Vim:

the
What can I say... good Luck to them in this difficult work. And we probably will have to configure GNU Emacs.


Yes. You are not wrong. The film "Tron: Legacy". GNU Emacs used there. Who would have thought...

Forgot to tell: I write on Common Lisp (another dialect of the Lisp language), so some material will be about how to turn Emacs into a full IDE and girls for this programming language. Good example by the way...

So there you go!

the

Installation


For MS Windows:

the
    the
  • download archive official site;
  • the
  • create directory C:\emacs\ and unpack it in the downloaded archive.
  • the
  • to run C:\emacs\bin\addpm.exe (will create the item in start menu to start Emacs).

For GNU/Linux distributions (for example, deb-based distributions), there are four ways:

the
    the
  • in terminal, run the command:
    the
    sudo aptitude install emacs emacs-goodies-el
    

  • the
  • via the package Manager synaptic;
  • the
  • via the app center of the distribution;
  • the
  • collect source.

For Mac OS X:

the
At the time of this writing, the latest version of the editor — Emacs-24.4. Here is her take on Mac OS X or MS Windows. For GNU/Linux suggest to use the version which is presented in the standard repositories for Your distribution.

the

setup


There are at least four ways to configure Emacs:

the

    writing config file .emacs; the

  • in menu;
  • the
  • with the command M-x customize (C-x means Control-x, M-x means Meta-x where Meta may mean: "press Esc to release" or "press-Alt hold". (for example, Meta-x or click Esc, then x, or hold down Alt and press x);
  • steal to borrow someone else's file .emacs and begin working (not recommend).


We are not looking for easy ways! We will write a configuration file for the language elisp!
It is done!

the

configuration File .emacs


After You have successfully installed GNU Emacs on your computer, you need to create a file called .emacs and prescribe basic settings.
Symbols used in the article (repetition is the mother of learning):

the

    C-a: Ctrl-a;

    M-a: Meta-a (If You don't have the Meta keys (Alt), use the Esc key);

    C-M-a: Ctrl-Meta-a.


So, you start Emacs. With the keyboard shortcut C-x C-f create new file .emacs and begin to write. No offense, but go into the syntax of the language elisp will not — this will turn the article into a monster. In the end just give links to the required resources.

For starters, let's tell Emacs about what operating system he started. For this we will write on two elisp functions that will help us in this:

the
;; System-type definition
(defun system-is-linux()
(string-equal system-type "gnu/linux"))
(defun system-is-windows()
(string-equal system-type "windows-nt"))

Now, calling these functions as well for forks, we can customize cross-platform configuration file for Emacs (the result of our labors will file .emacs, which works fine in MS Windows and GNU/Linux distributions. On Mac OS X not checked).

the

IDE for Common Lisp


For turning Emacs into a full-fledged development environment for Common Lisp language, we need two packages:

the
    the
  • implementation of Common Lisp. I chose SBCL;
  • the
  • Slime — an Emacs mode for developing applications in the language Common Lisp.

If You are a user of MS Windows and suddenly write in Common Lisp, You will need:

the
    the
  • download SBCL;
  • the
  • set C:\sbcl\ downloaded SBCL;
  • the
  • download Slime;
  • the
  • place in the C:\slime\ downloaded Slime.

On GNU/Linux, everything is easier: run command line:

the
sudo aptitude install sbcl slime

the

Going further


If You are happy user of Mac OS X or GNU/Linux, Emacs is helpful to run as the server:

the
;; Start Emacs as a server
(when (system-is-linux)
(require 'server)
(unless (server-running-p)
(server-start))) ;; start Emacs as a server, if the OS is GNU/Linux

Next, specify Emacs the paths but will be able to find the installed add-ons (in particular, packages Slime and SBCL):

the
;; MS Windows path variable
(when (system-is-windows)
(setq win-sbcl-exe "C:/sbcl/sbcl.exe")
(setq win-init-path "C:/.emacs.d")
(setq win-init-ct-path "C:/.emacs.d/plugins/color-theme")
(setq win-init-ac-path "C:/.emacs.d/plugins/auto-complete")
(setq win-init-slime-path "C:/slime")
(setq win-init-ac-dict-path "C:/.emacs.d/plugins/auto-complete/dict"))

;; The Unix path variable
(when (system-is-linux)
(setq unix-sbcl-bin "/usr/bin/sbcl")
(setq unix-init-path "~/.emacs.d")
(setq unix-init-ct-path "~/.emacs.d/plugins/color-theme")
(setq unix-init-ac-path "~/.emacs.d/plugins/auto-complete")

(setq unix-init-ac-dict-path "~/.emacs.d/plugins/auto-complete/dict"))

Let's tell Emacs about who we are (little, decide using Emacs to send mail or to jabber'e to correspond...):

the
;; My name and e-mail adress
(setq user-full-name "%user-name%")
(setq user-mail-adress "%user-mail%")

My favorite dired-mode. Setting it up:

the
;; Dired
(require 'dired)
(setq dired-recursive-deletes 'top) ;; to be a non-empty directory delete...

You can now run dired-mode with the key combination C-x d. To delete the folder in dired-mode, hover the cursor over this folder, click d, then x. To remove from the folder, mark for deletion, click u.

A great way to jump to function definitions for almost all programming languages — Imenu. Suppose You have a file with the program 100500 lines with tons of features. Not a problem! Press F6 and minibuffer enter part of the name of the unknown function and TAB'th Supplement. Pressed Enter — here we define the required function:

the
;; Imenu
(require 'imenu)
(setq imenu-auto-rescan t) ;; automatically update the list of functions in the buffer
(setq imenu-use-popup-menu nil) ;; Imenu dialogues only minibuffer
(global-set-key (kbd "<f6>") 'imenu) ;; call Imenu F6

Write the name of an open buffer in the header of the window:

the
;; Display the name of the current buffer in the title bar
(setq frame-title-format "GNU Emacs: %b")

Remember that we defined the path in which Emacs searches for add-ons and external programs? Let "walk" along these routes (where add-on) when you run:

the
;; Load path for plugins
(if (system-is-windows)
(add-to-list 'load-path win-init-path)
(add-to-list 'load-path unix-init-path))

Have not forgotten that Emacs provides You lovely environment to plain/text notes (organizer), management information, project management, organization knowledge base, etc. — org-mode? Configure:

the
;; Org-mode settings
(require 'org) ;; Make org-mode
(global-set-key "\C-ca" 'org-agenda) ;; peredelanye keyboard combinations for interior
(global-set-key "\C-cb" 'org-iswitchb) ;; modes org-mode
(global-set-key "\C-cl" 'org-store-link)
(add-to-list 'auto-mode-alist '("\\.org$" . Org-mode)) ;; associate *.org files with org-mode

Hover austerity beauty — remove the welcome screens at startup:

the
;; Inhibit startup/splash screen
(setq inhibit-splash-screen t)
(setq ingibit-startup-message t) ;; the welcome screen you can call the combination C-h C-a

Select the expression between {},[],() when the cursor is on one of the brackets is useful for programmers:

the
;; Show-paren-mode settings
(show-paren-mode t) ;; enable highlighting of expressions between {},[],()
(setq show-paren-style 'expression) ;; highlight color expressions between {},[],()

In new versions of Emacs introduced a electic-mod's. The first of them automatically adds indentation (works very badly), the second — closes brackets, quotes, etc., Disable the first (Python programmers will understand me...) and turn on the second:

the
;; Electric-modes settings
(electric-pair-mode 1) ;; auto-close {},[],() move the cursor inside the parentheses
(electric-indent-mode -1) ;; disable electric indentation-indent-mod Ohm (default in Emacs 24.4)

I want to be able to delete selected text when typing on top? Please:

the
;; Delete selection
(delete-selection-mode t)

Remove excess: any menu, scroll-bar's tool bar's, etc.:

the
;; Disable GUI components
(tooltip-mode -1)
(menu-bar-mode -1) ;; disable the graphical menu
(tool-bar-mode -1) ;; disable tool-bar
(scroll-bar-mode -1) ;; disable scroll bar
(blink-cursor-mode -1) ;; the cursor is not blinking
(setq use-dialog-box nil) ;; no graphical dialogs and Windows - all through minibuffer
(setq redisplay-dont-pause t) ;; the best rendering buffer
(setq ring-bell-function 'ignore) ;; disable the beep

No automatic saves and backups! Only hardcore

the
;; Disable backup/autosave files
(setq make-backup-files nil)
(setq auto-save-default nil)
(setq auto-save-list-file-name nil) ;; I'm so used to... want to  include  - change nil t

The most sensitive and difficult place to set up the encoding:

the
;; Coding-system settings
(set-language-environment 'UTF-8)
(if (system-is-linux) ;; for GNU/Linux, utf-8, for MS Windows - windows-1251

(setq default-buffer-file-coding-system 'utf-8)
(setq-default coding-system-for-read 'utf-8)
(setq file-name-coding-system 'utf-8)
(set-selection-coding-system 'utf-8)
(set-keyboard-coding-system 'utf-8-unix)
(set-terminal-coding-system 'utf-8)
(prefer-coding-system 'utf-8))
(progn
(prefer-coding-system 'windows-1251)
(set-terminal-coding-system 'windows-1251)
(set-keyboard-coding-system 'windows-1251-unix)
(set-selection-coding-system 'windows-1251)
(setq file-name-coding-system 'windows-1251)
(setq-default coding-system-for-read 'windows-1251)
(setq default-buffer-file-coding-system 'windows-1251)))

Turn on the line numbering:

the
;; Linum plugin
(require 'linum) ;; call Linum
(line-number-mode t) ;; show line number in mode-line
(global-linum-mode t) ;; show line numbers in all buffers
(column-number-mode t) ;; show column number in mode-line
(setq linum-format "%d") ;; set the numbering format strings

Continue to restore the beauty:

the
;; Fringe settings
(fringe-mode '(8 . 0)) ;; organicity text left
(setq-default indicate-empty-lines t) ;; no row to select the glyphs next to the strip with the row number
(setq-default indicate-buffer-boundaries 'left) ;; indication of left

;; Display file size/time in mode-line
(setq display-time-24hr-format t) ;; 24-hour time format in mode-line
(display-time-mode t) ;; show clock in mode-line
(size-indication-mode t) ;; size of file in %Ah

Automatically wrap long lines

the
;; Line wrapping
(setq word-wrap t) ;; wrap text
(global-visual-line-mode t)

We define the window size of Emacs upon startup:

the
;; Start window size
(when (window-system)
(set-frame-size (selected-frame) 100 50))

Interactive search and discovery of files? Please:

the
;; IDO plugin
(require 'ido)
(ido-mode t)
(icomplete-mode t)
(ido-everywhere t)
(setq ido-vitrual-buffers t)
(setq ido-enable-flex-matching t)

Quick navigation between open buffers:

the
;; ibuffer Buffer Selection and settings
(require 'bs)
(require 'ibuffer)
(defalias 'list-buffers 'ibuffer) ;; separate list of buffers when you press C-x C-b
(global-set-key (kbd "<f2>") 'bs-show) ;; start of buffer selection by pressing F2


Color scheme. Without them? To do this:

the
    the
  • download the package color-theme Emacs here;
  • the
  • create directory .emacs.d/plugins/color-theme;
  • the
  • unpack there the content of the archive with the themes;
  • the
  • to locate the folder .emacs.d:
      the
    • for MS Windows in the root of C:\.emacs.d
    • the
    • to GNU/Linux in the home directory of ~/.emacs.d
  • the
  • write in our .emacs following lines:

the
;; Color-theme definition <http://www.emacswiki.org/emacs/ColorTheme>
(defun color-theme-init()
(require 'color-theme)
(color-theme-initialize)
(setq color-theme-is-global t)
(color-theme-charcoal-black))
(if (system-is-windows)
(when (file-directory-p win-init-ct-path)
(add-to-list 'load-path win-init-ct-path)
(color-theme-init))
(when (file-directory-p unix-init-ct-path)
(add-to-list 'load-path unix-init-ct-path)
(color-theme-init)))

Code highlighting:

the
;; Syntax highlighting
(require 'font-lock)
(global-font-lock-mode t) ;; the version included with Emacs 22. Just in...
(setq font-lock-maximum-decoration t)

Set the indents:

the
;; Indent settings
(setq-default indent-tabs-mode nil) ;; disable the ability to put padding TAB'om
(setq-default tab-width 4) ;; tabulation width to 4 spaces
(setq-default c-basic-offset 4)
(setq-default standard-indent 4) ;; standard indent width to 4 spaces
(setq-default lisp-body-indent 4) ;; move to Lisp expressions by 4 spaces
(global-set-key (kbd "RET") 'newline-and-indent) ;; when you press Enter move caret and make an indent
(setq lisp-indent-function 'common-lisp-indent-function)

Smooth scrolling:

the
;; Scrolling settings
(setq scroll-step 1) ;; up-down 1 line
(setq scroll-margin 10) ;; move buffer up/down when the cursor 10 steps from the top/bottom borders 
(setq scroll-conservatively 10000)

To shorten the message minibuffer:

the
;; Short messages
(defalias 'yes-or-no-p 'y-or-n-p)

In common with the OS clipboard:

the
;; Clipboard settings
(setq x-select-enable-clipboard t)

Settings blank lines at the end of the buffer:

the
;; End of file newlines
(setq require-final-newline t) ;; add new empty row to the end of the file when saving
(setq next-line-add-newlines nil) ;; don't add the new line to the end at offset 
;; cursor arrow keys

Highlight search results:
the
;; Highlight search resaults
(setq search-highlight t)
(setq query-replace-highlight t)

Move between splits using combinations of M-arrow-keys (except org-mode):

the
;; Easy transition between buffers: M-arrow-keys
(if (equal nil (equal major-mode 'org-mode))
(windmove-default-keybindings 'meta))

Remove extra spaces at end of lines, replace TAB's to spaces and align the indents when you save the buffer to the file automatically:

the
;; Delete trailing whitespace characters, format and untabify buffer save buffer when
(defun format-current-buffer()
(indent-region (point-min) (point-max)))
(defun untabify-current-buffer()
(if (not indent-tabs-mode)
(untabify (point-min) (point-max)))
nil)
(add-to-list 'write-file-functions 'format-current-buffer)
(add-to-list 'write-file-functions 'untabify-current-buffer)
(add-to-list 'write-file-functions 'delete-trailing-whitespace)

Package LIBRARY — work with C/C++/Java (a wonderful article Alex Ott'a in CEDET):

the
;; CEDET settings
(require 'cedet) ;; use the "stitched" version of CEDET. I have enough...
(add-to-list 'semantic-default-submodes 'global-semanticdb-minor-mode)
(add-to-list 'semantic-default-submodes 'global-semantic-mru-bookmark-mode)
(add-to-list 'semantic-default-submodes 'global-semantic-idle-scheduler-mode)
(add-to-list 'semantic-default-submodes 'global-semantic-highlight-func-mode)
(add-to-list 'semantic-default-submodes 'global-semantic-idle-completions-mode)
(add-to-list 'semantic-default-submodes 'global-semantic-show-parser-state-mode)
(semantic-mode t)
(global-ede-mode t)
(require 'ede/generic)
(require 'semantic/ia)
(ede-enable-generic-projects)

Autocompletion input. To do this:

the
    the
  • download the package auto-complete for Emacs here;
  • the
  • create directory .emacs.d/plugins/auto-complete;
  • the
  • unpack there the content of the archive with auto-complete;
  • the
  • to locate the folder .emacs.d:
      the
    • for MS Windows in the root of C:\.emacs.d
    • the
    • to GNU/Linux in the home directory of ~/.emacs.d
  • the
  • write in our .emacs following lines:

the
;; Auto-complete plugin <http://www.emacswiki.org/emacs/AutoComplete>
(defun ac-init()
(require 'auto-complete-config)
(ac-config-default)
(if (system-is-windows)
(add-to-list 'ac-dictionary-directories win-init-ac-dict-path)
(add-to-list 'ac-dictionary-directories unix-init-ac-dict-path))
(setq ac-auto-start t)
(setq ac-auto-show-menu t)
(global-auto-complete-mode t)
(add-to-list 'ac-modes 'lisp-mode)
(add-to-list 'ac-sources 'ac-source-semantic) ;; look for auto-completion in CEDET
(add-to-list 'ac-sources 'ac-source-variables) ;; among the variables
(add-to-list 'ac-sources 'ac-source-functions) ;; names of functions
(add-to-list 'ac-sources 'ac-source-dictionary) ;; in the folder where the edited buffer
(add-to-list 'ac-sources 'ac-source-words-in-all-buffer) ;; whole buffer
(add-to-list 'ac-sources 'ac-source-files-in-current-dir))
(if (system-is-windows)
(when (file-directory-p win-init-ac-path)
(add-to-list 'load-path win-init-ac-path)
(ac-init))
(when (file-directory-p unix-init-ac-path)
(add-to-list 'load-path unix-init-ac-path)
(ac-init)))

Will setup the environment for Common Lisp — Slime:

the
;; SLIME settings
(defun run-slime()
(require 'slime)
(require 'slime-autoloads)
(setq slime-net-coding-system 'utf-8-unix)
(slime-setup '(slime-fancy slime-asdf slime-indentation))) ;; load the main additions Slime
;;;; for MS Windows
(when (system-is-windows)
(when (and (file-exists-p win-sbcl-exe) (file-directory-p win-init-slime-path))
(setq inferior-lisp-program win-sbcl-exe)
(add-to-list 'load-path win-init-slime-path)
(run-slime)))
;;;; for GNU/Linux
(when (system-is-linux)
(when (and (file-exists-p unix-sbcl-bin) (file-directory-p unix-init-slime-path))
(setq inferior-lisp-program unix-sbcl-bin)
(add-to-list 'load-path unix-init-slime-path)
(run-slime)))

Set Bookmark — bookmarks to help you quickly move around the text:

the
;; Bookmark settings
(require 'bookmark)
(setq bookmark-save-flag t) ;; automatically save bookmarks to a file
(when (file-exists-p (concat user-emacs-directory "bookmarks"))
(bookmark-load bookmark-default-file t)) ;; try to find and open the file with bookmarks
(global-set-key (kbd "<f3>") 'bookmark-set) ;; create a bookmark for F3 
(global-set-key (kbd "<f4>") 'bookmark-jump) ;; jump to a bookmark by F4
(global-set-key (kbd "<f5>") 'bookmark-bmenu-list) ;; open a list of bookmarks
(setq bookmark-default-file (concat user-emacs-directory "bookmarks")) ;; store the bookmark in a bookmarks file in .emacs.d

In fact, everything! You can press C-x C-s and save the file .emacs. Where to put the file .emacs and the folder .emacs.d (if you use the way from my .emacs):

MS Windows:

the

    .emacs C:\Users\%username%\AppData\Roaming\ the

  • the folder .emacs.d in the root of C:\

GNU/Linux:

the

    .emacs in the home directory: /home/%username%/ the

  • the folder .emacs.d in the home directory: /home/%username%/

My .emacs download from my page on GitHub.
My Emacs:

image

the

Useful links


Many useful articles for GNU Emacs on Habrahabr. Also have a series of great screencasts on YouTube about Emacs, published by Dmitry Bushenko:

the

Series of screencasts (in English. language) Emacs Rocks.

Incredibly huge, detailed and useful article (in English. language): Sacha Chua's Emacs configuration.

A huge variety of color themes for Emacs. See here.

not to ignore the users of other editor — Vim, here is a link to my .vimrc on GitHub. There all is described (if that can get Vim to write an article...).

Looking forward to Your comments, dear readers. I hope You found something useful/new.

God be with You, Emacs...

Thank you for your attention.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Fresh hay from the cow, or 3000 icons submitted!

Knowledge base. Part 2. Freebase: make requests to the Google Knowledge Graph

Group edit the resources (documents) using MIGXDB