Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions _episodes/01-shell-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,15 @@ and so on.
> popular command
{: .challenge}

> ## An opinionated `.repronim.bashrc`
>
> If you would like to see an example of a `.bashrc` file that makes reproducible
> practices the "default" option, checkout [.repronim.bashrc](({{ page.root }}/code/.repronim.bashrc)).
> You can either `source` this file in your existing `.bashrc` file
> (e.g. you can copy this file to your home directory and add `source ~/.repronim.bashrc`
> to your original `.bashrc` file)
> or you can copy/paste useful lines from this file to your `.bashrc`.
{: .callout}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever teaching VM/environment we provide next time, we should make sure to include this .bashrc ;-)!


## Hints for correct/robust scripting in shell

Expand Down
Empty file removed code/.gitkeep
Empty file.
99 changes: 99 additions & 0 deletions code/.repronim.bashrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
set -o noclobber # do not overwrite existing files
set -o emacs # use the emacs shortcuts

# function to help remind users about the shortcuts
print_shortcuts() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested -- we could even use ? for the name of this function. I am just afraid that we all (I for sure) would forget what is the name (print_shortcuts). If at least it would become repronim specific (e.g. repronim_bashrc_help) then I could use completion to arrive to it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I like repronim_bashrc_help because the question mark could have unexpected behavior

LINE="Commandline/Cursor Editing Shortcuts::
Ctrl-a: Go to the beginning of the line you are currently typing on
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not to indent all those items but at least one space -- otherwise it clutters visual appearance IMHO

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, did it in 9792157, please revert/drop if you feel that it is not desired

Ctrl-e: Go to the end of the line you are currently typing on
Ctrl-u: Remove text on the line before the cursor position
Ctrl-h: Remove preceding symbol (same as backspace)
Ctrl-w: Delete the word before the cursor
Ctrl-k: Remove text on the line after the cursor position
Ctrl-t: Swap the last two characters before the cursor
Alt-t: Swap the last two words before the cursor
Alt-f: Move cursor forward one word on the current line
Alt-b: Move cursor backward one word on the current line
Tab: Auto-complete files, folders, and command names
Ctrl-x OR Ctrl-e OR Alt-e in zsh: Edit command line text in the editor (as defined by VISUAL environment variable)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pushed the fix (3b059b9), should be Ctrl-x Ctrl-e (not either of those but both in sequence)


CONTROL="Job Control Shortcuts::
Ctrl-c: Kill currently running process
Ctrl-d: Exit current shell
Ctrl-z: Suspend currently running process. fg restores it, and bg places it into background execution"

HISTORY="History Shortcuts::
Ctrl-p: Previous line in the history
Ctrl-n: Next line in the history
Ctrl-r: Bring up next match backwards in shell history"

if [ $# -lt 1 ]; then
echo -e "${LINE}\n\n${CONTROL}\n\n${HISTORY}"
return 0
fi

while [ $# -ge 1 ]; do
case "$1"
in
n)
echo -e "$LINE\n"
shift;;
line)
echo -e "$LINE\n"
shift;;
c)
echo -e "$CONTROL\n"
shift;;
control)
echo -e "$CONTROL\n"
shift;;
h)
echo -e "$HISTORY\n"
shift;;
history)
echo -e "$HISTORY\n"
shift;;
-h)
echo "Options are: n, line, c, control, h, history or blank"
echo "Example1: print_shortcuts n c h"
echo "Example2: print_shortcuts"
return 0
;;
--help)
echo "Options are: n, line, c, control, h, history or blank"
echo "Example1: print_shortcuts n c h"
echo "Example2: print_shortcuts"
return 0
;;
*)
echo "Option $1 not recognized use (n, line, c, control, h, history or blank)"
echo "type print_shortcuts -h for help"
echo "Example1: print_shortcuts n c h"
echo "Example2: print_shortcuts"
return 1
;;
esac
done
}


# create an eternal bash history file
# https://debian-administration.org/article/543/Bash_eternal_history
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you use this one personally @jdkent? I just have no personal experience with this one yet

Copy link
Author

@jdkent jdkent Jun 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the eternal bash history appears to just be a fail safe when the command is older than .bash_history holds. i played with cntrl+r and it does not use .bash_eternal_history, just the number of commands in history stored in memory (not in a file). You have to interact with the file directly to search for old commands (e.g. cat ~/.bash_eternal_history | grep "string").


bhf="${HOME}/.bash_eternal_history"
if [ ! -f "${bhf}" ]; then
touch "${bhf}"
fi

if [ "$(stat -c %a "${bhf}")" != "600" ]; then
chmod 600 "${bhf}"
fi

# NOTE: I changed ${PROMPT_COMMAND:...} to ${PROMPT_COMMAND%%;:...}
# to account for trailing semicolons existing in the commmand (like if you've installed pyenv)
# see: https://github.com/pyenv/pyenv-virtualenv/issues/247
export HISTTIMEFORMAT="%s "
PROMPT_COMMAND="${PROMPT_COMMAND%%;:+$PROMPT_COMMAND ; }"'echo $$ $USER "$(history 1)" >> ${bhf}'

# make a terminal prompt that shows the full path: http://ezprompt.net/
export PS1="\[\e[33m\]\u\[\e[m\]:\[\e[35m\]\s\[\e[m\]\[\e[37m\]:\[\e[m\]\[\e[36m\]\w\[\e[m\]\\$ "