At the bash prompt the command history can be explored using the up and down arrow keys. (I also often use CTRL+R.) Is it possible to do this without replacing the character string to the left of the cursor.

For example, I may have found two files using ls:

$ ls ../../blah/foo/bar/CMakeLists.txt

$ ls /usr/include/c++/4.7.2/armv7l-unknown-linux-gnueabihf/bits/c++config.h

I'd now like to shunt the second path into the first file. I begin by pressing the up arrow; then CTRL+A; and removing ls from the start using delete:

$ /usr/include/c++/4.7.2/armv7l-unknown-linux-gnueabihf/bits/c++config.h

I'd now like to use >> to append this path to the CMakeLists.txt file; which I will later edit from within a text editor. The command I'd like to create is:

$ echo /usr/include/c++/4.7.2/armv7l-unknown-linux-gnueabihf/bits/c++config.h >> ../../blah/foo/bar/CMakeLists.txt

It would be nice if I could scroll interactively through the history to locate the CMakeLists.txt path while leaving the .../c++config.h part untouched.

One possibility would be to use !! and !-N event designators, but if possible I'd much prefer something interactive; such as the up and down arrows; or CTRL+R.

2 Answers 2


(I'll use the key nomenclature used in the Readline documentation, namely that the Control key is represented by a C, so C-k is CTRL-K)

Find the first one with C-r, then C-d or Del 3 times to delete the ls. Then C-k to kill (cut) the rest of the line. Then C-r to search for the other one. C-e to move to the end of the line. Type >>. Then C-y to yank (paste) in the first path.

So, C-k (kill) and C-y (yank) are probably your best tools for this job.

  • This is great: I didn't know about yank and kill.
    – user74094
    Jul 11, 2013 at 22:28

If you are familiar with vi, you can set bash to vi mode with set -o vi and then use regular vi commands.

For example in your case f.y$ to yank the path in the first command line, then go the the next line with j, type $p to append the previously yanked text at the end of the line and finally clean-up the line with 0cwecho<esc>f a>><esc>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.