# RSH 019: shell tips and tricks
## Intro
## Suggestion from Twitter
- using * and ? with file names
- colorized man pages
- git prompt
- shortcuts/ aliases
## Each person has 10 minutes to show/explain something
### Anne
- Terminal for Windows users: [MobaXterm](https://mobaxterm.mobatek.net/)
- You don't need to be administrator to install it
### Many shells exist
- https://xon.sh/
- Bash, Fish, zsh, csh, tcsh, ...
- elvish https://elv.sh/
### Richard
- `CDPATH`
- just like `PATH` is used for running programs, `CDPATH` is used for `cd`
- I set `CDPATH=~git`, so that I can `cd project-name` for any project in git directly
- ! aliases
- We often need to re-run a command with the same argument. For example, you often `mkdir dir-name` and then `cd dir-name`. We want to type less?
- `!$` means last argument of last command line. `!` commands expand to recent history. `$` means "last argument"
- `!!` means entire previous line
- There is much more I won't go into
- (don't say) Line designator
- `!-n` refers to n:th previous line
- `!string` is recent line beginning with `string`, or `!?string?` for previous line containing string
- (don't say) Word designator: `:` + symbol, sometimes `:` optional
- `0`: zeroth argument (program name)
- `^`: first argument
- `$`: last argument
- `x`, `x-y`, `x-`: range of word numbers.
- (don't say) Modifiers
- `h`: remove trailing filename
- `t`: remove head, leave only last pathname
- Important options
- `shopt -s histreedit`
- `shopt -s histverify` - don't run, verify before running
- bash manual page section: [HISTORY EXPANSION](https://manpages.debian.org/bash/bash.1.en.html#HISTORY_EXPANSION)
- per-directory bash history
- I have a per-directory shell history, which gets saved in `_bashhist` in each directory (once the shell terminates).
-
- You can see the commands in hackmd, you can also find ideas in stack overflow.
- I have aliases `histsave`, `histread`, `histoff` as well
- Ignore this in git: ~/.gitignore, `git config core.excludesfile=~/.gitignore`.
- Warning: I wrote this myself, there are possibly bugs I haven't resolved yet.
```
histsave() { history -a ; }
histread() { history -n ; history -n ; } # Needs it twice somehow...
histoff() { HISTFILE=/dev/null ; }
cd_hook() {
# This function handles a per-directory history file
history -a
command cd "$@" ;
if [ -z "$PS1" -o -z "$HISTFILE" -o "$HISTFILE" = "/dev/null" ] ; then
# Don't do histfile magic if not in interactive mode, HISTFILE
# is unset or set to /dev/null
return
fi
#if echo "$PWD" | grep "^$HOME" >> /dev/null && test -w . -a -O . -a -G .;
#if test -w . -a -O . -a -G .;
if test -w . ;
then
# Within home directory
HISTFILE=./_bashhist
else
HISTFILE=~/.bash_history
fi
}
alias cd=cd_hook
cd_hook $PWD
if [ "$PS1" -a x$_loaded_hist_once = x ] ; then
history -r
_loaded_hist_once=True
fi
```
- Did you know history, keyboard shortcuts, and so on come from something called **readline**, and is often associated with bash because readline and bash came from the same author?
- You can use many of the same history searching, shortcuts, etc in other things that support readline
- Examples: Python, gdb,
- So I don't customize readline per-application or per-user too much, so that I am use to the keyboard shortcuts on any system, even if they aren't what I would ideally want.
- ! aliases (the only one I regularly use is !$ but I wish I - used more)
- per-directory bash history
- CDPATH
- convert git aliases to shell aliases
#- per-host configuration in .bashrc, to have one bashrc everywhere
#- my own git-aware bash prompt
#- browser temporary sessions
#- shortcut functions for sourcing/adding stuff to paths
#- set -x, which
### Roberto
I'll use the terminal in this [binder instance](https://mybinder.org/v2/gh/robertodr/nix-demo/HEAD)
- direnv
- nix-shell
- ~~fish?~~ Since Radovan is already talking about that. <- I think ok to talk about it also here and I can add 2 cents
### Radovan
- jumping between folders (cd -, pushd, popd)
- for loops: renaming many files at once
- let's use some command that I don't use every day
- tldr
- reusing past commands (reverse-i-search with CTRL+R)
- editing past commands (how to move to start or end of a line: CTRL+A or CTRL+E)
- what I like about fish
- aliases are functions
- autosuggestions and colors
- history is per-directory and context-aware
- simpler syntax (at the cost of not being POSIX-compilant)
- on a supercomputer/cluster I still advise to use bash (many tools assume bash and break otherwise)