4

To run a program in bash, I normally use relative paths because it's faster to type; for example, something like

me@host:~/dir/appX$  ./manage.py runserver

The command will then be stored in the history. To recall the command from history (CTRL+R normally), I need to be on the same path as when I ran it, making 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 built in) to insert the current path in the command line? Or maybe a better solution (should work on bash)?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
Maximiliano Padulo
  • 1,452
  • 11
  • 22
  • Only works as part of an argument, but not at the beginning of the command. And also only if I do ${PWD} and then TAB. Do you experience the same in your environment? – Maximiliano Padulo Jan 08 '15 at 16:23
  • possible duplicate of [Saving current directory to bash history](http://stackoverflow.com/questions/945288/saving-current-directory-to-bash-history) (you may have to slightly change the function) – fredtantini Jan 08 '15 at 16:23
  • @fredtantini not really. The one you relate is more for traceability purposes and won't actually improve the workflow as I'm looking for in the question. – Maximiliano Padulo Jan 08 '15 at 16:27
  • Indeed, I missed the Ctrl+R part… – fredtantini Jan 08 '15 at 16:29
  • `mkdir ~/bin; ln -s /home/me/dir/appX/manage.py ~/bin/manage-appX.py; PATH=${HOME}/bin:$PATH; manage-appX.py runserver` – twalberg Jan 08 '15 at 17:30
  • @twalberg that will be good only for particular situations, and you have to know them in advance. A different usecase. And I would use an alias – Maximiliano Padulo Jan 08 '15 at 20:55
  • @mxlian An alias would be fine as well, as long as there's only a couple apps to be concerned with. My personal preference would be maintaining a bunch of symlinks (which can be scripted) though, instead of a bunch of aliases, if the number of different instances gets too high... – twalberg Jan 08 '15 at 21:00
  • @twalberg what do you mean with 'can be scripted'? sounds maybe interesting... I got a lot of aliases in .bash_aliases, but btw this is offtopic (just got curious of your workflow) – Maximiliano Padulo Jan 08 '15 at 21:54
  • 1
    @mxlian e.g. if the OP has `/home/me/dir//manage.py` for a collection of different values of ``, then a simple `for` loop can build all the symlinks, and scripts to get rid of stale ones or whatever wouldn't be too difficult, either. Usage will vary depending on sys admin habits, though... The point was that you can write a script (which could even be considerably complex) to do symlink maintenance for you. You could in theory manage aliases in the same way, but if you manage to mangle your `.bashrc` via script or whatever, it can be painful... – twalberg Jan 08 '15 at 22:07

1 Answers1

4

You can do this in bash using Tilde Expansion. You need two tilde expansion related features, just 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 it says, you can type ~+ to get the current path. And then to expand it you need to type M-&. So the key sequence ~+M-& is all you need.

I found it a little difficult pressing all these keys, so I created a key binding for this. Add a line like below in your ~/.inputrc file:

"\C-a":"~+\e&"

With this I can now type ctrl+a on my keyboard to get the current path on the command line.

PS: It's possible that ctrl+a is already bound to something else (perhaps beginning of line) in which case it might be better to use another key combination. Use bind -p to check current bindings.

Vivek
  • 2,000
  • 3
  • 16
  • 22
  • you are awesome! The inputrc trick was crucial, because the M-& secuence is already in use by my WM, so it never gets to the terminal. By the way mapping C-a gets in the way with the useful moving commands. I ended up mapping C-v (quoted-insert), which is redundant with C-q loosing almost no functionality – Maximiliano Padulo Jan 09 '15 at 09:56
  • 1
    Glad it worked for you :) I did check the key bindings (`bind -p`) before using ctrl+a , but it wasn't already being used. – Vivek Jan 09 '15 at 11:24
  • Bind was also very useful. Great. I got bind -p | grep "C-a" `"\C-a": beginning-of-line` – Maximiliano Padulo Jan 09 '15 at 13:33
  • Note to those who use macOS for example: The `M` in `M-&` is the meta key. In Terminal.app this has to be separately enabled under Preferences -> Profiles -> Keyboard -> Use Option as Meta key – equinoxe5 Mar 28 '21 at 15:26