insert-file-name-into-minibuffer

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

insert-file-name-into-minibuffer

Alexander Klimov
It is quite common to insert the name of the current file into the
argument of shell-command or compile command (both use
read-shell-command). I wonder what it the preferred way to do it?

I currently use the following to allow M-. or C-u M-. to be used to
insert the file name or its full path:

(defun insert-file-name-into-minibuffer (full-path)
  (interactive "P")
  (let ((path (buffer-file-name (window-buffer (minibuffer-selected-window)))))
    (insert (if full-path
                path
              (file-name-nondirectory path)))))
(define-key minibuffer-local-map (kbd "M-.") 'insert-file-name-into-minibuffer)

--
Regards,
ASK

Reply | Threaded
Open this post in threaded view
|

Re: insert-file-name-into-minibuffer

Eli Zaretskii
> Date: Sat, 9 Feb 2019 14:39:36 +0000
> From: Alexander Klimov <[hidden email]>
>
> It is quite common to insert the name of the current file into the
> argument of shell-command or compile command (both use
> read-shell-command). I wonder what it the preferred way to do it?

Doesn't M-n in the minibuffer already do that?

Reply | Threaded
Open this post in threaded view
|

Re: insert-file-name-into-minibuffer

Alexander Klimov
On Sat, 9 Feb 2019, Eli Zaretskii wrote:
> > It is quite common to insert the name of the current file into the
> > argument of shell-command or compile command (both use
> > read-shell-command). I wonder what it the preferred way to do it?
>
> Doesn't M-n in the minibuffer already do that?

Thanks, never knew it works in read-shell-command this way!

OTOH, next-history-element replaces the whole line even if one
already typed `gcc ...' and does not allow to insert the file name
with full path.

Speaking of buffer file names, probably there should also be a more
direct way to copy the full path of the current file than C-x C-f M-n
C-S-backspace.

--
Regards,
ASK

Reply | Threaded
Open this post in threaded view
|

Re: insert-file-name-into-minibuffer

Marcin Borkowski-3

On 2019-02-09, at 22:30, Alexander Klimov <[hidden email]> wrote:

> On Sat, 9 Feb 2019, Eli Zaretskii wrote:
>> > It is quite common to insert the name of the current file into the
>> > argument of shell-command or compile command (both use
>> > read-shell-command). I wonder what it the preferred way to do it?
>>
>> Doesn't M-n in the minibuffer already do that?
>
> Thanks, never knew it works in read-shell-command this way!
>
> OTOH, next-history-element replaces the whole line even if one
> already typed `gcc ...' and does not allow to insert the file name
> with full path.
>
> Speaking of buffer file names, probably there should also be a more
> direct way to copy the full path of the current file than C-x C-f M-n
> C-S-backspace.

Not entirely what you're looking for, but are you aware of `w', `C-u w'
and `C-u 0 w' in Dired?

Hth,

--
Marcin Borkowski
http://mbork.pl

Reply | Threaded
Open this post in threaded view
|

Re: insert-file-name-into-minibuffer

Alexander Klimov
On Sat, 9 Feb 2019, Marcin Borkowski wrote:
> > Speaking of buffer file names, probably there should also be a more
> > direct way to copy the full path of the current file than C-x C-f M-n
> > C-S-backspace.
>
> Not entirely what you're looking for, but are you aware of `w', `C-u w'
> and `C-u 0 w' in Dired?

Yes (M-0 w), but opening Dired (BTW, dired-jump is not bound to C-x
C-j in `emacs -Q') is likely even less direct.

--
Regards,
ASK

Reply | Threaded
Open this post in threaded view
|

Re: insert-file-name-into-minibuffer

Marcin Borkowski-3

On 2019-02-09, at 22:50, Alexander Klimov <[hidden email]> wrote:

> On Sat, 9 Feb 2019, Marcin Borkowski wrote:
>> > Speaking of buffer file names, probably there should also be a more
>> > direct way to copy the full path of the current file than C-x C-f M-n
>> > C-S-backspace.
>>
>> Not entirely what you're looking for, but are you aware of `w', `C-u w'
>> and `C-u 0 w' in Dired?
>
> Yes (M-0 w), but opening Dired (BTW, dired-jump is not bound to C-x
> C-j in `emacs -Q') is likely even less direct.

Of course, but I thought it might be useful for others reading this.
(And BTW, I fully agree that your use case is legitimate and not covered
by M-n.  Otoh, I often run shell commands from Dired, so it's not that
indirect for me.)

Best,

--
Marcin Borkowski
http://mbork.pl

Reply | Threaded
Open this post in threaded view
|

Re: insert-file-name-into-minibuffer

Alexander Klimov
On Sat, 9 Feb 2019, Marcin Borkowski wrote:
> > Yes (M-0 w), but opening Dired (BTW, dired-jump is not bound to C-x
> > C-j in `emacs -Q') is likely even less direct.
>
> Of course, but I thought it might be useful for others reading this.

I use

(defun copy-buffer-file-name-as-kill (&optional nondirectory)
  (interactive "P")
  (let ((f (buffer-file-name)))
    (when nondirectory
      (setq f (file-name-nondirectory f)))
    (kill-new f)
    (message "%s" f)))

IMO it would be nice to have something like this bound to some key in
`emacs -Q'; as well as something like insert-file-name-into-minibuffer
because it is common to recall that you need the buffer file name only
once you already started typing the compilation command. Furthermore,
M-n does not insert buffer file name in M-x compile.

--
Regards,
ASK

Reply | Threaded
Open this post in threaded view
|

Re: insert-file-name-into-minibuffer

Marcin Borkowski-3

On 2019-02-09, at 23:19, Alexander Klimov <[hidden email]> wrote:

> On Sat, 9 Feb 2019, Marcin Borkowski wrote:
>> > Yes (M-0 w), but opening Dired (BTW, dired-jump is not bound to C-x
>> > C-j in `emacs -Q') is likely even less direct.
>>
>> Of course, but I thought it might be useful for others reading this.
>
> I use
>
> (defun copy-buffer-file-name-as-kill (&optional nondirectory)
>   (interactive "P")
>   (let ((f (buffer-file-name)))
>     (when nondirectory
>       (setq f (file-name-nondirectory f)))
>     (kill-new f)
>     (message "%s" f)))

Nice, and really useful!  I'm going to put it in my init.el, thanks.

> IMO it would be nice to have something like this bound to some key in
> `emacs -Q'; as well as something like insert-file-name-into-minibuffer
> because it is common to recall that you need the buffer file name only
> once you already started typing the compilation command. Furthermore,
> M-n does not insert buffer file name in M-x compile.

+1

--
Marcin Borkowski
http://mbork.pl

Reply | Threaded
Open this post in threaded view
|

Re: insert-file-name-into-minibuffer

Marcin Borkowski-3
In reply to this post by Alexander Klimov

On 2019-02-09, at 15:39, Alexander Klimov <[hidden email]> wrote:

> It is quite common to insert the name of the current file into the
> argument of shell-command or compile command (both use
> read-shell-command). I wonder what it the preferred way to do it?
>
> I currently use the following to allow M-. or C-u M-. to be used to
> insert the file name or its full path:
>
> (defun insert-file-name-into-minibuffer (full-path)
>   (interactive "P")
>   (let ((path (buffer-file-name (window-buffer (minibuffer-selected-window)))))
>     (insert (if full-path
>                 path
>               (file-name-nondirectory path)))))
> (define-key minibuffer-local-map (kbd "M-.") 'insert-file-name-into-minibuffer)

Hi, it seems I reinvented your code and written about it on my blog:
http://mbork.pl/2019-02-17_Inserting_the_current_file_name_at_point
(don't worry, you get the credit).  Thanks again!

Best,

--
Marcin Borkowski
http://mbork.pl