Override Native Mode Prefix Key in Emacs

I try to teach myself emacs, and because I use dvorak, I stupidly bounced Cc to the motion key and got used to it. Now I'm really starting to program with it, and I downloaded the python file and noticed that Cc is a prefix for all the special commands in python-mode .

Can I re-bind the prefix key and change all python commands in one fell swoop in my init.el file? If not, should all python commands be rewritten individually?

+4
source share
2 answers

If you look at the source code for python.el, you will see that the commands were added individually, using the full specification, for example. (define-key map "\Cc\Cr" 'python-send-region) .

So, you have to redo them all yourself. However, it is pretty straight forward. From your comment, you want to change the prefix key as C- ' , and the trick to getting the right to escape is to not use screens at all, but instead use the kbd () macro.

With this, you can update the definition of the mode map:

 (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-' Ck") 'python-mark-block) (define-key map (kbd "C-' Cd") 'python-pdbtrack-toggle-stack-tracking) (define-key map (kbd "C-' Cn") 'python-next-statement) (define-key map (kbd "C-' Cp") 'python-previous-statement) (define-key map (kbd "C-' Cu") 'python-beginning-of-block) (define-key map (kbd "C-' Cf") 'python-describe-symbol) (define-key map (kbd "C-' Cw") 'python-check) (define-key map (kbd "C-' Cv") 'python-check) ; a la sgml-mode (define-key map (kbd "C-' Cs") 'python-send-string) (define-key map (kbd "C-\\ Mx") 'python-send-defun) (define-key map (kbd "C-' Cr") 'python-send-region) (define-key map (kbd "C-' Mr") 'python-send-region-and-go) (define-key map (kbd "C-' Cc") 'python-send-buffer) (define-key map (kbd "C-' Cz") 'python-switch-to-python) (define-key map (kbd "C-' Cm") 'python-load-file) (define-key map (kbd "C-' Cl") 'python-load-file) ; a la cmuscheme (substitute-key-definition 'complete-symbol 'symbol-complete map global-map) (define-key map (kbd "C-' Ci") 'python-find-imports) (define-key map (kbd "C-' Ct") '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 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)) 
+1
source

If you want Ctrl + C to act like left and F12 to act like Cc :

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

Note that this also affects multiple key bindings, for example. you need to type Ctrl + X F12 to exit Emacs. The flip side of this coin is that Cc Cc is dialed as F12 F12 .

+3
source

All Articles