views:

53

answers:

2

I've been trying teach myself emacs, and because I am using dvorak, I foolishly rebound C-c to a movement key and got used to it. Now i'm actually starting to do some programming with it, and I loaded up a python file and noticed that C-c is the prefix to all the special commands in python-mode.

Can I rebind the prefix key and change all python commands in one swoop in my init.el file? If not, should I rebind all the python commands individually?

+1  A: 

If you look at the source code for python.el, you'll see that the commands were added individually using the full specification, e.g. (define-key map "\C-c\C-r" 'python-send-region).

So, you're going to have to redo them all yourself. That said it is pretty straight-forward. From your comment, you want to change the prefix key to be C-', and the trick to getting escaping right is to not use escapes at all, but instead to use the macro kbd (documentation link).

With that, you can update the mode-map definition to be:

(defvar python-mode-map
  (let ((map (make-sparse-keymap)))
    ;; Mostly taken from python-mode.el.
    (define-key map ":" 'python-electric-colon)
    (define-key map "\177" 'python-backspace)
    (define-key map (kbd "C-' <") 'python-shift-left)
    (define-key map (kbd "C-' >") 'python-shift-right)
    (define-key map (kbd "C-' C-k") 'python-mark-block)
    (define-key map (kbd "C-' C-d") 'python-pdbtrack-toggle-stack-tracking)
    (define-key map (kbd "C-' C-n") 'python-next-statement)
    (define-key map (kbd "C-' C-p") 'python-previous-statement)
    (define-key map (kbd "C-' C-u") 'python-beginning-of-block)
    (define-key map (kbd "C-' C-f") 'python-describe-symbol)
    (define-key map (kbd "C-' C-w") 'python-check)
    (define-key map (kbd "C-' C-v") 'python-check) ; a la sgml-mode
    (define-key map (kbd "C-' C-s") 'python-send-string)
    (define-key map (kbd "C-\\ M-x") 'python-send-defun)
    (define-key map (kbd "C-' C-r") 'python-send-region)
    (define-key map (kbd "C-' M-r") 'python-send-region-and-go)
    (define-key map (kbd "C-' C-c") 'python-send-buffer)
    (define-key map (kbd "C-' C-z") 'python-switch-to-python)
    (define-key map (kbd "C-' C-m") 'python-load-file)
    (define-key map (kbd "C-' C-l") 'python-load-file) ; a la cmuscheme
    (substitute-key-definition 'complete-symbol 'symbol-complete
                               map global-map)
    (define-key map (kbd "C-' C-i") 'python-find-imports)
    (define-key map (kbd "C-' C-t") 'python-expand-template)
    (easy-menu-define python-menu map "Python Mode menu"
      `("Python"
        :help "Python-specific Features"
        ["Shift region left" python-shift-left :active mark-active
         :help "Shift by a single indentation step"]
        ["Shift region right" python-shift-right :active mark-active
         :help "Shift by a single indentation step"]
        "-"
        ["Mark block" python-mark-block
         :help "Mark innermost block around point"]
        ["Mark def/class" mark-defun
         :help "Mark innermost definition around point"]
        "-"
        ["Start of block" python-beginning-of-block
         :help "Go to start of innermost definition around point"]
        ["End of block" python-end-of-block
         :help "Go to end of innermost definition around point"]
        ["Start of def/class" beginning-of-defun
         :help "Go to start of innermost definition around point"]
        ["End of def/class" end-of-defun
         :help "Go to end of innermost definition around point"]
        "-"
        ("Templates..."
         :help "Expand templates for compound statements"
         :filter (lambda (&rest junk)
                   (abbrev-table-menu python-mode-abbrev-table)))
        "-"
        ["Start interpreter" python-shell
         :help "Run `inferior' Python in separate buffer"]
        ["Import/reload file" python-load-file
         :help "Load into inferior Python session"]
        ["Eval buffer" python-send-buffer
         :help "Evaluate buffer en bloc in inferior Python session"]
        ["Eval region" python-send-region :active mark-active
         :help "Evaluate region en bloc in inferior Python session"]
        ["Eval def/class" python-send-defun
         :help "Evaluate current definition in inferior Python session"]
        ["Switch to interpreter" python-switch-to-python
         :help "Switch to inferior Python buffer"]
        ["Set default process" python-set-proc
         :help "Make buffer's inferior process the default"
         :active (buffer-live-p python-buffer)]
        ["Check file" python-check :help "Run pychecker"]
        ["Debugger" pdb :help "Run pdb under GUD"]
        "-"
        ["Help on symbol" python-describe-symbol
         :help "Use pydoc on symbol at point"]
        ["Complete symbol" symbol-complete
         :help "Complete (qualified) symbol before point"]
        ["Find function" python-find-function
         :help "Try to find source definition of function at point"]
        ["Update imports" python-find-imports
         :help "Update list of top-level imports for completion"]))
    map))
Trey Jackson
if I want to use `C-'`, how do I escape the apostrophe? (I get errors so I assume that is the problem)
colinmarc
@colinmarc Updated answer to use that prefix.
Trey Jackson
on an unrelated note, how do you get markdown to display keys like that?
colinmarc
urgh, I have my movement keys back, but for some reason tying `C-'` just inputs an apostrophe!? =(
colinmarc
@colinmarc To get the markdown for keys, wrap the text with <kbd> and </kbd>
Trey Jackson
@colinmarc Regarding `C-'`, the new definition works for me... To see what is bound to `C-'`, type `C-h k C-'` and in python mode for me it's still waiting for me to type something else (like `C-w`) which then reports `C-' C-w runs the command python-check, ....`.
Trey Jackson
@Trey Jackson - It gives me `' runs the command self-insert-command...` which means it's not registering the Ctrl part? Obviously this is kind of outside the scope of the question, but I've been googling around and haven't found anything, so any pointers would be very appreciated.
colinmarc
@colinmarc In a python buffer, try `M-: (define-key python-mode-map (kbd "C-' C-p") 'python-previous-statement) RET`. Hopefully it won't fail, but if it does, it'll likely fail b/c `C-' is not a prefix key`. You could also try changing the 'defvar' to 'setq'.
Trey Jackson
That worked, although I still can't type `C-'` - it just writes an apostrophe.
colinmarc
@colinmarc It might be something in your .emacs. Try running emacs without `emacs -q`, then load a python file and see if `C-'` still does an insert of `'`. If not, then there's something in your .emacs that's mussing things up.
Trey Jackson
Yup, it still does the same thing.
colinmarc
@colinmarc Ok, last ditch effort, also try w/out the site file `emacs -q --no-site-file`. Also, make sure you're not using Emacs 21.3.50.1 (`emacs --version` or `M-x emacs-version`).
Trey Jackson
Nope, same problem. And I'm using 23.3.1. Thanks for your help; I'll open a new question.
colinmarc
+1  A: 

If you want Ctrl+C to act as left and F12 to act as C-c:

(define-key key-translation-map [f12] "\C-c")
(define-key key-translation-map "\C-c" [left])

Note that this will also influence multi-key bindings, e.g. you now need to type Ctrl+X F12 to exit Emacs. The flip side of this coin is that C-c C-c is typed as F12 F12.

Gilles