4

The default in dired is to mark files and folders with m, and to unmark them with u. Instead of this, I'd rather like to use shift-up and shift-down for marking or unmarking lines (in a DWIM fashion). So shift-up would mark/unmark the current line and move the cursor one line up.

This is nothing particularly fancy, I guess, and I would preferably use something already built in into dired or provided by some extra package. But I haven't come across something like this so far.

0

2 Answers 2

3

Dired has a function for toggling marks, t. However, it works on all files at once. We can use narrowing to restrict it to the current line only. With that feature sorted, the rest is fairly straightforward:

(defun my-dired-toggle (arg)
  (save-restriction
    (narrow-to-region (point-at-bol) (point-at-eol))
    (dired-toggle-marks))
  (dired-previous-line arg))

(defun my-dired-mark-up ()
  (interactive)
  (my-dired-toggle 1))

(defun my-dired-mark-down ()
  (interactive)
  (my-dired-toggle -1))

(define-key dired-mode-map (kbd "<S-up>") 'my-dired-mark-up)
(define-key dired-mode-map (kbd "<S-down>") 'my-dired-mark-down)
2
  • To use narrow-to-region and widen for every line sounds like overkill, but it's nevertheless working without noticeable slowdowns on my machine. Thanks!
    – Timm
    Feb 20, 2017 at 14:47
  • 3
    I don't know that narrow-to-region is particularly costly, and it saves us having to write our own toggle-mark function. Thinking about this some more, I think save-restriction is probably more appropriate here than save-excursion, and would allow us to remove the widen call.
    – Tyler
    Feb 20, 2017 at 14:59
4

If you use Dired+ then you get similar behavior out of the box.

  • Command diredp-mark-region-files marks the files in the active region. (Similarly, diredp-unmark-region-files unmarks them.)

  • And shift-selection with the cursor keys works to select text (active region).

Command diredp-mark-region-files is not bound to a key by default, but you can bind it. E.g., if you bind it to, say C-o then just hit C-o after using, say S-down....

There is a reason that simply using shift-selection (e.g. S-down...) does not, by itself, mark or unmark files: With Dired+ you can do lots of different things with the active region. So the act of selecting is separated from the various actions on the selected files.

Another way to mark files using the region is to drag the mouse (mouse-1). Just click mouse-3 to then see a whole bunch of things you can do to the files in the region, one of which is to mark them.

dired+.el

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.