Pages

Saturday, June 6, 2026

Follow-on to exeln

Sebastián Monía1 offered some alternatives to the exeln function that I posted on June 4.2

First, he pointed out that there's no need to bind asynch with the let function. Instead he suggested that I use (when arg "&") to append the ampersand when there's a prefix argument. Thus, shell-command can be called this way:

(shell-command (concat
                (buffer-substring
                 (line-beginning-position) (line-end-position))
                (when arg "&")))

Second, call async-shell-command rather than shell-command with the "&" as suggested in shell-command's doc string, which says, "You can also use async-shell-command that automatically adds '&'."3

Third, use thing-at-point from the built-in thingatpt.el library to return the line at point rather than calling buffer-substring.

(defun exeln (arg)
  "Execute current line as a shell command.
With prefix ARG, run asynchronously."
  (interactive "P")
  (funcall (if arg
               #'async-shell-command
             #'shell-command)
           (thing-at-point 'line t)))

And although he didn't mention it explicitly, he reworded the doc string -- it blends in seamlessly with the native functions' doc strings.

I set out to contribute to the Emacs community, but I'm getting so much more out of it than I expected! Thank you again, Sebastián Monía!


1 https://site.sebasmonia.com/
2 exeln-execute-line.html
3 GNU Emacs online shell-command help

No comments:

Post a Comment

Thank you for taking time to comment. Please note that comments may be deleted if they:

* Contain link(s) to a domain outside USA or Canada or to a commercial enterprise.
* Include non-English words or characters.
* Are irrelevant to the subject matter of the post.

For posts older than 14 days, comments are moderated and require approval.