Understanding Bash History
symkat.com
Understanding Bash History
1–10 of 28 posts
Re: Understanding Bash History
#2Re: Understanding Bash History
#3Re: Understanding Bash History
#4Re: Understanding Bash History
#5Re: Understanding Bash History
#6^foo^bar replaces the foo in the last command with bar
example: less setup.conf, ^less^vim
alt+. recalls the last argument of the last command (in emacs mode, anyway)
example: less setup.conf, vim alt+.
As mentioned, Ctrl+R is great for history searching. Repeatedly pressing Ctrl+R goes to the next result, and you can use the arrow keys to edit.
cd - goes to the last directory. It handles most of the use cases for pushd and popd.
Re: Understanding Bash History
#7Mentioned in the comments, but another extremely useful feature is if you type ctrl+r and start typing a command, it will give you the last command you ran that contains that substring.
Re: Understanding Bash History
#8Also useful: you can append :s/foo/bar to bang commands to substitute once, :gs/foo/bar to substitute globally. `man bash` History Expansion section has the other options under subheading 'Modifiers'.
I highly recommend the following resource -- it's what really got me saving keystrokes:
Re: Understanding Bash History
#9Anyone know of a utility for saving annotations about the bash history lines? (Date, comments, current directory, etc.?)
Re: Understanding Bash History
#10I was sitting here between queries and I thought 'y'know, all I ever do with history is grep for things. I wonder what else I can do?' When I want to learn about something quick, I usually start with the built-in help; so I did
history --help
The built-in help for history, unfortunately, is woefully inadequate; this was the output:
history: usage: history [-c] [-d offset] [n] or history -awrn [filename] or history -ps arg [arg...]
I said to myself: "Hmm. Wonder what those do?" -- and in a moment of bash stupidity, I thought: "well, let's try it!"
history -c
There's no output from that command, but it didn't take long to figure out what it does. It deletes your entire bash history, clearing the history in RAM and emptying .bash_history.
Since I use history all the time, I panicked. Holy crap! What have I done? I spent a few minutes internalizing the obvious lesson that everybody knows - never run commands without knowing them first. Thankfully, though, I had another bash open at the time. And after I'd beaten myself up over it, I thought for a moment, jumped over to the other shell, and did:
cd && history | cut -c 8- >> .bash_history
... because (after all) the evil, evil history -c command doesn't kill the history from RAM in other shells.
Lucky me.