              (python-shell-switch-to-shell)))))
 
 ;; Add tweaks to standard python.el defined in this file.
-(defun ach-python-rebinds ()
-  "Add the functions defined in python-programming.el to Python buffer locally."
-  ;; Shell related commands
-  (local-set-key (kbd "C-c C-z") #'ach-python-switch-to-shell)
-  (local-set-key (kbd "C-c C-r") #'spacemacs/python-execute-file)
-  (local-set-key (kbd "C-c C-c") #'python-shell-send-dwim)
-  ;; I don't know why these are not defaults
-  (local-set-key (kbd "C-M-f") #'python-nav-forward-sexp)
-  (local-set-key (kbd "C-M-b") #'python-nav-backward-sexp)
-  (local-set-key (kbd "C-M-a") #'python-nav-backward-defun)
-  (local-set-key (kbd "C-M-e") #'python-nav-forward-defun))
+;; Shell related commands
+(define-key python-mode-map (kbd "C-c C-z") #'ach-python-switch-to-shell)
+(define-key python-mode-map (kbd "C-c C-r") #'spacemacs/python-execute-file)
+(define-key python-mode-map (kbd "C-c C-c") #'python-shell-send-dwim)
+;; I don't know why these are not defaults
+(define-key python-mode-map (kbd "C-M-f") #'python-nav-forward-sexp)
+(define-key python-mode-map (kbd "C-M-b") #'python-nav-backward-sexp)
+(define-key python-mode-map (kbd "C-M-a") #'python-nav-backward-defun)
+(define-key python-mode-map (kbd "C-M-e") #'python-nav-forward-defun)
 
-(add-hook 'python-mode-hook #'ach-python-rebinds)
+;; Debugging stuff
+(defvar ach-python-debug-string
+  "import ipdb; ipdb.set_trace()"
+  "Python debugging string to insert in buffers.")
 
-;; Debugging
-(defun ach-python-debug ()
-  "Insert import ipdb; ipdb.set_trace() in the buffer."
-  (interactive)
-  (insert "import ipdb; ipdb.set_trace()")
+(defun ach--python-insert-debug ()
+  "Insert `ach-python-debug-string' in a line before this one."
+  (back-to-indentation)
+  (insert ach-python-debug-string)
   (newline-and-indent))
+
+(defun ach--python-remove-debug-calls ()
+  "Remove every line containing `ach-python-debug-string' in buffer."
+  (save-excursion
+    (save-restriction
+      (widen)
+      (goto-char (point-min))
+      (let ((re (concat "^\\s-*" ach-python-debug-string "$")))
+        (while (re-search-forward re nil t)
+          (kill-whole-line))))))
+
+(defun ach-python-debug (&optional arg)
+  "Insert `ach-python-debug-string' in line above, remove all of them with ARG prefix."
+  (interactive "P")
+  (if arg
+      (ach--python-remove-debug-calls)
+    (ach--python-insert-debug)))
 
 (define-key python-mode-map (kbd "C-c C-e") 'ach-python-debug)
 

(use-package pyvenv
  :ensure t
  :config
  (defun ach-set-pyvenv-workon ()
    (local-set-key (kbd "C-c C-d") #'pyvenv-deactivate)
    (local-set-key (kbd "C-c C-w") #'pyvenv-workon))
  (add-hook 'python-mode-hook #'ach-set-pyvenv-workon))

;; The warning message is very annoying, let's get rid of it
;; Solution found at:

(with-eval-after-load 'ivy
  (add-to-list 'ivy-ignore-buffers "\\*epc con"))

(add-hook 'python-mode-hook #'rainbow-delimiters-mode)
(add-hook 'python-mode-hook (lambda () (color-identifiers-mode -1)))

