Last active
May 19, 2017 19:23
-
-
Save jk3us/76f47f4613199c86b92e5c6ae18baca7 to your computer and use it in GitHub Desktop.
My psql/tmux configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case "$TERM" in | |
screen*) | |
alias psql='PAGER=vim_psql_pager psql' | |
esac |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bind-key d split-window -h -p 25 pg_set_service # sets #{selected_db} | |
bind-key p split-window -p 25 'PAGER=tmux_psql_pager psql -q service=${selected_db}' | |
bind-key P new-window 'PAGER=tmux_psql_pager psql -q service=${selected_db}' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script creates a nice prompt generated from your .pg_service.conf | |
# file (https://www.postgresql.org/docs/current/static/libpq-pgservice.html) | |
# The selected service becomes the one that other tmux commands use | |
# Below, I map it to Prefix-d in tmux | |
options=$(grep '^\[.*]$' ~/.pg_service.conf | sed -e 's/^\[//' -e 's/]$//'| cat -n) | |
exec 3>&1 | |
choice=$(dialog.exe --menu "Please select a database" 19 40 9 $options 2>&1 1>&3) | |
exec 3>&- | |
#echo $selected | |
selected=$(grep '^\[.*]$' ~/.pg_service.conf | sed -e 's/^\[//' -e 's/]$//' | head -n $choice | tail -n 1) | |
tmux set-environment selected_db $selected | |
tmux display "Default database set to #{selected_db}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Based on | |
" https://unix.stackexchange.com/questions/27812/pager-program-like-less-able-to-repeat-top-n-lines | |
function FoldScroll(lines) | |
let b:foldend+=a:lines | |
if b:foldend < 2 | |
let b:foldend=2 | |
elseif b:foldend > line('$') | |
let b:foldend=line('$') | |
endif | |
norm zE | |
execute ':2,'.b:foldend.'fold' | |
endfunction | |
" :Less | |
" turn vim into a pager for psql aligned results | |
fun! Less() | |
syn region Heading start=/^ \l/ end=/[-+]\+$/ | |
syn match Border "|" | |
syn match IntVal " \d\+\(\n\| \)" | |
syn match NullVal " NULL\(\n\| \)" | |
syn match NegVal " -\d\+\(\n\| \)" | |
syn match FloatVal " \d\+\.\d\+\(\n\| \)" | |
syn match NegFloatVal " -\d\+\.\d\+\(\\n\| \)" | |
syn match DateTime "\d\{4}-\d\{2}-\d\{2} \d\{2}:\d\{2}:\d\{2}\(\.\d\{1,}\|\)" | |
syn match TrueVal " t\(\n\| \)" | |
syn match FalseVal " f\(\n\| \)" | |
hi def Heading ctermfg=246 | |
hi def IntVal ctermfg=229 | |
hi def FalseVal ctermfg=88 | |
hi def NullVal ctermfg=242 | |
hi def Border ctermfg=240 | |
hi def NegFloatVal ctermfg=160 | |
hi def FloatVal ctermfg=230 | |
hi def NegVal ctermfg=160 | |
hi def DateTime ctermfg=111 | |
hi def TrueVal ctermfg=64 | |
set nocompatible | |
set nowrap | |
set scrollopt=hor | |
set scrollbind | |
set mouse=a | |
set lazyredraw | |
set number | |
set laststatus=0 | |
" hide contents of upper statusline. editor note: do not remove trailing spaces in next line! | |
set statusline=\ | |
" This makes the folded line look like the existing header separator from | |
" pgsql! | |
set foldtext=getline(2) | |
let b:foldend = 2 | |
" arrows do scrolling instead of moving | |
nmap ^[OC zL | |
nmap ^[OD zH | |
" Remap common motion keys to use the fold scroll stuff | |
nmap <silent> ^[OB :call FoldScroll(1)<CR> | |
nmap <silent> ^[OA :call FoldScroll(-1)<CR> | |
nmap <silent> <C-ScrollWheelDown> zL | |
nmap <silent> <C-ScrollWheelUp> zH | |
nmap <silent> <ScrollWheelDown> :call FoldScroll(3)<CR> | |
nmap <silent> <ScrollWheelUp> :call FoldScroll(-3)<CR> | |
nmap <silent> <Space> :call FoldScroll(winheight(0))<CR> | |
nmap <silent> <PageDown> :call FoldScroll(winheight(0))<CR> | |
nmap <silent> <PageUp> :call FoldScroll(0-winheight(0))<CR> | |
nmap <silent> G :call FoldScroll(line('$'))<CR> | |
nmap <silent> gg :call FoldScroll(0-line('$'))<CR> | |
" faster quit (I tend to forget about the upper panel) | |
nmap q :qa!<CR> | |
nmap Q :qa!<CR> | |
endfun | |
command! -nargs=0 Less call Less() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Set this script to be your pager for psql (see bash alias below) to get pretty display and nice navigation | |
# * sticky column headers | |
# * arrow keys, Page Up/Down, and mouse wheel (ctrl+scroll for horizontal scrolling) | |
# * uses psql_pager vimscript above | |
# Create a fifo in a temp dir | |
tmpdir=$(mktemp -d) | |
trap 'rm -rf "$tmpdir"' EXIT INT TERM HUP | |
mkfifo "$tmpdir/pipe" | |
tmux_window_id=$(tmux display-message -p '#{window_id}') | |
# Try to kill any existing psql result pane for this window | |
existing_result_pane=$(tmux show-environment psql_result_pane_${tmux_window_id:1} 2>/dev/null) | |
if [[ $? && ! -z ${existing_result_pane##*=} ]] | |
then | |
tmux kill-pane -t %${existing_result_pane##*=} >/dev/null 2>&1 | |
fi | |
new_pane_id=$(tmux split-window -P -F '#D' -fd -p 75 "vim -u NONE -R -S ~/.vim/plugin/psql_pager.vim -c Less - < $tmpdir/pipe") | |
tmux set-environment psql_result_pane_${tmux_window_id:1} ${new_pane_id:1} | |
cat <&0 >$tmpdir/pipe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment