Skip to content

Instantly share code, notes, and snippets.

@lesliesrussell
Last active February 16, 2024 14:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lesliesrussell/46302d413fcf49e9717eeea57fdadcbf to your computer and use it in GitHub Desktop.
Save lesliesrussell/46302d413fcf49e9717eeea57fdadcbf to your computer and use it in GitHub Desktop.
emacs movement by thing
;; Defines a transient keymap for movement controls and sets up a global key binding to activate this transient map. This transient map, `my-movement-transient-map`, includes bindings for various movement commands like moving forward or backward by a word or character and moving to the next or previous line. The `activate-my-movement-map` function is defined to activate this transient map, and it is globally bound to `C-f`.
;; This setup allows you to press `C-f` followed by one of the specified keys (`f`, `b`, `c`, `l`, `n`, `p`) to perform the corresponding movement operation. The `set-transient-map` call with a second argument of `t` ensures that the transient map stays active until one of its keys is pressed.
;; This is a neat way to create a custom, modal-like interface for movement within Emacs, leveraging your Emacs Lisp skills to tailor your editing environment to your preferences. If you have any specific modifications or additional features you'd like to implement, feel free to ask!
(defun my-digit-argument-wrapper (digit)
"Wrapper for digit-argument to be used in transient maps."
(interactive "P")
(universal-argument)
(universal-argument-more digit))
;; Modify your keymap to use the wrapper function
;; (define-key my-movement-transient-map (kbd "1") '(lambda () (interactive) (my-digit-argument-wrapper 1)))
;; (define-key my-movement-transient-map (kbd "2") '(lambda () (interactive) (my-digit-argument-wrapper 2)))
;; ... and so on for the other digits
(defvar my-movement-transient-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "f") 'forward-word)
(define-key map (kbd "b") 'backward-word)
(define-key map (kbd "c") 'forward-char)
(define-key map (kbd "l") 'backward-char)
(define-key map (kbd "n") 'next-line)
(define-key map (kbd "p") 'previous-line)
(define-key map (kbd "a") 'beginning-of-visual-line)
(define-key map (kbd "e") 'end-of-visual-line)
(define-key map (kbd "m") 'back-to-indentation)
(define-key map (kbd ",") 'beginning-of-buffer)
(define-key map (kbd ".") 'end-of-buffer)
(define-key map (kbd "1") '(lambda () (interactive) (my-digit-argument-wrapper 1)))
(define-key map (kbd "2") '(lambda () (interactive) (my-digit-argument-wrapper 2)))
(define-key map (kbd "3") '(lambda () (interactive) (my-digit-argument-wrapper 3)))
(define-key map (kbd "4") '(lambda () (interactive) (my-digit-argument-wrapper 4)))
(define-key map (kbd "5") '(lambda () (interactive) (my-digit-argument-wrapper 5)))
(define-key map (kbd "6") '(lambda () (interactive) (my-digit-argument-wrapper 6)))
(define-key map (kbd "7") '(lambda () (interactive) (my-digit-argument-wrapper 7)))
(define-key map (kbd "8") '(lambda () (interactive) (my-digit-argument-wrapper 8)))
(define-key map (kbd "9") '(lambda () (interactive) (my-digit-argument-wrapper 9)))
(define-key map (kbd "0") '(lambda () (interactive) (my-digit-argument-wrapper 0)))
map)
"Transient map for word, character, and line movement.")
(defun activate-my-movement-map ()
"Activate the movement transient map."
(interactive)
(set-transient-map my-movement-transient-map t))
(global-set-key (kbd "C-f") 'activate-my-movement-map)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment