Emacs Master Cultivation manual

Posted by nic0las on Fri, 21 Jan 2022 19:55:45 +0100

Emacs Master Cultivation manual

Shortcut key

C-n, next line (shorthand: Nextline)
C-p, Previous line (shorthand: Previous line)
C-f, move one character Forward (shorthand: Forward)
C-b, one character backward (shorthand: backward)
C-k, delete from cursor position to end (shorthand: Kill)
C-a, back to the beginning of the line (shorthand: a is the beginning of the alphabet)
C-e, End of line (shorthand: End of line)
M - <, return to the beginning of the editing area (shorthand: <)
M - >, go to the last position in the editing area (shorthand: >)
C-v, flip down one screen
M-v, flip up one screen
Self contained documents: C-h t; Shorthand Help Tutorial
View the meaning of shortcut key: C-h k; Shorthand help keyword
View function definition and shortcut key binding: C-h f; Shorthand Help Function

First experience

Turn off the menu bar, toolbar, scroll bar, etc.:

(menu-bar-mode -1)
(tool-bar-mode -1)
(scroll-bar-mode -1)

The location of Emacs configuration files will be found in the following order:

  • ~/.emacs
  • ~/.emacs.d
  • ~/.config/emacs/init.el
    The first is a single file configuration; The second is more in line with engineering; The third applies only to versions ≥ 27. The tutorial will start with the first and gradually change to the second mode.

emacs configuration code

;;Turn off the startup interface
(setq inhibit-startup-screen t)

(setq package-archives '(
    ("melpa" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/")
    ("gnu" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")
    ("org" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/org/")))

(setq package-check-signature nil) ;Signature verification fails occasionally
 (require 'package) ;; Initialize package manager
 (unless (bound-and-true-p package--initialized)
    (package-initialize)) ;; Refresh software source index
 (unless package-archive-contents

(unless (package-installed-p 'use-package)
    (package-install 'use-package))

Managing extensions using use package

  • What is a use package

    • Simple understanding, is a macro
    • Manage plug-ins in a simpler and unified way
  • How to use it?

    • Basic format, and give an example

    ;; The most concise format
    (use-package restart-emacs)

    ;; Common formats
    (use-package smooth-scrolling
    :ensure t ; Are you sure you have installed
    :defer nil ; Do you want to delay loading
    :init (setq smooth-scrolling-margin 2) ; Initialization parameters
    :config (smooth-scrolling-mode t) ; Basic configuration parameters
    :bind ; Binding of shortcut keys
    :hook) ;hook mode binding

    ;; Recommended configurations (some from the official recommendations of use package)
    (setq use-package-always-ensure t) ; You do not need to manually add the: ensure t keyword for each package
    (setq use-package-always-defer t) ; The default is delayed loading. You don't need to add each package manually: defer t
    (setq use-package-always-demand nil)
    (setq use-package-expand-minimally t)
    (setq use-package-verbose t))

Change theme

(use-package gruvbox-theme
    :init (load-theme 'gruvbox-dark-soft t))

(use-package smart-mode-line
    (setq sml/no-confirm-load-theme t)
    (setq sml/theme 'respectful)

Engineering management configuration

  • lisp/init-elpa.el is used to store Elpa and Package initialization

  • lisp/init-package.el is used to store installed extensions

  • lisp/init-ui.el is used to store configuration related to interface topics

  • First set the loaded target directory to the load path.

    (add-to-list 'load-path
    (expand-file-name (concat user-emacs-directory "lisp")))
  • Each file exposes the name of external call through provide. For example:

    (provide 'init-ui)
  • Then in init Call through require in the El file:

    require 'init-ui
  • About custom configuration
    It is recommended to write a separate file. When it is open source, the file will not be submitted to Git. For example, custom el

    (setq custom-file
        (expand-file-name "custom.el" user-emacs-directory))
  • The following configurations will generate different configuration codes due to different operating systems, so it is necessary to judge the operating system.

    (defconst *is-mac* (eq system-type 'darwin))
    (defconst *is-linux* (eq system-type 'gnu/linux))
    (defconst *is-windows* (or (eq system-type 'ms-dos) (eq system-type 'windows-nt)))
  • Solve the jam of Emacs on Windows by modifying the font:

    (use-package emacs
      :if (display-graphic-p)
      ;; Font settings
      (if *is-windows*
          (set-face-attribute 'default nil :font "Microsoft Yahei Mono 9")
          (dolist (charset '(kana han symbol cjk-misc bopomofo))
            (set-fontset-font (frame-parameter nil 'font) charset (font-spec :family "Microsoft Yahei Mono" :size 12))))
      (set-face-attribute 'default nil :font "Source Code Pro for Powerline 11")))
  • One point start configuration:
    Set the system code to avoid random code everywhere.

    (prefer-coding-system 'utf-8)
    (set-default-coding-systems 'utf-8)
    (set-terminal-coding-system 'utf-8)
    (set-keyboard-coding-system 'utf-8)
    (setq default-buffer-file-coding-system 'utf-8)

    Set garbage collection threshold to speed up startup.

    (setq gc-cons-threshold most-positive-fixnum)
  • Test start time:
    Measurement of startup time through benchmark init package:

    (use-package benchmark-init
      :init (benchmark-init/activate)
      :hook (after-init . benchmark-init/deactivate))

Small TIPS:

  • Interrupt command input:
    C-g when entering any command, you can abandon the command and continue to type through the shortcut key at any time.

  • Replace yes/no with y/n:

    (use-package emacs :config (defalias 'yes-or-no-p 'y-or-n-p))
  • Line number configuration:

    (use-package emacs
        (setq display-line-numbers-type 'relative)
        (global-display-line-numbers-mode t))

Text editing

  • Select / copy / cut / paste for text editing:
    • Copy M-w
    • Shear C-w
    • Paste C-y;; Shorthand Yank
  • Deletion of text editing
    • Scenario 1: delete C-k from the cursor position to the end of the line

    • Scenario 2: delete the current row
      By default, there is no such shortcut key. You can install an extension, crux, which provides a series of optimization shortcut commands including this scenario.

      (use-package crux
          :bind ("C-c k" . crux-smart-kill-line))
    • Scenario 3: delete the first non empty character before / after at one time (delete continuous blank)

      (use-package hungry-delete
          :bind (("C-c DEL" . hungry-delete-backward)
                 ("C-c d" . hungry-delete-forward)))
    • Move the line / area of text editing up and down

      (use-package drag-stuff
        :bind (("<M-up>". drag-stuff-up)
               ("<M-down>" . drag-stuff-down)))
    • Enhanced search for text editing

      (use-package ivy
        :defer 1
        :hook (after-init . ivy-mode)
        (ivy-mode 1)
        (setq ivy-use-virtual-buffers t
              ivy-initial-inputs-alist nil
              ivy-count-format "%d/%d "
              enable-recursive-minibuffers t
              ivy-re-builders-alist '((t . ivy--regex-ignore-order))
        (ivy-posframe-mode 1)))
      (use-package counsel
        :after (ivy)
        :bind (("M-x" . counsel-M-x)
               ("C-x C-f" . counsel-find-file)
               ("C-c f" . counsel-recentf)
               ("C-c g" . counsel-git)))
      (use-package swiper
        :after ivy
        :bind (("C-s" . swiper)
               ("C-r" . swiper-isearch-backward))
        :config (setq swiper-action-recenter t
                      swiper-include-line-number-in-search t))
    • Automatic completion of text editing

      (use-package company
        (setq company-dabbrev-code-everywhere t
              company-dabbrev-code-modes t
              company-dabbrev-code-other-buffers 'all
              company-dabbrev-downcase nil
              company-dabbrev-ignore-case t
              company-dabbrev-other-buffers 'all
              company-require-match nil
              company-minimum-prefix-length 2
              company-show-numbers t
              company-tooltip-limit 20
              company-idle-delay 0
              company-echo-delay 0
              company-tooltip-offset-display 'scrollbar
              company-begin-commands '(self-insert-command))
        (push '(company-semantic :with company-yasnippet) company-backends)
        :hook ((after-init . global-company-mode)))
    • Sequence change of text editing
      The two characters before and after the cursor are interchanged C-t
      The two words before and after the cursor are interchanged M-t

    • Word count of text editing

      • Entire Buffer statistics:
        ;; perhaps
        M-x count-words-region
      • Selected area statistics:
        ;; Select first
        M-x count-words

Emacs common plug-ins

  • crux optimizes Emacs common operations

    (use-package crux
      :bind (("C-a" . crux-move-beginning-of-line)
             ("C-c ^" . crux-top-join-line)
             ("C-x ," . crux-find-user-init-file)
             ("C-c k" . crux-smart-kill-line)))
    • Which key shortcut
      When you press a key, it will prompt you the meaning of the next key. And wait for you to press.

      (use-package which-key
        :defer nil
        :config (which-key-mode))

Emacs window management

  • Buffer management of window management
    Buffer switching: C-x b
    Kill the current Buffer: C-x k
    Batch management Buffer:

    C-x C-b ;; get into Buffer list
    d ;; Mark deletion
    u ;; Unmark current line
    U ;; Unmark all
    x ;; Perform operation
    ? ;; View key help
  • MiniBuffer interactive optimization of window management
    The MiniBuffer displayed in the lower left corner has a large sight range, and it is more suitable to move to the central position.

    (use-package ivy-posframe
      (setq ivy-posframe-display-functions-alist
        '((swiper . ivy-posframe-display-at-frame-center)
          (complete-symbol . ivy-posframe-display-at-point)
          (counsel-M-x . ivy-posframe-display-at-frame-center)
          (counsel-find-file . ivy-posframe-display-at-frame-center)
          (ivy-switch-buffer . ivy-posframe-display-at-frame-center)
          (t . ivy-posframe-display-at-frame-center)))
  • Split screen of window management
    Turn on horizontal split screen C-x 3
    Open vertical split screen C-x 2
    Keep only the current split screen C-x 1
    Close current split screen C-x 0

  • Split screen width adjustment of window management
    Increase height C-x^
    Increase / decrease width C-X {C-X}

  • Fast switching split screen of window management
    By default, the following shortcut keys can be used to cycle and jump the window: C-x o

  • Tab tab management of window management
    C-x t 2 ;; New Tab
    1 ;; Close other tabs
    0 Close the current Tab
    b ;; Open Buffer in a new Tab

Programming language environment configuration

Language Server Protocol Support for Emacs

Languages: Some languages have to be installed manually. Others can be installed with M-xlsp-install-server.

Topics: emacs