惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
有赞技术团队
有赞技术团队
罗磊的独立博客
博客园 - 叶小钗
Jina AI
Jina AI
博客园 - 司徒正美
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
人人都是产品经理
人人都是产品经理
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
Microsoft Security Blog
Microsoft Security Blog
大猫的无限游戏
大猫的无限游戏
量子位
MyScale Blog
MyScale Blog
V
Visual Studio Blog
博客园 - 聂微东
The Cloudflare Blog
Engineering at Meta
Engineering at Meta
小众软件
小众软件
宝玉的分享
宝玉的分享

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Learning Emacs as A Nvimmer in One Day
2021-09-12 · via jdhao's digital space
update log

2021-11-24: Add more detailed instructions for installing doom-modeline.

I have been using Neovim for over three years and I have been constantly hearing the greatness of its rival editor Emacs. So today I am gonna to try it and see if it is truly great.

Edit: My final Emacs conf is here.

Install#

Check here: https://www.gnu.org/software/emacs/download.html

For Windows, go to this mirror site, and download emacs-27.2-x86_64.zip and extract it.

For macOS, use homebrew to install Emacs-plus1:

brew tap d12frosted/emacs-plus
brew install emacs-plus

How to Run?#

  • Windows: Go to extracted folder, run bin/runemacs.exe.
  • macOS: Run emacs from command line or search Emacs.app and run it.

Note to run emacs in maximized window, add following config to emacs config file:

(add-to-list 'initial-frame-alist '(fullscreen . maximized))

Ref:

Config file#

Emacs uses~/.emacs.d/init.el as its startup file (~/.config/emacs/init.el is also supported). Its configuration is written via a dialect of Lisp language called Emacs Lisp, or Elisp in short.

To check config file path, run C-H v user-init-file RET (RET is return key).

Reload config: note that it is not possible to entirely reload config, the best way is to restart your emacs. Open init.el, and run m-x eval-buffer.

Ref:

Install packages#

I am not a purist guy which only uses the built-in features of and editor and refuses to add plugins or packages.

Package is to Emacs what plugin is to Vim.

The built-in way#

To install packages, run M-x package-install, we are prompted to type the package name we want to install. Type the package name, and the package shall be installed.

Sometimes, due to bad network connection, emacs hangs forever when trying to connect to its package repo, and we see the following message:

Contacting host: elpa.gnu.org:443

In this case, we can change the package repo URL (add it to init.el)

;; this is just an example
(setq package-archives '(("gnu"   . "http://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")
                         ("melpa" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/")))

One disadvantage of using built-in package management is that it can not install a package automatically if its hasn’t been installed. We need to write a few lines of code in config file.

Using 3rd party package manager#

Aside from the built-in way to install packages, we can also use 3rd party package managers, for example, straight.el.

To install straight.el, just put the following script to init.el:

(defvar bootstrap-version)
(let ((bootstrap-file
       (expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
      (bootstrap-version 5))
  (unless (file-exists-p bootstrap-file)
    (with-current-buffer
        (url-retrieve-synchronously
         "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
         'silent 'inhibit-cookies)
      (goto-char (point-max))
      (eval-print-last-sexp)))
  (load bootstrap-file nil 'nomessage))

To install a package, add the following statement to init.el:

(straight-use-package 'some-package)
;; for example, to install company-mode, use the following
(straight-use-package 'company)

Straight.el will install a package if it has not been installed yet.

Color, theme, modeline#

We can surly customize the look of Emacs via handcraft, e.g., change background color and foreground color:

(add-to-list 'default-frame-alist '(foreground-color . "#E0DFDB"))
(add-to-list 'default-frame-alist '(background-color . "#102372"))

More advanced way to customize color is to use or install a color theme. Emacs comes with several built-in themes.

To find good themes, MELPA is a good place to start. A good theme package is doom-themes. It comes with a lot of themes.

To load a theme, type A-x load-theme, and then type a theme name, for example, doom-one, or we can add to config:

(load-theme 'doom-gruvbox t nil)

To show currently enabled themes, run c-h v and type custom-enabled-themes to check.

File editing#

  • Delete a line: C-S-Backspace.
  • Go to line beginning and end: C-a and c-e.
  • Yank selected text and paste: M-w and c-y.

Evil mode#

I find the editing experience in Emacs highly inferior to that of Vim. So We can use emacs-evil:

(straight-use-package 'evil)

;; enable evil mode
(evil-mode 1)

There is in fact a built-in viper-mode for Emacs, but it is not complete as compared to evil.

Ref:

Window and buffer#

  • open a horizontal split: c-x 2
  • open a vertical split: c-x 3
  • close a buffer: c-x k
  • close a window: c-x 0
  • open a file: c-x c-f
  • create a new file: same as c-x c-f
  • save a file: c-x c-s
  • quit emacs: c-x c-c
  • Open a buffer: c-x b
  • To show config file path, run M-:, then type (buffer-file-name) to show config path.

Ref:

Help system#

c-h ? gives an overview of the help system.

  • Check the value of a variable: c-h v <variable-name>
  • Check function doc: c-h f
  • Check which command a key binds to: c-h c

Ref:

Tab completion#

Add the following to your emacs conf:

(setq completion-cycle-threshold 1)

Ref:

UI#

disable toolbar, menu bar, scrollbar#

Toolbar, menu bar and scrollbar are ugly and take precious space. Let’s disable them all together:

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

Ref:

Show line number and relative number#

Like what vim does:

(global-display-line-numbers-mode)
(setq display-line-numbers-type 'relative)

Ref:

Modeline#

What we call statusline in Vim, they call it modeline in Emacs. IMO, they are very similar concepts. We can use doom-modeline to help use customize the modeline.

First, we need to install some special icon that doom-modeline uses. We need to install package all-the-icons to install the fonts needed:

(straight-use-package 'all-the-icons)

Run M-x all-the-icons-install-fonts to install the icon fonts.

Then install doom-modeline:

(straight-use-package 'doom-modeline)

Ref:

Tabline#

For Emacs 27, add the following setting to init.el:

show trailing white space#

Add following config:

(setq-default show-trailing-whitespace t)

Ref:

https://stackoverflow.com/a/34589105/6064933

Auto-completion#

We can use package company to enable auto-completion for functions and options etc. Add the following setting to init.el:

;; enable company mode in all buffers
(add-hook 'after-init-hook 'global-company-mode)

;; use tab and s-tab to cycle forward and backward completion items
(add-hook 'after-init-hook 'company-tng-mode)

The company-tng-mode is used to make Tab and SHIFT-Tab to move forward and backward in the auto-completion menu, for more details see issue here and PR here.

fuzzy search#

If you have used Vim, you may be familiar with fzf.vim or LeaderF or telescope.nvim, depending which Vim distribution you are using. In Emacs, we can use ivy.

Here is a minimal config:

(straight-use-package 'ivy)
(straight-use-package 'counsel)

;; enable ivy mode by default
(ivy-mode 1)

Then ivy will be automatically enabled when you open file/buffer or run commands. Note that the default fuzzy search behavior of ivy is different from that of LeaderF or fzf. For example, to search package-install in ivy, you can type pac ins, but not pacins, the space between is important.

Parentheses: highlight and insert#

To highlight matching parentheses, add the following config:

;; show matching parentheses
(show-paren-mode t)

;; the style for matching parentheses
(set-face-background 'show-paren-match "#ff0000")
(set-face-attribute 'show-paren-match nil
            :weight 'bold :underline t :overline nil :slant 'normal)

To insert matching parentheses automatically, install package smartparens:

(straight-use-package 'smartparens)

;; enable smartparens mode
(smartparens-global-mode t)

To change, delete, add matching pairs easily, there is also evil-surround, mimicking the behavior of vim-surround by Tpope.

Ref:

Issues#

custom-set-faces is added automatically to init.el#

After installing packages, init.el is automatically updated and include custom-set-faces. We can use the following setting to disable this behavior:

(setq custom-file (locate-user-emacs-file "custom.el"))
(load custom-file)

Ref:

Change backup directory#

By default, Emacs saves a backup file in the same directory as the current file, which pollutes the current working directory. We can move the backup directory to a centralized directory:

;; change backup settings
(setq backup-directory-alist '(("." . "~/.cache/emacs-backups")))

Conclusion#

After learning and trying Emacs for one day, I would say that Emacs is truly a great GUI editor which has some really cool features. For example, in Emacs, you can set different font style (size, bold, italic, font family etc.) to different texts in the same buffer, which org-mode used for beautiful type-setting inside Emacs2. You can also get the vscode-like experience via customize groups.

However, when it comes to editting text and navigation, I think Vim/Nvim is still the king, the proof of which is apparent since we have got evil-mode in Emacs.

References#