Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/Kungsgeten/selected.el

Keymap for when region is active
https://github.com/Kungsgeten/selected.el

Last synced: 3 months ago
JSON representation

Keymap for when region is active

Awesome Lists containing this project

README

        

#+TITLE:selected.el [[http://melpa.org/#/selected][file:http://melpa.org/packages/selected-badge.svg]]

=selected.el= provides the =selected-minor-mode= for Emacs. When =selected-minor-mode= is active, the keybindings in =selected-keymap= will be enabled when the region is active. This is useful for commands that operates on the region, which you only want bound to a key when the region is active. =selected.el= also provides =selected-global-mode=, if you want =selected-minor-mode= in every buffer.

=selected-keymap= has no default bindings. Bind it yourself:

#+BEGIN_SRC emacs-lisp
(require 'selected)
(define-key selected-keymap (kbd "q") #'selected-off)
(define-key selected-keymap (kbd "u") #'upcase-region)
(define-key selected-keymap (kbd "d") #'downcase-region)
(define-key selected-keymap (kbd "w") #'count-words-region)
(define-key selected-keymap (kbd "m") #'apply-macro-to-region-lines)
#+END_SRC

It is cleaner with [[https://github.com/jwiegley/use-package][use-package]]:

#+BEGIN_SRC emacs-lisp
(use-package selected
:ensure t
:commands selected-minor-mode
:bind (:map selected-keymap
("q" . selected-off)
("u" . upcase-region)
("d" . downcase-region)
("w" . count-words-region)
("m" . apply-macro-to-region-lines)))
#+END_SRC

Then activate it with =M-x selected-minor-mode=. It can be a good idea to add =selected-minor-mode= to the hooks of the major-modes where you want it activated. =selected-off= is a function which deactivates the keybindings until the next time the region becomes active. This is useful when you want to execute a command on the region, but then disable =selected-minor-mode= in favour of other commands like [[https://github.com/magnars/multiple-cursors.el][multiple-cursors]].

You might have other minor-modes which conflict with =selected=, such as [[https://github.com/abo-abo/worf][worf]], [[https://github.com/abo-abo/lispy][lispy]] or [[https://github.com/Kungsgeten/ryo-modal][ryo]]. In that case you can use =(setq selected-minor-mode-override t)=.

* Major mode specific bindings

You may want some keybindings to operate on the region, but only if you're in a certain major-mode. This is possible if you create a keymap named =selected--map= (for instance =selected-org-mode-map=) and add the mode-specific bindings there. Here's an example, using =use-package=:

#+BEGIN_SRC emacs-lisp
(use-package selected
:ensure t
:commands selected-minor-mode
:init
(setq selected-org-mode-map (make-sparse-keymap))
:bind (:map selected-keymap
("q" . selected-off)
("u" . upcase-region)
("d" . downcase-region)
("w" . count-words-region)
("m" . apply-macro-to-region-lines)
:map selected-org-mode-map
("t" . org-table-convert-region)))
#+END_SRC

If the major-mode specific keymap isn't found, derived from modes will be searched. For instance if you open an =org-mode= file, don't have =selected-org-mode-map= but do have a =selected-text-mode-map= then =selected-text-mode-map= will be used.

* Installation and setup

=selected.el= is on [[https://melpa.org/][MELPA]], so the easiest way is to install it from there using =M-x package-install=. MELPA needs to be in your =package-archives=:

#+BEGIN_SRC emacs-lisp
(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/") t)
#+END_SRC

If you add =(selected-global-mode)= to your init-file, or if you add =selected-minor-mode= to a hook, please do so towards the end of your init-file. This is in order to prioritize the bindings in =selected-keymap= over other minor-modes.
* Recommended resources

Here's some functions and packages that are useful with =selected.el=:

- [[https://github.com/magnars/expand-region.el][expand-region]]
- [[https://github.com/magnars/multiple-cursors.el][multiple-cursors]]
- [[https://www.emacswiki.org/emacs/move-text.el][move-text]]
- [[https://www.emacswiki.org/emacs/RandomizeBuffer][randomize-region]]
- [[https://www.emacswiki.org/emacs/HighlightLibrary][highlight]]
- [[https://github.com/Malabarba/emacs-google-this][google-this]] or [[https://github.com/xahlee/lookup-word-on-internet][lookup-word-on-internet]]

Feel free to suggest additions to this list!

* Changelog

- 1.01 :: Added =selected-global-mode=.
- 1.02 :: Derived modes now searches up the hierarchy for =selected--map=.