Cooking Linux the right way: Vim and Emacs

OK, let's talk about Linux superpowers. Vim's superpower is to move fast, really fast. To edit by commands with single keystrokes, cut, paste, find & replace the text at a maximum speed. Yes, there's some learning curve, but you can start with Vim mode in some editors and IDE and after learning Vim make use of Vim keystrokes in other programs, including Chromium, for example. Navigating and editing with the keyboard, without using a mouse will be faster.

Emacs' superpower is that you can get further than customizing, you can re-write the behavior of the editor using Lisp. As a demo of this deep customization or re-writing capabilities, you can use the Evil mode, which emulates Vim inside Emacs well. Emacs have modules for RSS reader, Telegram client, Media player, Email and Newsreader, IRC client, and more. The question with Emacs is actually "What do you want from it?"

Vim and Emacs are different regarding approaches to text editing: Vim is like "The editor does exactly what I type", while Emacs is more "Let me do that for you", like inserting closing parenthesis, formatting text, etc. But both editors are customizable to the extent that you can have almost the same behavior. Both have file managers nowadays. Emacs is an integrated environment to suit all your needs, while Vim sticks to the standards and there's an advanced mode to use Vim, (which I'll cover in this article) with a terminal multiplexer. To demonstrate the superpowers I'll take three use cases Markdown, HTML, note taking. I'm not a programmer and that's what I use on a day-to-day basis. I worked in Vim and Emacs for several months like 8 hours a day, so it's not a first impression.

Emacs is better as a GUI app, and Vim, from my viewpoint, is better in a terminal. Thus Vim has a life hack: if you have to work in Windows and you don't have privileges to install programs, you can install Git for Windows. Vim is included, so you can install any plugins and customize Vim further.

I'll provide very basic configs I use, and I'd recommend learning both text editors, they are nice. When you are bothered, you can switch and have fun. :) For each use case, I'll try to describe briefly some useful functions.

Vim Emacs
Notes VimWiki, Orgmode.nvim, Neorg Org mode
HTML Emmet, Snippets HTML mode
Markdown Vim-markdown, Snippets Markdown mode

Initial setup

My configs are quite short, let's look at them, and then I'll explain everything line-by-line. So, my Vim config is the following

syntax enable
colorscheme monokai
set wrap linebreak nolist
call plug#begin()
  Plug 'itchyny/lightline.vim'

  Plug 'SirVer/ultisnips'
  Plug 'honza/vim-snippets'
    let g:UltiSnipsExpandTrigger="<tab>"
    let g:UltiSnipsJumpForwardTrigger="<c-b>"
    let g:UltiSnipsJumpBackwardTrigger="<c-z>"

  Plug 'plasticboy/vim-markdown'
  Plug 'godlygeek/tabular'
  Plug 'mattn/emmet-vim'
  call plug#end()
  set laststatus=2 
  set langmap=ФИСВУАПРШОЛДЬТЩЗЙКЫЕГМЦЧНЯ;ABCDEFGHIJKLMNOPQRSTUVWXYZ,фисвуапршолдьтщзйкыегмцчня;abcdefghijklmnopqrstuvwxyz
  set conceallevel=2

And this is my Emacs config file:

(scroll-bar-mode -1)
(tool-bar-mode -1)
(menu-bar-mode -1)
(setq default-frame-alist '((undecorated . t)))
(setq inhibit-startup-screen t)
(setq dired-listing-switches "-laGh1v")
(setq-default word-wrap t)
(setq sentence-end-double-space nil)
(custom-set-faces
'(default ((t (:family "Source Code Pro" :foundry "ADBO" :slant normal :weight semi-bold :height 132 :width normal)))))
(add-to-list 'custom-theme-load-path "~/.emacs.d/themes/")
(load-theme 'monokai t)
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
(custom-set-variables
  '(menu-bar-mode nil)
  '(package-selected-packages '(use-package reverse-im markdown-mode))
  '(tool-bar-mode nil))
(use-package reverse-im
  :ensure t
  :custom
  (reverse-im-input-methods '("russian-computer"))
  :config
  (reverse-im-mode t))
(recentf-mode 1)
(global-set-key (kbd "C-x C-r") 'recentf-open-files)

For me, that doesn't look too complicated. For Vim I add the color scheme, add a status line, and a soft word wrap:

syntax enable
colorscheme monokai
set wrap linebreak nolist
call plug#begin()
  Plug 'itchyny/lightline.vim'
call plug#end()

To fix a bug with the status line and add mapping to Cyrillic keys (not to switch to Cyrillic in normal mode) we need to add this:

set laststatus=2 
set langmap=ФИСВУАПРШОЛДЬТЩЗЙКЫЕГМЦЧНЯ;ABCDEFGHIJKLMNOPQRSTUVWXYZ,фисвуапршолдьтщзйкыегмцчня;abcdefghijklmnopqrstuvwxyz

As Vim is launched in some terminals, I specify the font in the config for the terminal. Regarding Emacs, first, we need to switch off the menu (it's accessible with F10), and the scrollbar and switch off the window decoration to save some space. I also switch off the startup screen as it becomes annoying after a few launches. I turn on soft word wrap and make Dired, the file manager inside Emacs, look a bit prettier.

(scroll-bar-mode -1)
(tool-bar-mode -1)
(menu-bar-mode -1)
(setq default-frame-alist '((undecorated . t)))
(setq inhibit-startup-screen t)
(setq dired-listing-switches "-laGh1v")
(setq-default word-wrap t)

To add a theme and specify a custom font set I do the following:

(setq sentence-end-double-space nil)
(custom-set-faces
'(default ((t (:family "Source Code Pro" :foundry "ADBO" :slant normal :weight semi-bold :height 132 :width normal)))))
(add-to-list 'custom-theme-load-path "~/.emacs.d/themes/")
(load-theme 'monokai t)

I don't specify the font face manually, instead, I choose it from the menu and Emacs adds it to the config. For some reason Emacs thinks that sentences are divided using double spaces, so the first line is to fix it to be able to jump between sentences with Alt-E and Alt-A.

I use a separate reverse-im module to make hotkeys work with Cyrillic keymap, first, we need to add repositories and install 'package':

(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
(use-package reverse-im
  :ensure t
  :custom
  (reverse-im-input-methods '("russian-computer"))
  :config
  (reverse-im-mode t))

Markdown

Regarding Markdown, let's take Vim first:

  • Snippets allow you to insert markup for almost everything (links, images, headers, etc.)
  • Vim-markdown allows folding, insert table of contents and also have TOC inside Vim to move faster between sections!
  • Bullets auto-insert is also available
  • Auto table formatting

To do this, you need to install several plugins and set conceal level:

call plug#begin()
  Plug 'SirVer/ultisnips'
  Plug 'honza/vim-snippets'
    let g:UltiSnipsExpandTrigger="<tab>"
    let g:UltiSnipsJumpForwardTrigger="<c-b>"
    let g:UltiSnipsJumpBackwardTrigger="<c-z>"

  Plug 'plasticboy/vim-markdown'
  Plug 'godlygeek/tabular'
  call plug#end()
  set conceallevel=2

Snippets are quite useful and some people don't get their full capabilities. For a link, for example, I enter '[c' and then Tab in Insert mode, then enter the name for the link, press Ctrl-B and then enter the URL. I also can get back to the link name with Ctrl-Z in Insert mode. Well, it doesn't work so well with links inside tables. :( Concealing also breaks formatting sometimes, like you have plenty of whitespace at the end of the line and some characters on the next line.

Emacs allows it all this plus a bit more:

  • It can automatically insert numbered list as well
  • Links concealing is a bit better
  • A few more functions to work with headers

All of this is done in the Markdown package. Emacs adds lines to config itself, you need to run a package install command. I like Emacs' documentation a bit more because it's embedded into the code. In Vim you have to look at the actual snippet code, and in Emacs, I do Ctrl-H M to view all the up-to-date documentation for Markdown mode.

HTML

It's simple, you need to insert tags, lots of tags. Emmet is available for Emacs, as for Vim. With Emmet you can enter ul>li*3 to insert an unordered list with 3 bullets. Emmet is a bit overkill for me, so I use embedded HTML mode in Emacs and snippets in Vim to insert paragraphs and some lists. For example, in Vim this could be 'p' and Tab, in Emacs Ctrl-C and then Enter. The workflow after that can be different, as in the terminal I can use xclip to copy a file to the buffer, in Vim you can do ':%y+' and in Emacs, you can do Ctrl-X H and then Alt+W.

Taking notes

Org mode generally is a reason to use and learn Emacs, and it's great. At first, I didn't get it so I started with VimWiki. I had to leave it towards Emacs for several reasons:

  • Working with links in convenient ways matters
  • Big headers help a lot
  • I know that I can have more dimensions: categories and tags
  • Lots of tools to work with org files also matter

Org mode is embedded, so you can just open an .org file and start experimenting!

For Vim you are welcome to try Orgmode.nvim and Neorg, they look promising, but I didn't try them. You can also use vim-markdown for note-taking, you'll be able to jump between files using links or preview HTML in a browser.

Vim advanced mode

The 'standard' Vim use is Gnome Terminal + Vim or GVim. What I call 'advanced' mode is a terminal multiplexer like Tmux + terminal (I prefer Alacritty) + Vim. It's a separate topic, how to set it up and use it, I'll just mention what it allows you to do:

  • Have a terminal pane and a Vim pane inside one window and use Tmux to switch between windows
  • Have pre-defined sessions with pre-defined windows and panes
  • Copy and paste a text from the terminal using the keyboard

Some people say that it's less disturbing to have everything in a terminal, as you don't switch the context. Just try it and decide for yourself. :)

Conclusions

Emacs is my choice at the moment because my acid test revealed that I can do more and get stressed less with Emacs. For example, I work a lot with links, and my workflow is the following:

  1. Type some text
  2. Decide that some words should become a link, so I select those words
  3. Insert link target from the clipboard, Emacs just inserts all the markup

I suffer to do the same with snippets. I had a lot of mistakes with link markup, so I prefer the Emacs way to work with them as it's error-proof. Besides, Org mode was developed in Emacs and it's polished, and Emacs has a lot of export functions which saved a ton of time for me. I learned about Orgmode.nvim and Neorg doing quick research right before writing this article, they seem to be still under development, and I had no reason to switch from Emacs. Although I wrote this particular article in Vim just to be sure that my .vimrc works. :)

Update: I've found a way to get almost the same in Vim finally: UltiSnips + Visual mode.

@Konstantin Ovchinnikov
Tags: #linux #productivity #vim #emacs

Comments