📜 ⬆️ ⬇️

Emacs as a code editor for Python and Golang

image


Introduction


When six months ago I decided to switch from Vim to Emacs, I first decided to look for articles on how to configure the latter for Habré. To my surprise, there was only one article in which they told how to set up this editor to work with Python. I had 2 years of experience with vim and there were certain requirements that were not covered in this article. In general, there are very few Russian-language articles about working in Emacs on Python on the Internet. I will not talk about the subtleties of customizing Emacs itself; even a separate article is not enough for this.


I just want to warn fans of Emacs vs Vim holivar, as well as Emacs / Vim vs IDE - I do not want to breed useless disputes on these topics. After a long search, I found an editor that suits me all and which can be customized as you please. I just want to share my configs, and also hope to see alternative solutions in the comments in order to continue customizing this tool for myself.


Requirements


The very first requirement is an easily portable configuration and installation of plug-ins between home and work.
For this I use github . Since it often happens that I change something in configs, I use meld to view the difference between files.
In meld, you can view both directories and individual files. After synchronization with the same meld I add all changes to the home directory.


An equally important requirement is a convenient plugin manager, with lazy loading and updating, like NeoBundle from vim. In the process of finding such a manager, it also turned out that plug-ins can be compiled in the e-mail to speed up loading. Fortunately, there was just such a manager who met all the requirements and more. This is el-get - the perfect manager with auto-compilation, automatic initialization, and also the most important - installation recipes. I will give a short example of a recipe, for clarity.


(:name flycheck :type github :pkgname "flycheck/flycheck" :checkout "0.25.1" :minimum-emacs-version "24.3" :description "On-the-fly syntax checking extension" :build '(("makeinfo" "-o" "doc/flycheck.info" "doc/flycheck.texi")) :info "./doc" :depends (dash pkg-info let-alist seq) :post-init (progn (add-hook 'python-mode-hook #'flycheck-mode) (add-hook 'js-mode-hook #'flycheck-mode) (add-hook 'web-mode-hook #'flycheck-mode) (add-hook 'lisp-interaction-mode-hook #'flycheck-mode) (add-hook 'fish-mode-hook #'flycheck-mode) (add-hook 'markdown-mode-hook #'flycheck-mode) (add-hook 'go-mode-hook #'flycheck-mode) (setq flycheck-check-syntax-automatically '(mode-enabled save idle-change)) (setq flycheck-highlighting-mode 'lines) (setq flycheck-indication-mode 'left-fringe) (setq flycheck-checker-error-threshold 2000) )) 

This is a recipe for installing a flycheck plugin, to syntactically check files when editing, on the fly. In the recipe, you can specify the source of the code - the git / github repository or just a link to the file or the name of the plug-in in ELPA / MELPA, the necessary dependencies, in the case of github - a branch or tag.
The: build command allows you to run system utilities if they are necessary for the correct installation of the plugin.
There is also a wonderful command -: post-init, in it you can specify the Lisp code, which will be launched every time after the initialization of the plugin. I decided to register in this command all the settings associated with this plugin. In general - a very convenient tool.


I immediately refused the ~/.emacs.el file due to synchronization between work and home.
Therefore, all settings are located in the ~/.emacs.d . The thinnest init.el in which logically split files from the settings folder are connected.


 (add-to-list 'load-path (expand-file-name "settings" user-emacs-directory)) (require 'dark-mint-theme) (require 'scratch_my) (require 'package_my) (require 'hooks_my) (require 'keybindings_my) ...    customize  

The first file is the design theme. More topics here


scratch_my.el describes all the standard settings and includes the necessary modes that are already present from scratch in any fresh Emacs.


Package_my.el has a list of all installed plugins and several functions for correct installation.


Hooks of different modes are described in the file hooks_my.el .


Change key shortcuts rendered in a separate file keybindings_my.el .


Also synchronized folder with yasnippet snippets and recipes .
Very often I make changes to recipes by adding plugin settings there, so I keep them in a separate folder.


Plug-ins independent of programming language


I will not describe in detail all the plug-ins, a link is attached to each where everything is described in detail.


Perhaps we start with avy , a plugin that allows you to switch to a word containing the entered character (an analogue of vim-easymotion ).


For autocompletion, use company-mode — universal autocompletion for Emacs.
It works very quickly, it is fully configured, although its documentation is incomplete, I often dropped in the source code. Convenience manifests itself in the structure of this plug-in. There is a backend — an individual code for different modes and a frontend — a common code that draws the results of the auto-completion in Emacs itself. For different modes, different backends are used. For example, company-elisp backend is used for lisp, company-jedi is used for python (jedi's backend is a library for static analysis of Python code).
To supplement the golang code, the company-go backend is used.


expand-region is used for semantic text selection. More detail in this video .


I use three great plugins to work with git - git-gutter , mo-git-blame and magit . I want to highlight the last plugin - it's just awesome, a couple of touches and the code is already on the server ( video ).


But what about the file manager you ask? neotree . Integration with git is present.


Very useful plugin - helm - autocompletion to everything. Search in open buffers ( helm-swoop ), in recently opened files, in files in the current directory, search in commands, rename variables in several buffers, and much more. Excellent article describes all the features of this plugin.


multiple-cursors - after watching this video, you will definitely want to try it :).


A plugin with which you can assign an action to double-click any key is called key-chord ( video )


Analogue vim-powerline - powerline


projectile - plugin for project management in Emacs. Project search, project file replacement, transition within the project, project switching and many other useful functions. Details in this article.


yasnippet - allows you to create and use snippets for different programming languages.


Python mode


Finally, we turn to setting up Emacs directly to work with Python.


Perhaps we start with hooks


 ;; Python mode (defun my-merge-imenu () (interactive) (let ((mode-imenu (imenu-default-create-index-function)) (custom-imenu (imenu--generic-function imenu-generic-expression))) (append mode-imenu custom-imenu))) (defun my-python-hooks() (interactive) (setq tab-width 4 python-indent 4 python-shell-interpreter "ipython" python-shell-interpreter-args "-i") (if (string-match-p "rita" (or (buffer-file-name) "")) (setq indent-tabs-mode t) (setq indent-tabs-mode nil) ) (add-to-list 'imenu-generic-expression '("Sections" "^#### \\[ \\(.*\\) \\]$" 1)) (setq imenu-create-index-function 'my-merge-imenu) ;; pythom mode keybindings (define-key python-mode-map (kbd "M-.") 'jedi:goto-definition) (define-key python-mode-map (kbd "M-,") 'jedi:goto-definition-pop-marker) (define-key python-mode-map (kbd "M-/") 'jedi:show-doc) (define-key python-mode-map (kbd "M-?") 'helm-jedi-related-names) ;; end python mode keybindings (eval-after-load "company" '(progn (unless (member 'company-jedi (car company-backends)) (setq comp-back (car company-backends)) (push 'company-jedi comp-back) (setq company-backends (list comp-back))) ))) (add-hook 'python-mode-hook 'my-python-hooks) ;; End Python mode 

To work with plugins, you need to install a couple of packages via pip


 pip install -U jedi virtualenv flake8 

We set the indentation settings and the path to the interpreter, expose specific shortcut keys, add company-jedi backend and configure imenu .


About auto-completion has already been higher (company-jedi), searching by file and file structure (class names, variables, methods, etc.) occurs via imenu (F10), opening and closing the NeoTree file manager occurs when pressing F7.


Now a little about the used plugins


auto-virtualenv - works great, especially in conjunction with projectile.


jedi-core - gives company-jedi auto-completion options, goes over definitions, shows docks for functions and classes, and so on.


pip-requirements - as the name implies - package to work with requirements.


py-autopep8 - allows you to automatically format the code in accordance with the pep8 standard.


py-isort - you can automatically sort all imports in a file


Python REPL


In the hooks we indicated which version of the shell we would use.


  (setq python-shell-interpreter "ipython" python-shell-interpreter-args "-i") 

When you click Cc Cc - the entire contents of the current buffer is transferred to ipython, where you can immediately test it.
If you need to see a part of the code, select it and press Cc Cr


You can also use the debbager built into python-mode by executing the command Mx pdb RET (RET - in emax it means enterter / return, at first it confused me). There is also a more functional version - realgud


As an example, I want to show some screenshots
image


image


Emacs for golang


Several plugins are used when working with go projects.


go-mode - go-mode operation mode.
Allows you to view the documentation, go through the definitions, use various utilities for checking and formatting code such as gofmt, golint, and so on. Very handy plugin.


flycheck-gometalinter - integration of gometalinter with flycheck.


company-go - backend for company


image


Other plugins


markdown-mode - in this mode, I just write this article. And with the help of livedown, I view the result immediately in the browser.


web-mode - allows you to work with html, css, Django / Jinja2 templates.


restclient - plugin for working with various apishkami. Emacsrocks will show more in your video .


Conclusion


It is a pity that in one article it is impossible to paint all the features and subtleties of setting each plug-in, but I hope the list with a short description will allow readers to learn something new from my experience. Also, Habrahabr is famous for the fact that you can often find out really useful information from the comments, so I ask you to share your settings and experiences; it will be useful for me and you.


Traditionally, all errors and comments please send me a private message.


useful links



')

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


All Articles