Skip to content

Instantly share code, notes, and snippets.

@sebnyberg
Last active July 22, 2024 12:42
Show Gist options
  • Save sebnyberg/92587e2423feabc02156e600781e90ac to your computer and use it in GitHub Desktop.
Save sebnyberg/92587e2423feabc02156e600781e90ac to your computer and use it in GitHub Desktop.
alias ls='ls --color=auto'
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
alias dir='dir --color=auto'
alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
# Add an "alert" alias for long running commands. Use like so:
# sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias dc="docker-compose"
alias dcf="docker-compose -f docker-compose.yml"
alias mk="minikube"
alias k="kubectl"
alias tf="terraform"
alias expargs='export $(cat .env | xargs)'
# https://hackernoon.com/handy-docker-aliases-4bd85089a3b8
alias dm='docker-machine'
alias dmx='docker-machine ssh'
alias dk='docker'
alias dki='docker images'
alias dks='docker service'
alias dkrm='docker rm'
alias dkl='docker logs'
alias dklf='docker logs -f'
alias dkflush='docker rm `docker ps --no-trunc -aq`'
alias dkflush2='docker rmi $(docker images --filter "dangling=true" -q --no-trunc)'
alias dkt='docker stats --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}"'
alias dkps="docker ps --format '{{.ID}} ~ {{.Names}} ~ {{.Status}} ~ {{.Image}}'"
cd() {
builtin cd "$@" && ls -F
}
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
# Path setup
export PATH="$HOME/miniconda3/bin:$PATH"
export PATH="$PATH:$HOME/bin"
# Load aliases
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
shopt -s histappend
# ignore case when using cd
bind 'set completion-ignore-case On'
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
shopt -s globstar
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
# force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
color_red="\033[0;31m"
color_yellow="\033[0;33m"
color_green="\033[0;32m"
color_ochre="\033[38;5;95m"
color_blue="\033[0;34m"
color_white="\033[0;37m"
color_reset="\033[0m"
git_info() {
local git_status="$(git status 2> /dev/null)"
local on_branch="On branch ([^${IFS}]*)"
local on_commit="HEAD detached at ([^${IFS}]*)"
local untracked_files="Untracked files:"
local not_staged="Changes not staged for commit:"
local staged="Changes to be committed:"
local branch_is_ahead="Your branch is ahead of"
if git status &>/dev/null; then
local msg="("
if [[ $git_status =~ $on_branch ]]; then
local branch=${BASH_REMATCH[1]}
msg+="${color_blue}${branch}${color_reset}"
elif [[ $git_status =~ $on_commit ]]; then
local commit=${BASH_REMATCH[1]}
msg+="${color_blue}${commit}${color_reset}"
fi
if [[ $git_status =~ $branch_is_ahead ]]; then
msg+="|${color_green}\u2191${color_reset}"
fi
msg+=")"
if [[ $git_status =~ $untracked_files
|| $git_status =~ $not_staged
|| $git_status =~ $staged ]]; then
msg+=" "
if [[ $git_status =~ $untracked_files ]]; then
msg+="${color_red}\u271A${color_reset}"
fi
if [[ $git_status =~ $not_staged ]]; then
msg+="${color_yellow}\u271A${color_reset}"
fi
if [[ $git_status =~ $staged ]]; then
msg+=" ${color_green}\u25CF${color_reset}"
fi
msg+=" "
fi
echo -e "$msg"
fi
}
# user
PS1="\[${color_red}\]\u\[${color_reset}\]"
# @ host
PS1+=" @ \[${color_yellow}\]\h\[${color_reset}\]"
# Current working directory
PS1+=" in \[${color_green}\]\w\[${color_reset}\]"
# (git_branch)
PS1+=" \$(git_info)"
# newline + '#' for root otherwise $
PS1+="\n\$ "
export PS1
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
fi
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
if [ -z "$SSH_AUTH_SOCK" ] ; then
eval `ssh-agent -s`
ssh-add
fi
# Load auto completion for kubectl
if command -v kubectl &>/dev/null; then
source <(kubectl completion bash)
fi
# remap prefix from 'C-b' to 'C-a'
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix
# Use bash as the default shell
set-option -g default-shell "/bin/bash"
# 0 is too far from ` ;)
set -g base-index 1
# Automatically set window title
set-window-option -g automatic-rename off
set-option -g set-titles on
#set -g default-terminal screen-256color
set -g status-keys vi
set -g history-limit 10000
setw -g mode-keys vi
setw -g monitor-activity on
# Enable mouse mode (tmux 2.1 and above)
set -g mouse on
bind-key \ split-window -h
bind-key - split-window -v
bind-key J resize-pane -D 5
bind-key K resize-pane -U 5
bind-key H resize-pane -L 5
bind-key L resize-pane -R 5
bind-key M-j resize-pane -D
bind-key M-k resize-pane -U
bind-key M-h resize-pane -L
bind-key M-l resize-pane -R
# Vim style pane selection
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
# Use Alt-vim keys without prefix key to switch panes
bind -n M-h select-pane -L
bind -n M-j select-pane -D
bind -n M-k select-pane -U
bind -n M-l select-pane -R
# Use Alt-arrow keys without prefix key to switch panes
bind -n M-Left select-pane -L
bind -n M-Right select-pane -R
bind -n M-Up select-pane -U
bind -n M-Down select-pane -D
# Shift arrow to switch windows
bind -n S-Left previous-window
bind -n S-Right next-window
# No delay for escape key press
set -sg escape-time 0
# Reload tmux config
bind r source-file ~/.tmux.conf
# THEME
set -g status-bg black
set -g status-fg white
set -g window-status-current-bg white
set -g window-status-current-fg black
set -g window-status-current-attr bold
set -g status-interval 60
set -g status-left-length 30
set -g status-left '#[fg=green](#S) #(whoami) '
set -g status-right '#[fg=yellow]#(cut -d " " -f 1-3 /proc/loadavg)#[default] #[fg=white]%H:%M#[default]'
set nocompatible
filetype plugin indent on
set encoding=utf-8
set fileencoding=utf-8
scriptencoding utf-8
set history=256
set timeoutlen=250
set clipboard+=unnamed " copy to clipboard
set pastetoggle=<F10> " set pastemode (no formatting)
set shiftround " round indent to multiple of shiftwidth
set t_Co=256
set autowrite
set autoread
set nobackup
set nowritebackup
set directory=~/.tmp
set noswapfile
set hidden " enables swapping of files without saving
set hlsearch
set ignorecase
set smartcase
set incsearch
let mapleader = ' '
let maplocalleader = ' '
" ===================[Formatting]===================
set fo+=l " wrap lines wider than the window without linebreaking
set fo-=o " automatically insert the current comment leader after hitting 'o' or 'O' in Normal mode.
set fo-=r " do not automatically insert a comment leader after an enter
set fo-=t " do not auto-wrap text using textwidth (does not apply to comments)
set wrap
set linebreak
set textwidth=0
set wrapmargin=0
set tabstop=2
set softtabstop=2
set shiftwidth=2
set expandtab
set smarttab
set backspace=indent
set backspace+=eol
set backspace+=start
set autoindent
set cindent
" ===================[Visual]===================
syntax on
" set background=dark
let base16colorspace=256 " Access colors present in 256 colorspace
set mousehide
set number
set relativenumber
set showmatch
set matchtime=2
set incsearch
set nohlsearch
set wildmode=list:longest,full
set completeopt+=preview
set novisualbell
set noerrorbells
set laststatus=2
set shortmess=atI
set showcmd
set scrolloff=2
set foldenable
set foldmethod=syntax
set foldopen=block,hor,tag
set foldopen+=percent,mark
set foldopen+=quickfix
set foldlevelstart=99
set virtualedit=block
set splitbelow
set splitright
set iskeyword-=_
" set list
set listchars=tab:▸\ ,eol:¬
" Ctrl-j is normally bound to 0x00 or NULL, make it available for binding
let g:C_Ctrl_j = 'off'
" Enable javascript folding
let javaScript_fold=1
" Just in case I ever stop using Linux *sob*
if has("gui_running")
set guioptions-=m guioptions-=T guioptions-=r guioptions-=L
if has("x11")
set guifont=-*-consolas-r-normal-*-*-60-*-*-m-*-*
endif
endif
" =================================[ Movement ]=================================
" Press jk to exit insert mode
inoremap jk <esc>
vnoremap jk <esc>
nnoremap <C-l> :tabnext<cr>
nnoremap <C-h> :tabprevious<cr>
" <tab><tab> to change to the next buffer
nnoremap <localleader><tab> :bn<cr>
" <space><tab> to change to the next window
nnoremap <leader><tab> <c-w><c-w>
" Tab movement
nnoremap <leader>1 {1}gt
nnoremap <leader>2 {2}gt
nnoremap <leader>3 {3}gt
nnoremap <leader>4 {4}gt
nnoremap <leader>h gT
nnoremap <leader>l gt
" Move between paragraphs
nmap <C-k> {
nmap <C-j> }
" ==============================[ Text formatting ]==============================
" Quicker indent
nnoremap < my<<`y
nnoremap > my>>`y
vnoremap < <gv
vnoremap > >gv
" Fix indent of the document by pressing = twice
nnoremap == m`gg=G``zz
" Press <tab>j or <tab>k to insert a row below or above the current line
" respectively. Works with modifiers ([count]<tab>j) e.g. 5<tab>j
nmap <leader>j o<esc>k
nmap <leader>k O<esc>j
" Bubble a line up or down
nmap <localleader>k ddkP
nmap <localleader>j ddp
" ==================================[ Folding ]==================================
" Note: requires matchit to properly fold functions
nnoremap <leader>c zf%
" Open a fold using leader o
nnoremap <leader>o za
" ==============================[ Copy and paste ]==============================
" Paste from x buffer
" Note: write :version to check compability (requires xterm_clipboard)
nnoremap <leader>p :set paste<cr>"+p :set nopaste<cr>
nnoremap <leader>P :set paste<cr>"+P :set nopaste<cr>
" Copy to x buffer using yy or ctrl+c (yy is originally analogous to Y)
" "<reg>y copies the selected text to register <reg>
vnoremap yy my"+y`y
vnoremap <C-c> "+y
" ================================[ Set options ]================================
nnoremap <silent><localleader>p :set paste!<cr>
" =============================[ Window management ]=============================
" Resize the vertically split windows
" Note: The resize is relative to the currently focused window
nnoremap <localleader>h :vert res +10<cr>
nnoremap <localleader>l :vert res -10<cr>
nnoremap <leader>+ :res +10<cr>
nnoremap <leader>- :res -10<cr>
" Split vertically using <leader>v and horizontally using <leader>s
nnoremap <leader>v :vsp<cr>
nnoremap <leader>s :sp<cr><c-w><c-w>
" ==================================[ Folding ]==================================
" Note: requires matchit to properly fold functions
nnoremap <leader>c zf%
" Open a fold using leader o
nnoremap <leader>o za
" ==============================[ Copy and paste ]==============================
" Paste from x buffer
" Note: write :version to check compability (requires xterm_clipboard)
nnoremap <leader>p :set paste<cr>"+p :set nopaste<cr>
nnoremap <leader>P :set paste<cr>"+P :set nopaste<cr>
" Copy to x buffer using yy or ctrl+c (yy is originally analogous to Y)
" "<reg>y copies the selected text to register <reg>
vnoremap yy my"+y`y
vnoremap <C-c> "+y
" ===================================[ Misc ]===================================
" Nicer bindings for basic commands
nnoremap , :
nnoremap <leader>w :w!<cr>
nnoremap <leader>q :q!<cr>
" Close QuickFix window using leader qf
nnoremap <leader>qf :ccl<cr>
" Saves using Ctrl-S, but only if the buffer has been changed
noremap <C-s> :update<cr>
imap {<CR> {<CR>}<ESC>O
#!/bin/bash
#
# Initialization script for vim, git, bash and tmux
#
# Example usage:
#
# ./init.sh bash vim
#
# Exit on fail
set -e
# Links to configuration files
gist_url='https://gist.githubusercontent.com/sebnyberg/92587e2423feabc02156e600781e90ac'
vimrc_url="${gist_url}/raw/.vimrc"
bashrc_url="${gist_url}/raw/.bashrc"
bash_aliases_url="${gist_url}/raw/.bash_aliases"
tmux_config_url="${gist_url}/raw/.tmux.conf"
# Parse arguments
while [[ $# -gt 0 ]];
do
opt="$1"
shift;
case "$opt" in
"bash") init_bash=true ;;
"vim") init_vim=true ;;
"tmux") init_tmux=true ;;
*) echo >&2 "Invalid option: $@, choose one of: tmux, bash, vim"; exit 1;;
esac
done
# Terminal echo colors
color_red='\033[0;91m'
color_green='\033[0;92m'
color_yellow='\033[0;93m'
color_blue='\033[0;94m'
color_purple='\033[0;95m'
color_cyan='\033[0;96m'
color_white='\033[0;97m'
color_off='\033[0m'
# Generate collision-safe path for putting temporary files
tmp_path="/tmp/$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 16 | head -n 1)"
mkdir -p $tmp_path
pprint() {
echo -e "[${color_purple}Dotfiles${color_off}]: $1"
}
pprint_error() {
echo -e "[${color_purple}Dotfiles${color_off}]: ${color_red}${1}${color_off}"
exit 1
}
# Save availability of packages as variables
command -v vim &>/dev/null && vim_exists=true
command -v git &>/dev/null && git_exists=true
command -v tmux &>/dev/null && tmux_exists=true
command -v bash &>/dev/null && bash_exists=true
# Missing & requested config check
[[ $init_vim = true && ! $vim_exists = true ]] && pprint_error "could not find vim in path"
[[ $init_tmux = true && ! $tmux_exists = true ]] && pprint_error "could not find tmux in path"
[[ $init_bash = true && ! $bash_exists = true ]] && pprint_error "could not find bash in path"
fetch_from_url_and_replace() {
# Fetches a file from the url at $1 and puts it at $HOME/$2
fetch_url=$1
file_name=$2
target_path="$HOME/$file_name"
# Download bash config to tmp path
pprint "Downloading ${file_name}"
curl -s "${fetch_url}" -o "${tmp_path}/${file_name}"
# Backup if target already exists
if [ -f "$target_path" ]; then
pprint "Backed up: ${file_name} -> ${file_name}.bak"
mv $target_path "${target_path}.bak"
fi
# Move tmp file to final destination
mv "${tmp_path}/${file_name}" $target_path
}
# ==========[ Bash setup ]==========
if [[ $init_bash = true ]]; then
fetch_from_url_and_replace $bashrc_url ".bashrc"
fetch_from_url_and_replace $bash_aliases_url ".bash_aliases"
# Make sure that bashrc is sourced when spawning a new login shell
pprint 'Making sure that .bash_profile contains "source ~/.bashrc"'
pprint 'This is required by e.g. tmux to load .bashrc upon opening new sessions'
if [ ! -f $HOME/.bash_profile ]; then
echo 'source ~/.bashrc' > ~/.bash_profile
else # file exists
if ! fgrep -xq "source ~/.bashrc" ~/.bash_profile; then
echo >> ~/.bash_profile
echo 'source ~/.bashrc' >> ~/.bash_profile
fi
fi
fi
# ==========[ Vim setup ]==========
[[ $init_vim = true ]] && fetch_from_url_and_replace $vimrc_url ".vimrc"
[[ $init_vim = true && $git_exists = true ]] && git config --global core.editor vim
# ==========[ Tmux setup ]==========
[[ $init_tmux = true ]] && fetch_from_url_and_replace $tmux_config_url ".tmux.conf"
pprint "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment