Assuming bash
or compatible, you can copy the command from your history to the clipboard after running it:
$ fc -lrn | head -1 | xclip -se c
If you don't want to run it before copying it to the clipboard, jump to the front with Ctrl+A and temporarily turn it into a comment by adding #
.
Having an alias for the command line above is handy when recording command line procedures for later reference or documentation. However, it's even more convenient to just append the last command directly to a file as needed by making a command like this (c2f
=command to file
):
$ c2f() { fc -lrn | head -1 >> "${1?}"; }
Usage:
$ cat <(echo '!! example command that "includes chars" with shell > semantics, etc') | cat >&2
$ c2f readme.md
readme.md
now contains:
...
cat <(echo '!! example command that "includes chars" with shell > semantics, etc') | cat >&2
To make it quicker to edit the file later, it can also be convenient to slightly expand on the above and add some appropriate syntax wrapping the command being append to the file, such as ```bash...```
.