Is there any keyboard shortcut to print out the absolute path of a file while typing on the command line?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
magnetor1000
Level 2
Level 2
Posts: 85
Joined: Fri Apr 01, 2022 7:53 pm

Is there any keyboard shortcut to print out the absolute path of a file while typing on the command line?

Post by magnetor1000 »

I need a fast way to print out the absolute path to a file on the command line as I am typing.

Whenever I need to print out the absolute path of a file, what I am used to doing is first pwd, going back and copying that from the output and adding it to the filename on the command line.

I made a script as a workaround, for ln -s, I named it lns:

Code: Select all

#! /bin/bash

# applies absolute path to source file (TARGET) using only its short name when creating symbolic link

ln -s "$(realpath -e "$1")" "$2"
That only solves in one particular instance. Is there a keyboard shortcut (or can I create one) that allows me to have the command line print out the full absolute path while I am typing?

Similar to pressing tab to autocomplete a filename that bash recognizes is being typed out, but instead of it being done while it is being typed out, it is done retroactively, as in after I typed out the filename? Or maybe even while I am typing it out, that would work too.

Or is there any way to modify the behavior of tab key to behave in that way, when I specifically want it to?

If there is no built-in way to do this, is there perhaps a way I can define a keyboard shortcut as a script, and have that script echo out the realpath -e filename.txt live to the command line for the filename.txt immediately left to it?

And I think that this would be considered echoing it out to "standard input", correct? Since that would essentially be echoing it out live to the command line, which is standard input? (I just want to make sure that my conceptual understanding is accurate of the standard input/output in this context)
Last edited by LockBot on Mon Apr 10, 2023 10:00 pm, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
vimes666
Level 6
Level 6
Posts: 1229
Joined: Tue Jan 19, 2016 6:08 pm

Re: Is there any keyboard shortcut to print out the absolute path of a file while typing on the command line?

Post by vimes666 »

Define a new shortcut, give it some name and use this command:

Code: Select all

/bin/bash -c "windowid=$(xdotool getwindowfocus)  &&  sleep 0.5 && xdotool windowactivate --sync $windowid type $PWD"
You may have to install xdotool, I am not sure about that.
If you think the issue is solved, edit your original post and add the word solved to the title.
weedeater64
Level 1
Level 1
Posts: 44
Joined: Mon Jun 08, 2015 6:23 pm

Re: Is there any keyboard shortcut to print out the absolute path of a file while typing on the command line?

Post by weedeater64 »

Code: Select all

$ fzf | command
Self-Perfection
Level 1
Level 1
Posts: 2
Joined: Tue Oct 25, 2022 3:02 pm

Re: Is there any keyboard shortcut to print out the absolute path of a file while typing on the command line?

Post by Self-Perfection »

Which shell do you use? Zsh probably have something fancy for this.

As for bash I would suggest defining macro for readline, which will type

Code: Select all

"$( readlpath -e  
So with this macro you can place cursor before file name, press your hotkeys as defined in inputrc, and then just add

Code: Select all

)"
after file name that is just like 3 more keystrokes.
mikeflan
Level 17
Level 17
Posts: 7106
Joined: Sun Apr 26, 2020 9:28 am
Location: Houston, TX

Re: Is there any keyboard shortcut to print out the absolute path of a file while typing on the command line?

Post by mikeflan »

I need a fast way to print out the absolute path to a file on the command line as I am typing.
Not what you asked for, but note that if you highlight a file in Nemo and hit Ctrl-C you have the full path on the clipboard.
1000
Level 6
Level 6
Posts: 1039
Joined: Wed Jul 29, 2020 2:14 am

Re: Is there any keyboard shortcut to print out the absolute path of a file while typing on the command line?

Post by 1000 »

Is there any keyboard shortcut to print out the absolute path
It's very difficult for me to understand the topic, so please forgive me if I write stupid things

Shortcut or alias in bash ( you can build ).

Your " realpath -e "$1" " is cool because working only with files which exists.
For example

Code: Select all

$ echo $(realpath -e .bashrc)
/home/user/.bashrc
so it is better than " echo $(pwd)/File ".

You can also use mentioned above fzf
for example

Code: Select all

$ echo "$(locate bash | grep home | fzf)"
/home/user/.bashrc
If I want to go out from fzf, I can try Ctrl + C or q + Enter

It is a matter of convenience.
For example, For now ,it's more convenient for me is paste link manually,
I paste command, for example " cp /old/path/file /destination/path/ "
with Ctrl+W I remove the old path
and I paste new path. ( I thought it was Ctrl+C instead Shift+Ctrl+V :o )
and sometimes I build scripts.
User avatar
xenopeek
Level 25
Level 25
Posts: 29597
Joined: Wed Jul 06, 2011 3:58 am

Re: Is there any keyboard shortcut to print out the absolute path of a file while typing on the command line?

Post by xenopeek »

Bash uses readline for command line editing. Here's something using readline macros that looks I guess similar to what you want: https://stackoverflow.com/a/4120926. It's beyond my understanding but maybe the readline documentation will help (or not…): https://tiswww.case.edu/php/chet/readli ... umentation
Image
Locked

Return to “Scripts & Bash”