Skip to content

Instantly share code, notes, and snippets.

@NanoDano
Created June 3, 2020 21:08
Show Gist options
  • Select an option

  • Save NanoDano/935b4baba57e2f3bd4448db79d217b41 to your computer and use it in GitHub Desktop.

Select an option

Save NanoDano/935b4baba57e2f3bd4448db79d217b41 to your computer and use it in GitHub Desktop.
Bash history tips

Bash history tips

Run the last command again

!!

Check history

history

Run specific history command

!319

Repeat the last command but replace something

^searchString^replaceString^

or

!!:s/searchString/replaceString/

or search the whole command instead of first occurrence

!!:gs/old/new/

History tricks

make a directory then move into it:

mkdir -p $HOME/temp/dir
cd !$ 

!$ is shorthand for "the last word from the previous command". If I wanted to pick the third word out of the previous command, that would be: !!:3 (don't forget there is a zeroth word).

For example:

# Print first word (the command) of the last command
echo !!:0

Run the last command that contained string

Easily re-run the last command that contains a certain word:

!?restart
!?tail

Search history with CTRL-R

Press CTRL-R to search history with dynamic autocomplete

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment