Paste current path in bash command line
To run a program in bash, I usually use relative paths because it types faster; for example something like
me@host:~/dir/appX$ ./manage.py runserver
Then the command will be saved in history. To call a command from history (usually CTRL+ R), I need to be on the same path as when it was run, which makes the recall function less useful.
One solution is to insert the full path the first time, but it takes a lot of writing.
me@host:~/dir/appX$ /home/me/dir/appX/manage.py runserver
Is there a way (preferably inline) to insert the current path on the command line? Or maybe a better solution (should work in bash)?
source to share
You can do this in bash using the Tilde extension. You need two tilde expansion expansion options by simply showing the relevant parts from man bash
below:
Tilde Expansion If the tilde-prefix is a `~+', the value of the shell variable PWD replaces the tilde-prefix. tilde-expand (M-&) Perform tilde expansion on the current word.
As the saying goes, you can enter ~+
to get the current path. And then to expand it, you need to enter M-&
. Thus, the key sequence ~+M-&
is all you need.
I was having a hard time compressing all these keys, so I created a keybind for that. Add the line as shown below in your ~ / .inputrc file:
"\C-a":"~+\e&"
With that, I can now type ctrl+a
on my keyboard to get the current path on the command line.
PS: It is possible that it is ctrl+a
already tied to something else (maybe the beginning of the line), in which case it would be better to use a different keyboard shortcut. Use bind -p
to check the current bindings.
source to share