It turns out, C-c C-d is already bound in python-mode
(to 'python-pdbtrack-toggle-stack-tracking
), so you may want to revisit your choice of key binding.
Note: if you just want to cut/paste a solution, jump to the end of the answer. Read on for an explanation of how to get there - in case you want to do it again.
The macro is a good start, but what you have won't quite work. To get something you can bind to a key, try M-x insert-kbd-macro for that macro, and you'll get:
(fset 'add-docstring
(lambda (&optional arg) "Keyboard macro." (interactive "p") (kmacro-exec-ring-item (quote ("def ...unprintable characters...6\"3" 0 "%d")) arg)))
(hm.... non-printable characters, I can't cut/paste into SO, but you can do it yourself to get the right thing). With a little munging, what you get is equivalent to this:
(fset 'add-docstring
(lambda (&optional arg)
"Keyboard macro."
(interactive "p")
(kmacro-exec-ring-item `(,(kbd "C-r def C-n C-a C-m C-p C-i C-u 6 \" C-u 3 C-b") 0 "%d")
arg)))
That's the first step. With the above, you can do M-x add-docstring
and get the behavior you want.
The next step is what you asked - how to bind to keys locally. The documentation for keybindings begins here, and of interest to you is the Local Keymaps section, which leads to the following:
(add-hook 'python-mode-hook
(lambda () (define-key python-mode-map (kbd "C-c C-d") 'add-docstring)))
This sets up an anonymous function to be called when python-mode
is turned on, and that function does one thing - it sets up the key binding you want in the keymap specifically for python-mode.
If you read the Keymaps section closely, you'll see that Emacs follows the convention that only users should bind commads to C-c a, where a is any lower or upper-case letter (e.g. C-c d C-c T C-c p are all available), and packages constrain mode-specific bindings to C-c %, where % is any punctuation character or control key (e.g. C-c C-c C-c [ C-c C-z).
So, if you change your binding to C-c d, then you're pretty much guaranteed not to conflict with any packages out there. The python.el
shipped with Emacs does follow these conventions, as do most (all?) packages shipped with Emacs.
You'll notice that I use kbd
to read in the key sequences. It is portable and I find it a lot easier to read.
There are further things you could do to clean this up:
- put the customizations in a named function
- rewrite the macro in elisp
- use
eval-after-load
instead of the hook (see this question)
Here's what I'd do for #1, which gives you a handy place to put other customizations:
(add-hook 'python-mode-hook 'my-python-customizations)
(defun my-python-customizations ()
"set up my personal customizations for python mode"
;; put other customizations in here
(define-key python-mode-map (kbd "C-c C-d") 'add-docstring))
(defun add-docstring (&optional arg)
"Keyboard macro."
(interactive "p")
(kmacro-exec-ring-item `(,(kbd "C-r def C-n C-a C-m C-p C-i C-u 6 \" C-u 3 C-b") 0 "%d")
arg))
Using a named function is a little cleaner in that you can later do (remove-hook 'python-mode-hook 'my-python-customizations)
if you so desire. Also, if you look at the value for the hook (C-h v python-mode-hook RET), it's obvious what gets called (the anonymous function is longer and more difficult to read).
For the bonus points, after you pasted the code into your .emacs
do M-x eval-region, which will tell Emacs to evaluate the code in the region. To see the changes in your existing python buffers, you'd just have to open a new python file/buffer, which would trigger the keybinding change - which is common to all python buffers.
Happy hacking.