Skip to content

Instantly share code, notes, and snippets.

@mathematicalmichael
Last active November 4, 2019 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathematicalmichael/4190e1fae7627a5436d30c24f8dc0e7c to your computer and use it in GitHub Desktop.
Save mathematicalmichael/4190e1fae7627a5436d30c24f8dc0e7c to your computer and use it in GitHub Desktop.
startup_scripts/ :: A hook for docker images to run on spawn
shopt -s expand_aliases
# load in your custom fzf extensions
if [ -f ~/.bash_fzf ]; then
. ~/.bash_fzf
fi
alias srv='python -m http.server'
alias erc='vim ~/.bash_aliases'
# watch "markdown"
md () {
markdown2 --extras footnotes,task_list,fenced-code-blocks,cuddled-lists \"$1\" > index.html
}
wmd () {
watch -n1 "markdown2 --extras footnotes,task_list,fenced-code-blocks,cuddled-lists \"$1\" > index.html"
}
rr () {
if [ -f ~/.bash_profile ]; then
. ~/.bash_profile; else
. ~/.bashrc
fi
}
alias ..='cd ../' # Move up 1 directory
alias ...='cd ../../' # Move up 2 directories
alias ....='cd ../../../' # Move up 3 directories
# alias f='open -a Finder ./' # Open current folder in finder (mac osx)
alias clc='clear'
alias ll='ls -Ghaltr'
alias ls='ls -pG'
alias la='ls -A'
alias nf='ls | wc -l' # number of files in current directory
alias sz='du -sh' # human-readable file size of current directory
alias gitsu='git status -uno'
alias gits='git status'
alias gitcam='git commit -am "auto-commit, updates"'
alias gitpom='git push origin master'
alias gitr='git config credential.helper "cache --timeout 1440000"'
alias gitrm='git config credential.helper ""'
# Styling
# ------------
export FZF_DEFAULT_OPTS="--layout=reverse --border --inline-info"
# --preview 'head -100 {}'"
export FZF_COMPLETION_TRIGGER='~~'
# ------------
## From the wiki: https://github.com/junegunn/fzf/wiki/examples#git
# ------------
# vim
# v - open files in ~/.viminfo
v() {
local files
files=$(grep '^>' ~/.viminfo | cut -c3- |
while read line; do
[ -f "${line/\~/$HOME}" ] && echo "$line"
done | fzf-tmux -d -m -q "$*" -1) && vim ${files//\~/$HOME}
}
# Homebrew
# ------------
# Install (one or multiple) selected application(s)
# using "brew search" as source input
# mnemonic [B]rew [I]nstall [P]lugin
bip() {
local inst=$(brew search | fzf -m)
if [[ $inst ]]; then
for prog in $(echo $inst);
do brew install $prog; done;
fi
}
# Update (one or multiple) selected application(s)
# mnemonic [B]rew [U]pdate [P]lugin
bup() {
local upd=$(brew leaves | fzf -m)
if [[ $upd ]]; then
for prog in $(echo $upd);
do brew upgrade $prog; done;
fi
}
# Delete (one or multiple) selected application(s)
# mnemonic [B]rew [C]lean [P]lugin (e.g. uninstall)
bcp() {
local uninst=$(brew leaves | fzf -m)
if [[ $uninst ]]; then
for prog in $(echo $uninst);
do brew uninstall $prog; done;
fi
}
# Terminal
# ------------
function cd() {
if [[ "$#" != 0 ]]; then
builtin cd "$@";
return
fi
while true; do
local lsd=$(echo ".." && ls -p | grep '/$' | sed 's;/$;;')
local dir="$(printf '%s\n' "${lsd[@]}" |
fzf --reverse --preview '
__cd_nxt="$(echo {})";
__cd_path="$(echo $(pwd)/${__cd_nxt} | sed "s;//;/;")";
echo $__cd_path;
echo;
ls -p -FG "${__cd_path}";
')"
[[ ${#dir} != 0 ]] || return 0
builtin cd "$dir" &> /dev/null
done
}
# Another fd - cd into the selected directory
# This one differs from the above, by only showing the sub directories and not
# showing the directories within those.
fd() {
DIR=`find * -maxdepth 0 -type d -print 2> /dev/null | fzf-tmux` \
&& cd "$DIR"
}
# fda - including hidden directories
fda() {
local dir
dir=$(find ${1:-.} -type d 2> /dev/null | fzf +m) && cd "$dir"
}
# using ripgrep combined with preview
# find-in-file - usage: fif <searchTerm>
fif() {
if [ ! "$#" -gt 0 ]; then echo "Need a string to search for!"; return 1; fi
rg --files-with-matches --no-messages "$1" | fzf --preview "highlight -O ansi -l {} 2> /dev/null | rg --colors 'match:bg:yellow' --ignore-case --pretty --context 10 '$1' || rg --ignore-case --pretty --context 10 '$1' {}"
}
# Git
# ------------
# fbr - checkout git branch (including remote branches), sorted by most recent commit, limit 30 last branches
fbr() {
local branches branch
branches=$(git for-each-ref --count=30 --sort=-committerdate refs/heads/ --format="%(refname:short)") &&
branch=$(echo "$branches" |
fzf-tmux -d $(( 2 + $(wc -l <<< "$branches") )) +m) &&
git checkout $(echo "$branch" | sed "s/.* //" | sed "s#remotes/[^/]*/##")
}
# fshow - git commit browser
fshow() {
git log --graph --color=always \
--format="%C(auto)%h%d %s %C(black)%C(bold)%cr" "$@" |
fzf --ansi --no-sort --reverse --tiebreak=index --bind=ctrl-s:toggle-sort \
--bind "ctrl-m:execute:
(grep -o '[a-f0-9]\{7\}' | head -1 |
xargs -I % sh -c 'git show --color=always % | less -R') << 'FZF-EOF'
{}
FZF-EOF"
}
# fcoc - checkout git commit (with preview)
alias glNoGraph='git log --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr% C(auto)%an" "$@"'
_gitLogLineToHash="echo {} | grep -o '[a-f0-9]\{7\}' | head -1"
_viewGitLogLine="$_gitLogLineToHash | xargs -I % sh -c 'git show --color=always % | diff-so-fancy'"
# fcoc_preview - checkout git commit with previews
fcoc() {
local commit
commit=$( glNoGraph |
fzf --no-sort --reverse --tiebreak=index --no-multi \
--ansi --preview="$_viewGitLogLine" ) &&
git checkout $(echo "$commit" | sed "s/ .*//")
}
# fco_preview - checkout git branch/tag, with a preview showing the commits between the tag/branch and HEAD
fco() {
local tags branches target
branches=$(
git --no-pager branch --all \
--format="%(if)%(HEAD)%(then)%(else)%(if:equals=HEAD)%(refname:strip=3)%(then)%(else)%1B[0;34;1mbranch%09%1B[m%(refname:short)%(end)%(end)" \
| sed '/^$/d') || return
tags=$(
git --no-pager tag | awk '{print "\x1b[35;1mtag\x1b[m\t" $1}') || return
target=$(
(echo "$branches"; echo "$tags") |
fzf --no-hscroll --no-multi -n 2 \
--ansi --preview="git --no-pager log -150 --pretty=format:%s '..{2}'") || return
git checkout $(awk '{print $2}' <<<"$target" )
}
# fshow_preview - git commit browser with previews
fshow() {
glNoGraph |
fzf --no-sort --reverse --tiebreak=index --no-multi \
--ansi --preview="$_viewGitLogLine" \
--header "enter to view, alt-y to copy hash" \
--bind "enter:execute:$_viewGitLogLine | less -R" \
--bind "alt-y:execute:$_gitLogLineToHash | xclip"
}
# Docker
# ------------
# Select a running docker container to stop
function ds() {
local cid
cid=$(docker ps --format "table {{.ID}}-{{.Names}}, {{.Image}}:{{.Ports}}" | sed 1d | fzf -q "$1" | awk '{print $1}')
[ -n "$cid" ] && docker stop -t 5 "$cid"
}
# Select a docker container to remove
function drm() {
local cid
cid=$(docker ps -a --format "table {{.ID}} \t {{.Names}} \t {{.Image}}" | sed 1d | fzf -q "$1" | awk '{print $1}')
[ -n "$cid" ] && docker rm "$cid"
}
# Select a docker container to start and attach to
function da() {
local cid
cid=$(docker ps -a | sed 1d | fzf -1 -q "$1" | awk '{print $1}')
[ -n "$cid" ] && docker start "$cid" && docker attach "$cid"
}
# fkill - kill processes - list only the ones you can kill. Modified the earlier script.
fkill() {
local pid
if [ "$UID" != "0" ]; then
pid=$(ps -f -u $UID | sed 1d | fzf -m | awk '{print $2}')
else
pid=$(ps -ef | sed 1d | fzf -m | awk '{print $2}')
fi
if [ "x$pid" != "x" ]
then
echo $pid | xargs kill -${1:-9}
fi
}
{
// CodeMirror
// @jupyterlab/codemirror-extension:commands
// Text editor settings for all CodeMirror editors.
// ************************************************
// Key Map
// Configures the keymap to use
"keyMap": "default",
// Theme
// CSS file defining the corresponding
// .cm-s-[name] styles is loaded
"theme": "solarized dark"
}
# install any packages here, one line at a time
autopep8
#!/bin/bash
# in order to add environmental variables, put them in the following format:
echo "TEST_ECHO_HOOK='it worked' " >> ~/.bashrc
# you won't have sudo permissions since this script is run as the user
# but you can install extra packages that do not require it
# be mindful of paths you may not have permission to access
# misconfiguring will lead to a frozen start-up.
# however, this is a good place to run conda installations
# this adds time to spawning the server, so only run things you would normally
# be installing manually on log-in anyway and willing to wait for.
# adding || true allows graceful failure.
# vim settings
git clone https://github.com/mathematicalmichael/docker-env.git \
--depth 1 \
~/startup_scripts/docker-env/ || true
mv ~/startup_scripts/docker-env/setup/vim ~/.vim
mv ~/startup_scripts/docker-env/setup/vimrc ~/.vimrc
mv ~/startup_scripts/docker-env/setup/bash_aliases ~/.bash_aliases
mv ~/startup_scripts/docker-env/setup/bash_fzf ~/.bash_fzf
echo "moved setup files."
echo "moving settings for jlab"
# dark mode and settings
mkdir -p ~/.jupyter/lab/user-settings/@jupyterlab/apputils-extension/
mv ~/startup_scripts/themes.jupyterlab-settings ~/.jupyter/lab/user-settings/@jupyterlab/apputils-extension/
mkdir -p ~/.jupyter/lab/user-settings/@jupyterlab/codemirror-extension/
mv ~/startup_scripts/commands.jupyterlab-settings ~/.jupyter/lab/user-settings/@jupyterlab/codemirror-extension/
echo "removing docker-env"
rm -rf ~/startup_scripts/docker-env/
echo "configuring settings"
# some other settigs, preferential default programs if they exist.
if [ -f /usr/bin/vim ]; then
echo "export EDITOR=/usr/bin/vim" >> ~/.bashrc;
fi
if [ -f /usr/bin/less ]; then
echo "export PAGER=/usr/bin/less" >> ~/.bashrc;
else
echo "export PAGER=/usr/bin/more" >> ~/.bashrc;
fi
echo "remember git config"
git config --global credential.helper "cache --timeout 144000"
echo "default latex"
# you can choose the default compiler for the jupyterlab latex extension
echo "c.LatexConfig.latex_command = 'pdflatex'" >> /home/$NB_USER/.jupyter/jupyter_notebook_config.py
echo "Completed successfully."
{
// Theme
// @jupyterlab/apputils-extension:themes
// Theme manager settings.
// *************************************
// Selected Theme
// Application-level visual styling theme
"theme": "JupyterLab Dark"
}
execute pathogen#infect()
set rtp+=/usr/local/opt/fzf
set nu
syntax on
set tabstop=4
set shiftwidth=4
set expandtab
"<bar> separates commands. <CR> is carriage return - equivalent of pressing enter"
map tt :tabe
map ! :!
map FF :FZF <CR>
"load in bash aliases"
let $BASH_ENV = "~/.bash_aliases"
"spellcheck!"
set spelllang=en_us
set spellfile=$HOME/.vim/spell/en.utf-8.add
set spell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment