4

I have a idea to mark regions in emacs easier.

  1. I press C-SPC to start.

  2. I use a vi style key to extend selection. such as

"j" : line down
"k": line up

instead of using arrow key or C-n, C-p, a singe char is easier to press

  1. When finish selecting, I choose a key to do some thing, also use a vi style key

    "c": deactive region, copy region. "d" delete region "#" comment region "space" just leaving without do anything

I know I can use "M-w" "M-k" or something to do it, but I think vi style key is a easier way to do the job.

I search everywhere, but there is no elip package can do such thing.

Can someone help me to write some functions to do it? Or give me some suggestions.

I found a nice way to do it, share the solution:

(

defvar active-region-mode-map
  (let ((map (make-sparse-keymap)))
    map)
  )

(define-minor-mode active-region-mode
  "Active Region minor mode."
  :init-value nil
  :lighter " Region"
  :keymap active-region-mode-map
  :group 'active-region
  )

(defun active-region-on ()
  (active-region-mode 1))
(defun active-region-off ()
  (active-region-mode -1))
(add-hook 'activate-mark-hook 'active-region-on)
(add-hook 'deactivate-mark-hook 'active-region-off)

Now, enjoy it, "active-region-mode-map" map keybinding you like. For example:

(define-key active-region-mode-map (kbd "j") 'next-line)
Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
textpattern
  • 397
  • 3
  • 14
  • I don't know enough elisp/have enough time to do this myself, but: You can probably rebind j/k/c/d/# from their "self-insertion" code to some custom code to check if the mark is active. If it is, do what you describe, if it isn't, call self-insert. – drysdam Apr 13 '11 at 15:12
  • I will try it. I think some can write a region-mode-map. Such code can make me easier to define keybinding. – textpattern Apr 13 '11 at 15:31
  • Yeah, that's probably a good idea. Automate that "rebind self-insert" thing and then it's easy. – drysdam Apr 13 '11 at 16:00
  • The region has more uses than just for commenting, deleting etc. The region restricts the area of operation of many commands. e.g. "C-M-h C-x n n" to narrow to the current function etc. While your project is interesting, I think your productivity with Emacs will improve if you master it's own paradigms rather than graft stuff from vim. – Noufal Ibrahim Apr 15 '11 at 05:07
  • Thank you! This code is great! I use it for move-text.el, multiple-cursors.el and eval-code-in-region... Really saves me a lot of time to figure out the key-binding stuff, or maybe even a keyboard : ). – wenjun.yan Apr 05 '13 at 15:47

1 Answers1

0

You can have a look at the viper-mode.

Bozhidar Batsov
  • 55,802
  • 13
  • 100
  • 117
  • I know viper-mode, but my idea is a easier way to do this in emacs. Inside a region, you can use a single key instead of combo key. It is not a edit mode, just like dired mode or ibuffer mode. – textpattern Apr 13 '11 at 15:21
  • 1
    I don't think that this is feasible, unless you're willing to define a whole mode just for this functionality... Single key are by default bound to self-insert and getting around that is a bit tricky, not to mention that it violates the Emacs core keybinding principles :-) – Bozhidar Batsov Apr 13 '11 at 15:32
  • But in some mode , you can use this kind of key binding. Dired mode, "n" "p" instead of " c-n" "c-p" Inside a region, you can use such key binding too. I wish there is a region-mode-map. – textpattern Apr 13 '11 at 15:39
  • 1
    Since the there is no editing in such modes they have clearly bound the keys to something different than self-insert. You could potentially add some advice around mark-region(or whatever it was called). The advice is something like aspect oriented programming - it allows to invoke code around other code. Wich such an advice you could temporarily rebind your keys for the duration of select region and restore the old-bindings afterwards. have a look at defadvice in the Emacs Lisp manual. – Bozhidar Batsov Apr 13 '11 at 15:53
  • 1
    I just solve the problem, you can write a simple minor mode, named as active-region, and write a active-region-mode-map. with this mode, you can define any keybing in region. Overridng a default key binding is very easy (M-w to something else). I found the answer in google, but your comment give me good advice. Thank you very much. – textpattern Apr 13 '11 at 16:05
  • @textpattern: Why don't you write up your solution and submit it as an answer to your question? – Shannon Severance Apr 13 '11 at 20:16
  • Because it is just too simple. There is a internal hook named: activate-mark-hook, with this hook, you can define keybinding or do something in region. I will update my post, put the answer in it. – textpattern Apr 13 '11 at 23:06