6
$ ln -s ./dir/target.txt /path/to/source.txt{cursor here}

How do swap file typed filenames in command line? Alt+T swaps words, not filenames, resulting in ln -s ./dir/target.txt /path/to/txt.source.

Ideally the solution should also interpret "filenames with quotes.txt" correctly.

Can it be done by some readline and/or bash configuration, or by some clever use of existing shortcuts?

Vi.
  • 5,688

2 Answers2

7

Partial answer based on this. {} denotes cursor:

$ ln -s ./dir/target.txt /path/to/source.txt{}

Ctrl+W (repeat this if once is not enough, but don't Backspace)

$ ln -s ./dir/target.txt {}

Ctrl+{,,,}, ,

$ ln -s {}./dir/target.txt 

Ctrl+Y, Space

$ ln -s /path/to/source.txt. {}/dir/target.txt

Ready.

Vi.
  • 5,688
  • Based on your answer and the link you provide, I've just learned a whole heap more that I didn't know about bash :) Where does one find these gems? – Madivad Mar 31 '16 at 02:19
  • Easier way to get to before the first path: ctrl-a (start of line), ctrl-right or alt-f twice (forward word). – Peter Cordes Jan 10 '20 at 19:59
5

I think this can be done using bang commands:

!:0 !:2 !:1

!:0 word designator 0, the zeroth word.
!:n The nth word.


man bash

Word designators, section.

  • Only giving a code snippet is not enough without explanation. – peterh Mar 01 '15 at 23:08
  • I type !:0 !:2 !:1 after /path/to/source.txt, but nothing happens. Obviously, I don't want to execute the incorrect command line. – Vi. Mar 02 '15 at 00:20
  • Word Designators Word designators are used to select desired words from the event. -> Events? Is it that annoying (bash: !": event not found) thing that gets into my way and drops my command line when I forgot to escape !? – Vi. Mar 02 '15 at 00:21