📜 ⬆️ ⬇️

GNU Emacs. An article that I never found ...

Good day, Reader!

In this article I want to tell you in detail about setting up the GNU Emacs text editor.

The GNU Emacs operating system is a programmable text editor for programmers written in a programmable programming language.
')
The Emacs extension uses the Lisp language dialect - Emacs Lisp .
Strictly speaking, Emacs is not just a text editor. Emacs is an interpreter of the Emacs Lisp language, the designer of a text editor, sharpened just for you. A small part of the program is implemented in C (about 30% is responsible for basic interaction with the OS, input-output, window drawing), and all the main functionality is in Emacs Lisp (hereinafter, elisp). It is this architecture that distinguishes GNU Emacs from other professional text editors — it is indecently expandable.

The functionality of this editor is so huge and diverse that it is extremely difficult for a beginner to get used to it. What is worth only the process of adjustment - it may take years, or the whole life . That is why I decided to write this article - a detailed guide to the initial configuration of Emacs, for those who:


It is worth saying that various ambitious projects related to the development of “text editors of the 21st century”, the killers of Emacs and / or Vim replacements, are increasingly appearing in the modern IT industry.


Well, what to say ... Good luck to them in this difficult work. And we, perhaps, will be engaged in the GNU Emacs setup.


Yes. You are not mistaken. The film "Tron: Heritage." GNU Emacs is used there as well. Who would have thought…

I forgot to warn you: I write in Common Lisp (another Lisp dialect), so some of the material will be about how to turn Emacs into a full-fledged IDE with maps and girls for this programming language. A good example, by the way ...

So let's go!

Installation


For MS Windows:


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


For Mac OS X:


At the time of this writing, the latest version of the editor is Emacs-24.4 . So take it for Mac OS X or MS Windows. For GNU / Linux, I advise you to use the version that is presented in the standard repositories of your distribution.

Customization


There are at least four ways to configure Emacs:


We're not looking for easy ways! Let's write the configuration file in the elisp language!
No sooner said than done!

Configuration file .emacs


After you have successfully installed GNU Emacs on your computer, you must create a file called .emacs and write the basic settings in it.
Notation used in the article (repetition is the mother of the doctrine):


So, run Emacs. Using the Cx Cf key combination, create a new .emacs file and start writing in it. Do not be offended, but I will not go into the syntax of the elisp language - this will turn the article into a monster. In the end, just give links to the necessary resources.

First of all, let us tell Emacs what operating system it was running on. To do this, we write two functions on elisp that will help us with this:

 ;; 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 conditions for branch operators, we can set up a cross-platform configuration file for Emacs (the result of our labors will be an .emacs file that works fine on MS Windows and on GNU / Linux distributions. On Mac OS X, it did not check ).

IDE for Common Lisp


To turn Emacs into a complete development environment for Common Lisp, we need two packages:


If you are an MS Windows user and suddenly write to Common Lisp, then you need:


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

 sudo aptitude install slime sbcl 

Let's go further


If you are a lucky Mac OS X user or GNU / Linux distribution, then Emacs is useful to run as a server:

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

Next, let us indicate to Emacs the ways in which it can find installed add-ons (in particular, the Slime and SBCL packages):

 ;; 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")) ;; 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-slime-path "/usr/share/common-lisp/source/slime/") (setq unix-init-ac-dict-path "~/.emacs.d/plugins/auto-complete/dict")) 

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

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

My favorite dired-mode . Configure it:

 ;; Dired (require 'dired) (setq dired-recursive-deletes 'top) ;;      ... 

Now you can run dired-mode with the Cx d key combination. To delete a folder in dired-mode, move the cursor to that folder, press d , then x . To remove the mark for deletion from the folder, press u .

A wonderful way to "jump" on the definitions of functions for almost all programming languages ​​- Imenu . Suppose you have a file with a program for 100,500 lines with a bunch of functions. No problem! Press F6 and in the minibuffer we enter part of the name of the function we are looking for and add TAB 'ohm. Press Enter - and we are on the definition of the desired function:

 ;; Imenu (require 'imenu) (setq imenu-auto-rescan t) ;;       (setq imenu-use-popup-menu nil) ;;  Imenu    (global-set-key (kbd "<f6>") 'imenu) ;;  Imenu  F6 

We write the name of the open buffer in the header of the window:

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

Remember that we have defined the ways in which Emacs is looking for add-ons and external programs? Let them "walk" along these paths (where are the add-ons) at launch:

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

It’s not forgotten that Emacs provides you with an excellent environment for plain / text notes (organizer), reference information, project management, knowledge base organization, etc. - org-mode ? Configure:

 ;; Org-mode settings (require 'org) ;;  org-mode (global-set-key "\C-ca" 'org-agenda) ;;      (global-set-key "\C-cb" 'org-iswitchb) ;;  org-mode (global-set-key "\C-cl" 'org-store-link) (add-to-list 'auto-mode-alist '("\\.org$" . Org-mode)) ;;  *.org   org-mode 

Bring asceticism beauty - remove welcome screens at startup:

 ;; Inhibit startup/splash screen (setq inhibit-splash-screen t) (setq ingibit-startup-message t) ;;      Ch Ca 

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

 ;; Show-paren-mode settings (show-paren-mode t) ;;     {},[],() (setq show-paren-style 'expression) ;;     {},[],() 

In new versions of Emacs, electic-mod 's have been introduced. The first one automatically indents (it works badly), the second one closes brackets, quotes, etc. Turning off the first ( Python programmers will understand me ... ) and turn on the second:

 ;; Electric-modes settings (electric-pair-mode 1) ;;  {},[],()      (electric-indent-mode -1) ;;   electric-indent-mod' (default in Emacs-24.4) 

Want to be able to delete selected text when typing on top? You are welcome:

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

We remove the extra: any menu, scroll-bar 's, tool-bar ' s, etc .:

 ;; Disable GUI components (tooltip-mode -1) (menu-bar-mode -1) ;;    (tool-bar-mode -1) ;;  tool-bar (scroll-bar-mode -1) ;;    (blink-cursor-mode -1) ;;    (setq use-dialog-box nil) ;;      -    (setq redisplay-dont-pause t) ;;    (setq ring-bell-function 'ignore) ;;    

No automatic saves and backups! Hardcore only:

 ;; Disable backup/autosave files (setq make-backup-files nil) (setq auto-save-default nil) (setq auto-save-list-file-name nil) ;;   ...   -  nil  t 

The most painful and difficult place to set up is encodings:

 ;; Coding-system settings (set-language-environment 'UTF-8) (if (system-is-linux) ;;  GNU/Linux  utf-8,  MS Windows - windows-1251 (progn (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))) 

Include line numbering:

 ;; Linum plugin (require 'linum) ;;  Linum (line-number-mode t) ;;     mode-line (global-linum-mode t) ;;       (column-number-mode t) ;;     mode-line (setq linum-format " %d") ;;     

We continue to induce beauty:

 ;; Fringe settings (fringe-mode '(8 . 0)) ;;     (setq-default indicate-empty-lines t) ;;           (setq-default indicate-buffer-boundaries 'left) ;;    ;; Display file size/time in mode-line (setq display-time-24hr-format t) ;; 24-    mode-line (display-time-mode t) ;;    mode-line (size-indication-mode t) ;;    %- 

Automatic transfer of long lines:

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

Determine the size of the window with Emacs at startup:

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

Interactive search and open files? You are welcome:

 ;; 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:

 ;; Buffer Selection and ibuffer settings (require 'bs) (require 'ibuffer) (defalias 'list-buffers 'ibuffer) ;;      Cx Cb (global-set-key (kbd "<f2>") 'bs-show) ;;  buffer selection  F2 


Color schemes. How without them? For this:


 ;; 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:

 ;; Syntax highlighting (require 'font-lock) (global-font-lock-mode t) ;;    Emacs-22.  ... (setq font-lock-maximum-decoration t) 

Indent settings:

 ;; Indent settings (setq-default indent-tabs-mode nil) ;;     TAB' (setq-default tab-width 4) ;;   - 4   (setq-default c-basic-offset 4) (setq-default standart-indent 4) ;;    - 4   (setq-default lisp-body-indent 4) ;;  Lisp-  4   (global-set-key (kbd "RET") 'newline-and-indent) ;;   Enter      (setq lisp-indent-function 'common-lisp-indent-function) 

Smooth scrolling:

 ;; Scrolling settings (setq scroll-step 1) ;; -  1  (setq scroll-margin 10) ;;   /    10   /  (setq scroll-conservatively 10000) 

Shorten messages in the minibuffer:

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

Common with the OS clipboard:

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

Settings for blank lines at the end of the buffer:

 ;; End of file newlines (setq require-final-newline t) ;;          (setq next-line-add-newlines nil) ;;         ;;   

Highlight search results:

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

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

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

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

 ;; Delete trailing whitespaces, format buffer and untabify when save buffer (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) 

CEDET package - working with C / C ++ / Java (great article by Alex Ott 'a on CEDET):

 ;; CEDET settings (require 'cedet) ;;  ""  CEDET.  ... (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) 

Auto-complete input. For this:


 ;; 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) ;;    CEDET (add-to-list 'ac-sources 'ac-source-variables) ;;   (add-to-list 'ac-sources 'ac-source-functions) ;;    (add-to-list 'ac-sources 'ac-source-dictionary) ;;       (add-to-list 'ac-sources 'ac-source-words-in-all-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))) 

Set up the environment for Common Lisp - Slime :

 ;; 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))) ;;    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))) 

Configure Bookmark - bookmarks that help you quickly navigate through the text:

 ;; Bookmark settings (require 'bookmark) (setq bookmark-save-flag t) ;;      (when (file-exists-p (concat user-emacs-directory "bookmarks")) (bookmark-load bookmark-default-file t)) ;;        (global-set-key (kbd "<f3>") 'bookmark-set) ;;    F3 (global-set-key (kbd "<f4>") 'bookmark-jump) ;;     F4 (global-set-key (kbd "<f5>") 'bookmark-bmenu-list) ;;    (setq bookmark-default-file (concat user-emacs-directory "bookmarks")) ;;     bookmarks  .emacs.d 

Actually, everything! You can click Cx Cs and save the .emacs file. Where to put the .emacs file and the .emacs.d folder (if you use the paths from my .emacs ):

MS Windows:


GNU / Linux:


My .emacs can be downloaded from my page on GitHub .

My Emacs:

image

useful links


Many useful articles on GNU Emacs on Habrahabr. There is also a series of great screencasts on YouTube about Emacs , published by Dmitry Bushenko:



Screencast Series (in English) Emacs Rocks.

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

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

In order not to disregard the users of another editor - Vim, here is the link to my .vimrc on GitHub. There everything is described in detail (if anything, I can write an article on Vim ...).

I look forward to your comments, dear readers. I hope you found something useful / new for yourself.

May Emacs be with you ...

Thanks for attention.

Source: https://habr.com/ru/post/248663/


All Articles