Bash – How to expand a relative path at the command line, with tab completion

autocompletebashcommand historycommand line

In bash is there any way to expand a relative path into an absolute path, perhaps with tab completion?

I ask because sometimes I will search for a file, find it, then vim it:

cd /etc
cd apache2
cd sites-available
vim 000-default

Then I will do some other things and change directories:

cd /tmp
ls
cd ~
ls

Then I'll find that I need to edit the same file again, but it is not possible to use history, (or history search), because the file path is relative.

Current Workaround

If I know I'm going to be reusing a file a lot, after I find it I will grab the full path using something like realpath:

$ realpath 000-default
/etc/apache2/sites-available/000-default
(copy and paste path into next command:)
$ vi /etc/apache2/sites-available/000-default

Caveats

This still requires copying and pasting. I believe there should be an easier way to get a file's full path into my history. What options do I have?

I believe that I have (accidentally) pressed some key combination before that expanded a relative path into a full path. I know in bash you can type:

cd /etc
ls bash_*(control+x *)

and get:

ls bash.bashrc bash_completion bash_completion.d

So is there a way I can expand a relative path into an absolute path, so that I can save operations on a file in my history with that file's full path?

Best Answer

I can't find a good way to do that.

What I do is type $PWD before the file name, then press Tab to expand it. In bash you may need to press Ctrl+Alt+e instead of Tab.

e.g.

vi $PWD/000-default

then

Ctrl+Alt+e

then

Enter

Related Question