Customization tips¶
Use flycheck instead of flymake¶
(when(load"flycheck"tt)(setqelpy-modules(delq'elpy-module-flymakeelpy-modules))(add-hook'elpy-mode-hook'flycheck-mode))
Enable emacs 26 flymake indicators in an otherwise light modeline¶
(setqelpy-remove-modeline-lightert)(advice-add'elpy-modules-remove-modeline-lighter:around(lambda(fun&restargs)(unless(eq(carargs)'flymake-mode)(applyfunargs))))
See also theassociated issue
Auto-format code on save¶
Elpy allows you to auto-format your code withC-cC-rf (elpy-format-code).Many people find it useful to auto-format their code automatically on save.This can be achieved with the following snippet:
(add-hook'elpy-mode-hook(lambda()(add-hook'before-save-hook'elpy-format-codenilt)))
The formatting function (elpy-format-code here) can be replaced with your preferred one.For example to use black:
(add-hook'elpy-mode-hook(lambda()(add-hook'before-save-hook'elpy-black-fix-codenilt)))
Alternatives toelpy-goto-definition¶
Fallback to rgrep¶
You may sometimes find when you try to navigate to a function/class definition withelpy-goto-definition (M-.), that instead of jumping to the definition, you get the message “No definition found”. If you see this error often (because of the nature of the code you work on), you can use the following function instead of/in addition toelpy-goto-definition:
(defunelpy-goto-definition-or-rgrep()"Go to the definition of the symbol at point, if found. Otherwise, run`elpy-rgrep-symbol'."(interactive)(if(version<emacs-version"25.1")(ring-insertfind-tag-marker-ring(point-marker))(xref-push-marker-stack))(condition-casenil(elpy-goto-definition)(error(elpy-rgrep-symbol(concat"\\(def\\|class\\)\s"(thing-at-point'symbol)"(")))))
This function will try to find the definition of the symbol at point usingelpy-goto-definition, but will doelpy-rgrep-symbol instead, if the former function fails to return a result. You can bind this function to the key combination of your choice, or you can bind it toM-. to use it as a replacement for the the defaultgoto-definition function:
(define-keyelpy-mode-map(kbd"M-.")'elpy-goto-definition-or-rgrep)
Jumping to assignment¶
As an alternative toelpy-goto-definition, Elpy also provides the functionelpy-goto-assignment that jumps to the line where the symbol at point has been assigned.For functions and classes, it behaves roughly likeelpy-goto-definition but has some advantages in certain situations (like if you want to jump to a decorated function).You can try this alternative with the following code:
(define-keymap(kbd"M-.")'elpy-goto-assignment(define-keymap(kbd"C-x 4 M-.")'elpy-goto-assignment-other-window)
Enable full font locking of inputs in the python shell¶
(advice-add'elpy-shell--insert-and-font-lock:around(lambda(fstringface&optionalno-font-lock)(if(not(eqface'comint-highlight-input))(funcallfstringfaceno-font-lock)(funcallfstringfacet)(python-shell-font-lock-post-command-hook))))(advice-add'comint-send-input:around(lambda(f&restargs)(if(eqmajor-mode'inferior-python-mode)(cl-letf((g(symbol-function'add-text-properties))((symbol-function'add-text-properties)(lambda(startendproperties&optionalobject)(unless(eq(nth3properties)'comint-highlight-input)(funcallgstartendpropertiesobject)))))(applyfargs))(applyfargs))))
See details inhttps://github.com/jorgenschaefer/elpy/issues/1428 andhttps://debbugs.gnu.org/cgi/bugreport.cgi?bug=32344.