Bash command to copy before cursor and paste after?
There are a number of tricks (there's a duplicate to be found I think), but for this I tend to do
cp /etc/prog/dir1/myconfig.yml{,.bak}
which gets expanded to your command.
This is known as brace expansion. In the form used here, the {}
expression specifies a number of strings separated by commas. These "expand" the whole /etc/prog/dir1/myconfig.yml{,.bak}
expression, replacing the {}
part with each string in turn: the empty string, giving /etc/prog/dir1/myconfig.yml
, and then .bak
, giving /etc/prog/dir1/myconfig.yml.bak
. The result is
cp /etc/prog/dir1/myconfig.yml /etc/prog/dir1/myconfig.yml.bak
These expressions can be nested:
echo a{b,c,d{e,f,g}}
produces
ab ac ade adf adg
There's a variant using numbers to produce sequences:
echo {1..10}
produces
1 2 3 4 5 6 7 8 9 10
and you can also specify the step:
echo {0..10..5}
produces
0 5 10
History expansion can be useful for this kind of thing (assuming bash history is enabled).
In your example, you could do:
cp /etc/prog/dir1/myconfig.yml !#:1.bak
Here, the !#
refers to the current line, !#:1
refers to parameter 1 on the current line,
and !#:1.bak
refers to parameter 1 on the current line with '.bak' tacked onto the end.
When you have the history specifier typed out, you can use Ctrl+Alt+E to expand it to its actual value if you want to e.g. double check or modify the filename.
The "History Expansion" section in the bash man pages has more info.
Similar to progo's answer, but somewhat simpler: C-w
cuts (“kills”) the word to the left of the cursor, C-y
inserts (“yanks”) it again.
$ cp /etc/dir1/myconfig.yml█ (C-w)
$ cp █ (C-y)
$ cp /etc/dir1/myconfig.yml█ (space, C-y)
$ cp /etc/dir1/myconfig.yml /etc/dir1/myconfig.yml█ (finish text)
$ cp /etc/dir1/myconfig.yml /etc/dir1/myconfig.yml.bak█