-rw-rw-r-- 1 bs bs 20598 Jul 28 14:55 I-am-a-file.txt
                                              ^(cursor here)

A magic shortcut key is pressed, then:

-rw-rw-r-- 1 bs bs 20598 Jul 28 14:55 I-am-a-file.txt
                                      ^(cursor here)

Does this magic shortcut key exist?



Does the magic shortcut key exist?

The closest you can get without writing your own command is by setting the user option wdired-use-dired-vertical-movement to a non-nil value:

wdired-use-dired-vertical-movement is a variable defined in ‘wdired.el’.
Its value is nil

If t, the "up" and "down" movement works as in Dired mode.
That is, always move the point to the beginning of the filename at line.

If ‘sometimes’, only move to the beginning of filename if the point is
before it, and ‘track-eol’ is non-nil.  This behavior is very handy
when editing several filenames.

If nil, "up" and "down" movement is done as in any other buffer.

You can customize this variable.

Then you can, for example, M-0C-n to move to the beginning of the current file name.

If you don't want to permanently change the value of this user option, or if its sometimes setting isn't convenient enough for this purpose, then you can write a simple wrapper command:

(defun my-wdired-move-bof (n)
  "Move point to the beginning of the current WDired file name.
If numeric prefix argument N is not 1, move forward N - 1 lines
first.  This is for consistency with `move-beginning-of-line',
which see."
  (interactive "^p")
  (let ((wdired-use-dired-vertical-movement t))
    (wdired-next-line (1- n))))

and then bind it to some "magic shortcut" key, such as M-a (overrides backward-sentence, which isn't very useful in WDired) or C-ca (reserved for users):

(with-eval-after-load 'wdired
  (define-key wdired-mode-map (kbd "C-c a") #'my-wdired-move-bof))


As @Omar correctly pointed out in a comment, my-wdired-move-bof can be written more simply if you do not care about handling a numeric prefix argument, i.e. if you only ever want to move to the beginning of a file name on the current line:

(defun my-wdired-move-bof ()
  "Move point to the beginning of the current WDired file name."

As you can see, this is just wrapping the dired.el non-interactive function dired-move-to-filename as an interactive command. This way you can also pass a non-nil argument to dired-move-to-filename so that it displays an error message when something goes wrong, e.g. when invoked on a non-file line.

  • There is a (non-interactive) function called dired-move-to-filename, so my-wdired-move-bof could (without the numeric argument handling) be implemented as simply (dired-move-to-filename). – Omar Jul 31 '18 at 3:19
  • @Omar I'm aware of dired-move-to-filename, as that is what wdired-next-line is implemented in terms of. I deliberately used wdired-next-line in my sample command for several reasons: 1) dired-move-to-filename is, as you say, a non-interactive function from a different library, whereas wdired-next-line is an interactive command with a default key binding from the same library. This makes the latter a part of the user-facing API and thus far less likely to be broken backward-incompatibly. – Basil Jul 31 '18 at 20:25
  • @Omar 2) The numeric argument handling of both vanilla wdired-next-line and the sample my-wdired-move-bof is trivial and makes both commands strictly more general and thus potentially more convenient than plain dired-move-to-filename. 3) Since my-wdired-move-bof is a very thin wrapper around wdired-next-line, which in turn is a very thin wrapper around dired-move-to-filename, I saw no benefit in "simplifying" my-wdired-move-bof by rewriting a less general wdired-next-line. YMMV. – Basil Jul 31 '18 at 20:28
  • I agree with points 2 and 3 (as for 1, I think dired is pretty stable and it's already a dependency of wdired, so...). I just wanted to offer an easier to understand implementation of the basic functionality. – Omar Jul 31 '18 at 21:45
  • 1
    @Omar Indeed, thanks, that's why I updated the answer to mention your suggestion. Re: Dired's stability - if you follow emacs-devel you'll know that things can often change from version to version with various levels of subtlety. dired-move-to-filename shows signs of not being intended for external use, such as undocumented, non-intuitive, and non-interactive arguments, whereas wdired-next-line is not only in the subject library, but is as stable a bet as you can get. – Basil Jul 31 '18 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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