├── LICENSE ├── README.md ├── Screenshots ├── example_1.png ├── example_2.png └── example_3.png ├── config.sh └── repeat_history.sh /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, 2016 Maurice Tollmien 2 | 3 | Permission to use, copy, modify, and/or distribute this software for any 4 | purpose with or without fee is hereby granted, provided that the above 5 | copyright notice and this permission notice appear in all copies. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Repeat History 2 | 3 | The script is meant to be a small addition for the linux bash. 4 | 5 | It makes the bash history more easily accessible than going through Cmd+R manually, by listing a number (default 20) of last 6 | commands. You can then just type the number of the command you would like to execute 7 | from the list. 8 | 9 | It especially helps to get an overview of slightly different commands and makes it easier to select the right one. 10 | 11 | ## Install 12 | 13 | Go to the cloned files and execute config.sh. When given no parameters, the script is given the system wide alias: **rh** (for: repeat history). 14 | 15 | Otherwise you can specify the alias name as first argument to the config.sh script. 16 | 17 | - ./config.sh [alias_name] 18 | 19 | This appends the appropriate alias to your ~/.bashrc file. 20 | 21 | You can alternately add it manually to your ~/.bashrc file (replace $PATH_TO_SCRIPT with the path to repeat_history.sh): 22 | 23 | **alias rh='history -a && . $PATH_TO_SCRIPT/repeat_history.sh'** 24 | 25 | ## Usage 26 | 27 | **rh [searchWord] [NumberOfMaxOptions]** 28 | 29 | This command is now system wide accessible by typing **rh** into the terminal. 30 | 31 | The first argument to **rh** is the search word for the command, you want to execute. If no argument is given, the last 20 executed commands are shown. 32 | 33 | The second argument to **rh** changes the number of options to show. 34 | 35 | ## Screenshots 36 | 37 | Some screenshots to see how it looks like. 38 | 39 | Call to rh without arguments: 40 | 41 | ![rh without arguments](https://github.com/MauriceGit/Repeat_History/blob/master/Screenshots/example_1.png "rh without arguments") 42 | 43 | Complete example: 44 | 45 | ![complete example](https://github.com/MauriceGit/Repeat_History/blob/master/Screenshots/example_2.png "complete example") 46 | 47 | Example of rh without selection: 48 | 49 | ![rh invalid input](https://github.com/MauriceGit/Repeat_History/blob/master/Screenshots/example_3.png "rh with no selection") 50 | 51 | ## Todo 52 | 53 | - Remove duplicates from the list so it is not spammed with the same command. 54 | - Add usage and help output. 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /Screenshots/example_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MauriceGit/Repeat_History/150fe076f2530dc093af48e5b6fba274dc4d45e2/Screenshots/example_1.png -------------------------------------------------------------------------------- /Screenshots/example_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MauriceGit/Repeat_History/150fe076f2530dc093af48e5b6fba274dc4d45e2/Screenshots/example_2.png -------------------------------------------------------------------------------- /Screenshots/example_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MauriceGit/Repeat_History/150fe076f2530dc093af48e5b6fba274dc4d45e2/Screenshots/example_3.png -------------------------------------------------------------------------------- /config.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | aliasName="rh" 4 | 5 | if [[ $# -gt 0 ]] 6 | then 7 | aliasName="$1" 8 | fi 9 | 10 | cat ~/.bashrc | grep -v "alias $aliasName=" > ~/.bashrc_tmp 11 | 12 | # we need to make the history available for the command... 13 | # Didn't find a way around history -a... 14 | echo "alias $aliasName='history -a && . $PWD/repeat_history.sh'" >> ~/.bashrc_tmp 15 | mv ~/.bashrc_tmp ~/.bashrc 16 | 17 | 18 | -------------------------------------------------------------------------------- /repeat_history.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | searchWord="$1" 4 | 5 | if [[ $# -gt 1 ]] 6 | then 7 | size="$2" 8 | else 9 | size=20 10 | fi 11 | 12 | OLDIFS=$IFS 13 | 14 | IFS=' 15 | ' 16 | 17 | output=$(cat $HISTFILE | /bin/grep "$searchWord" | /bin/grep -v "rh " | uniq | tail -n $size) 18 | 19 | outputArray=($output) 20 | 21 | IFS=$OLDIFS 22 | 23 | # Print all possible commands 24 | for ((i=0;i<${#outputArray[@]};i++)) 25 | do 26 | echo -e "$i:\t${outputArray[$i]}" 27 | done 28 | 29 | if [[ ${#outputArray[@]} -gt 0 ]] 30 | then 31 | read input 32 | 33 | isNumber=$(echo "$input" | /bin/egrep '^[0-9]+$') 34 | 35 | # Check on validity 36 | if [[ -z "$isNumber" || "$input" -ge $size ]] 37 | then 38 | echo "input is not valid." 39 | else 40 | command=${outputArray[$input]} 41 | # custom append the executed command to the history 42 | history -s "$command" 43 | # execute command 44 | eval $command 45 | fi 46 | fi 47 | 48 | --------------------------------------------------------------------------------