According to the results of research work programmers about 20% of the time spent on direct writing code, and about 80% of the time - to view the old, its analysis. Based on this premise, the text editor must first provide convenient means of navigating through the code. Most of the described features deal with navigation and analysis.
In this article, I will try to demonstrate the maximum of the convenient in daily use of the features of the Emacs editor for the Python language, and more specifically for editing Django projects.
As the saying goes: “they have been waiting for the promised three years.” Although three years have not passed, there is already a good deal from my previous article , Emacs for Beginners: An Introduction . I will not “spread out across the tree to the tree,” but I will try to show as briefly as possible how the various Emacs features work as applied to Python. ')
Article format:
feature description
an example of how to include it in your config
and accompanying video demonstration of the work. Warning: sound quality on all videos so-so. I will fix it, but later ...
Pretty handy feature. In any place, if you have forgotten any details on the documentation, you can call one of the options for displaying documentation:
pylookup - indexed Sphinx version for standard Python library
pydoc - inline documentation from the object (via ropemacs)
I show these features separately as they are independent of each other.
1.1. Rope pydoc
Recipe of inclusion: according to the recipe of inclusion rope separate and talk and is described in:
After turning on the rope, its keybindings and menu start working:
In the menu, this function is available through: [Rope] -> [Shown documentation]
Keybinding: Cc d
1.2. Pylookup - an index of documentation on standard library Python
The package is installed from here: https://github.com/tsgates/pylookup . It has two active components: pylookup.el is placed in a directory readable by emacs for downloading Lisp, and pylookup.py is placed somewhere where it will be accessible along the way for the user.
cd ~/ svn co http://code.djangoproject.com/svn/django/trunk/docs/ django-docs cd django-docs make html cd _build/html ln -s genindex.html genindex-all.html
There is a wonderful pdbtrack module that “watches” movements on the program code during debugging on the n, s keys and others. And by the way, skilfully working with pdb can be achieved more than in similar visual debugging tools.
pdbtrack is automatically enabled with the inclusion of the main python-mode mode.
In order for it to be invoked in emacs, you need to put a line like: import pdb; pdb.set_trace () in your code. The executable program must be executed in emacs / shell.
3. Rgrep
rgrep - fast search for a string in all files with a specific extension. Here it is worth noting that starting with the emacs 23.x version, the developers changed something in the call interface and now it’s impossible to separate the extensions with a space, such as [* .html * .py], if you don’t know the workaround: to enter a space in rgrep - screen it with the Cq key
rgrep works surprisingly fast, and even in our largest project with tens of thousands of files it finds what it takes in a split second, which is important for frequent searches.
My rgrep is tied to a key: C-f7
4. Occur
occur shows the entry of this line in the current file. In order to make it convenient to call, I made a macro that automatically highlights the current word and then calls the occor on it. I have this function tied to the key: Cz o
5. Blocks in Emacs
Blocks are:
ordinary
square
Also, blocks can be stored in different registers, respectively, you can work with different blocks at the same time.
5.1. Multiple blocks
Yes. It is not used very often, but when applied, it saves you a lot of time that would have been spent up-and-down on klipper / parcellite or even worse (if you have one clipboard).
Keys:
Cx rs char - to remember the block in the named buffer symbol, for example Cx rs1 - to remember in the block by name 1.
Cx ri char - insert from a named block
5.1. Square blocks
5.1.1. Visual square blocks
The visual mode is activated by the C-enter button. A rectangular area appears in which you can fill in the text, delete, or copy the whole rectangle. In this mode, all stantadnye buttons work with the block - Alt + Y to remember block and ctrl + y to paste.
5.1.2. Non-visual square blocks
There is a second variant of square blocks, somewhat trimmed, but working for the terminal mode. It works like this: select the block as usual (C-space) but at the same time work with the block by keys:
Cx rr char - memorize a rectangular block in a named register
Cx ri char - paste from register
5.2. View kill ring
Some convenient replacement of a named block is the klipper analog built into emacs, called killring:
It is worth noting that the situation with blocks and key bindings to operations is confused by several objective, historical reasons:
Buttons familiar to all Windows for working with Cc Cv blocks do not work by default.
There are two clipboards in xorg: main (primary) and secondary (secondary)
Klipper, Parcellite are also engaged in managing the selection often change the sequence, adding a mess to everything
Emacs changed the procedures for working with xorg primary / secondary selection in its current version 0.24
As a result, often even such a trivial thing as copying in blocks becomes difficult to master. emacs.
It also adds to the fire that in the xorg true way to copy blocks: Control Insert to copy and Shift Insert to embed the block. In this case, the mouse copies to one clipboard and keyboard selections fall into another.
For starters, I often advise you to master the “True Way” right away and not try to turn on the so-called cua-mode, designed to free the Cc Cv standard emacs key bindinds, depriving the end user of the mass of convenient functionality. If you really study emacs, then learn two additional keys:
Alt-w to remember block
Cw to insert it
not a super problem. For those for whom it is - turn on Cua-mode boldly and work further, but most likely you don’t care Return to the topic and return the standard keys when you feel that there are not enough convenient keyboard shortcuts.
6. Yasnippet - input automation with snippets
Yasnippet is a convenient way to automate the input of frequent, but difficult to memorize, blocks of text.
It is installed as an independent emacs package, usually available in the repository of your system.
7.3. By functions and classes of the current file via Speedbar
I set up the Scroll_Lock key:
(global-set-key [Scroll_Lock] 'speedbar)
7.4. Transition to the place of definition of a variable [class, method]
Configured via Rope and bookmark on the keys:
Alt + Enter - go to the place of definition
Alt + Shift + Enter - bounce back
The disadvantage of setup is that the record of the conversion history is global and each transition leaves trash in bookmarks, You will need to rewrite this point in Lisp with an array. But while working and so.
(defun rope-goto-definition-save-place () """ save current place as 'save-place' bookmark and rope-goto-definition """ (interactive) (bookmark-set"save-place"1) (rope-goto-definition) ) (defun rope-return () """ save current place as 'save-place' bookmark and rope-goto-definition """ (interactive) (bookmark-jump"save-place") ) (global-set-key [(M return)] 'rope-goto-definition-save-place) (global-set-key [(M shift return)] 'rope-return)
7.5. Bookmarks
Cz b - put the link
Cz Up - go to the link above (in the current file)
Cz Down - go to the link below (in the current file)
Unfortunately, even after a lot of processing, this solution is not ideal. Sometimes flymake writes _flymake files out of place on network services, sometimes just incorrectly fulfills. But in fact in 98% of cases it is very useful as it allows find out earlier made mistake.
9.2. call pep8
The module is installed in the system: pep8.
; pep8 (require 'python-pep8) (global-set-key (kbd "Cc p 8") 'pep8)
9.3. pylint call
Similarly. First, install the pylint system package and then:
; pylint (require 'python-pylint) (global-set-key (kbd "Cc p l") 'pylint)
10. Django specificity
1) It is necessary to start the project from django-shell - in order to have pdb available. 2) There are several different modules for the syntax display django templates.
11. Compilation mode for python and quick start.
Anywhere in the python program, if this is a stand-alone script, you can run it through the Cc Cc key. When an error is generated in this case, the cursor will be replaced with the error.
Option number two: run the program from the compile option. In my config, it is configured as:
(global-set-key [C-f9] 'compile)
In the case of using the compile function, you can use the F8 / Shift-F8 error traversal.
In both cases, the pdb call will not work and the programs will start without a link to the terminal.
12. In conclusion
What is not included in this article
I tried to describe the features most related to editing Python files. Many are not included in this review, although they are directly related to working with the Python project, for example, such a feature as working with a version control system - it deserves a separate article, which I intend to do in the [near future].
The article is not finished, as comments arrive, I will definitely make changes and additions.
What do I expect from reader comments :
Show me what I forgot (or did not know), I will add.
Who is not lazy to make a similar review on vim, Sublime, and etc.? First of all, the features described here, and those [useful] features that are missing in Emacs, but still present somewhere else, are of interest.
PS The sound quality was disappointing, I wrote using ffmpeg -vf crop = 970: 505: 7: 15 -f alsa -i hw: 0 -f x11grab -r 25 -s 1680x1050 -i: 0.0 -s 1280x720 -vcodec libx264 -vpre lossless_ultrafast filename.avi and on the microphone on the laptop. First, the keystrokes are loud, secondly the sound is quiet and sometimes there is extraneous noise. Alas ... Maybe I will retake the video using headset, but so far there is - that is. Better so than without them at all, right?