Emacs as DevOps editor

by (December 29, 2016)

Posted in Tools  Tags:Emacs, Editor, DevOps

Preface

Using vi is like being in a relationship, you are involved. Using Emacs is like a marriage, you are committed, in large part because there is so much to learn. This is one of the reasons I don’t recommend it for everyone. – devopsanywhere

Introduction

Lets first answer the question: who is DevOps ? What kind of activities he or she is performing ? Answering this question is necessary precondition to get into workflow implementation details.

  • Typical sysadmin activities: logging onto remote servers, managing files and directories, editing configurations, restarting services, watching logs and so on.
  • Software development activities: all DevOps work somehow connected to writing various scripts on different languages and formats: Bash, Python, Go, Ansible, Salt, Puppet, JSON, YAML, etc. Those all are stored in projects in DVCS systems like git or Mercurial.
  • Management activities - because DevOps work is more about managing your organisation via code - it usually requires developing documentation on current processes, documenting improvements, architectures and new procedures.

Lets see how all of the above can be implemented using Emacs.

Step by step Emacs introduction to DevOps activities

Shell scripts

Shell scripting is a large part of any sysadmin or DevOps activities. Emacs supports various shells syntax by default, you don’t need to install any additional modules.

Emacs provides the following for shell scripting:

  • Support for shell scripting syntax and templates (check the top menu “Sh-Script” for all operations)
  • Ability to execute script within the emacs
  • Debugger

A short video with shell scripting introduction:

Various scripts and languages support

Emacs has hundreds of available modules for different languages. You can install those by running M-x package-list-packages

In the following video it is explained how various Emacs modes can help doing DevOps jobs.

  • yaml-mode

General work with files

Working with any lists is easy in Emacs because files/buffers/functions and other things are lists, and Lisp was always good on fast manipulation of lists.

Following extension packages are recommended:

Using the combination of above you will be able to quickly open files for:

  • Project context
  • Directory

All the operations you usually do in Midnight Commander can be done in Emacs as well: finding files, searching for text, replacing in multiple files, copy and move between directories, etc.

Key bindings

Action Key Function
Open file C-x C-f find-file
Close file C-x k kill-buffer
Open file (helm) * C-z C-f helm-find-files
Open file from current project (helm-projectile) * C-c h p helm-projectile
Show Ztree * C-x z ztree-dir
Dired (file manager) C-x d dired
Dired (jump to current file) C-x j dired-jump
Buffers list C-x C-b list-buffers
iBuffer * F9 ibuffer

All keys starting from * are custom.

Work with remote files

Its a good alternative for editing files on host. Why?

  • Sometimes text editor on the other end don’t provide simple things like syntax highlighing or support for various modes - even if you install basic Emacs on other side, it will still require some configuration, and using local editor can be preferrable, especifally when there are a lot of buffer exchange operations with other snippets
  • It can be even faster, especially when you open remote files from bookmark.
  • In situations like editing file inside of Docker container you would’t have good terminal emulation.

Note on multi-hop

You can open files on remote hosts and hop to next method which can be SSH or sudo.

For example to open file under ‘root’ user on remote ‘smith16’ server do this:

C-x C-f /ssh:smith16|sudo:root@smith16:/etc/

Literate DevOps

This approach is very good as its a combination of sys-admin work and documentation work. As result of this activity DevOps produces working documentation on some step-by-step process of installation or maintenance.

… See also this one: Literate Devops with Emacs

Management activities: documentation, trackers and calendar

Emacs org-mode is well known suite for managing documentation. The main advantage is that you can write documentation in org format and then export it to any of preferred formats like Markdown, PDF or Jira/Confluence/Redmine/etc.

Emacs org-mode can connect to trackers like Redmine and Trello and show you the final Agenda screen for current date. It also contains additional tags, like repeating events or deadlines.

Shared sessions for software development

Using text-based editor is the advantage for pair programming - in some situations you can share your screen with tmux or tmate and then several people can work on the same code.

Powerful text editing abilities

Emacs text editing capabilities are very good. Look at this video for example:

Integration with DVCS

DVCS - Distributed Version Control Systems like git or Mercurial. Using those from Emacs makes your life much easier as you don’t need to remember sometimes cryptic commands. It also makes it possible to browse history, branches, revisions and other things easy.

Recommended videos:

Useful emacs packages

  • which-key - shows available key combinations
  • flychech - syntax checker
  • markdown-mode
  • magit
  • ahg
  • ace-jump-mode
  • crontab-mode
  • bm - bookmarks manager
  • helm
  • helm-ag
  • php-mode
  • elpy, jedi - for Python development

Nice themes

  • cyberpunk
  • leuven
  • base16
  • syscolors
  • avk-emacs-themes - mine!
  • tdsh
  • subatomic

… and more at https://emacsthemes.com/

Nice fonts

  1. Input Mono
  2. Source Code Pro
  3. DejaVu Sans Mono
  4. Ubuntu Mono
  5. Monaco
  6. BP Mono
  7. Terminus
  8. Consolas

Also see: http://www.slant.co/topics/67/~programming-fonts, http://omarrr.com/top-7-open-source-monospace-fonts-for-developers/

P.S.

Text editors are like wine. The older the better. You do not want a shiny new text editor. You want the text editor that has been around long enough and used by enough geeks that every conceivable pattern of manipulating symbols on the screen has been thought of, and crystallized into a re-usable pack. – Vivek Haldar