1

I want to provide additional command line options to a command in dired marked with '!', eg how do I add '-V' to something like 'less'?

9
  • 1
    You just type the command (including options) at the prompt.
    – NickD
    Oct 28, 2023 at 15:48
  • What @NickD said. C-h k ! in Dired.
    – Drew
    Oct 28, 2023 at 18:31
  • So, if I mark 'less' with a '!', then I have to type in 'less -V' at the prompt and not just '-V' ?!!! Doesn't sound right. I could mark 'less' with a '!' and type in 'gzip -V' at the prompt. I'm obviously missing something....
    – balanga
    Oct 28, 2023 at 18:41
  • Bring up a dired buffer, navigate to some text file using the arrow key and press !. That brings up a prompt that says: "! on <filename>: " where <filename> is whatever filename your cursor was on when you pressed !. Then you type in whatever command you want to run on that file, e.g. "less -S". The point is that you don't have to type the filename: you just type the command (including options).
    – NickD
    Oct 28, 2023 at 20:48
  • What I'm looking for is a way to the run the file I selected with parameters, not to run a file on the selected file. Looks like I didn't phrase my question adequately, or misunderstood the way that dired-do-shell-command works. Maybe I can use 'w' with dired, which copies the filename into the mini-buffer, but is there any way to invoke a shell with that file name in it? I guess I'm looking for shortcut for is 'w' 'shell-command' 'c-y' " " parms.... Maybe there is such an option, but I haven't located it yet.
    – balanga
    Oct 28, 2023 at 22:03

1 Answer 1

1

In Dired, you can use dired-file-name-at-point to get the name of the executable at point as a string. Then you can prompt for additionnal arguments and build the command. Here is an example.

(require 'dired)

(defun execute-file-at-point ()
  "Execute the file at point.

The user may provide additionnal arguments."
  (interactive)
  (let* ((exec-file (dired-file-name-at-point))
         (command-base (concat "./" (file-name-nondirectory exec-file) " "))
         (command (read-string "Additionnal arguments : " command-base)))
    (shell-command command)))

You can bind it to a key in dired-mode using :

(define-key dired-mode-map (kbd "e") 'execute-file-at-point)
5
  • This is what I get in the minibuffer... Wrong type argument: commandp, execute-file-at-point
    – balanga
    Nov 1, 2023 at 17:38
  • @balanga Did you evaluate the defun to make sure execute-file-name-at-point is defined ? Nov 1, 2023 at 21:20
  • I copied and pasted the above block, asis, into my early-init.el. I'll try again with a blank file and just copy that block to see what happens.
    – balanga
    Nov 2, 2023 at 10:20
  • Pressing C-h k show that the command execute-file-at-point should be run, but I get an error - Symbol’s function definition is void: execute-file-at-point
    – balanga
    Nov 2, 2023 at 10:28
  • It looks like you did not evaluate the defun. Pasting code in a file does not execute it. Place the code in the *scratch* buffer, then run M-x eval-buffer. This way everything should be properly defined. Notice that I added (require 'dired). Nov 2, 2023 at 10:48

Your Answer

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

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