Skip to content

Instantly share code, notes, and snippets.

@me7
Last active February 4, 2023 20:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save me7/e19805c0e6f27d9273b0 to your computer and use it in GitHub Desktop.
Save me7/e19805c0e6f27d9273b0 to your computer and use it in GitHub Desktop.
fzf .bash_profile and .bashrc
# System settings before starting X
. $HOME/.bashrc
PATH=$PATH:$HOME/bin
# set up alsa
/usr/bin/amixer sset Master Mono 90% unmute &> /dev/null
/usr/bin/amixer sset Master 90% unmute &> /dev/null
/usr/bin/amixer sset PCM 90% unmute &> /dev/null
#[[ -z $DISPLAY && $XDG_VTNR -eq 1 ]] && exec startx
source ~/z.sh
source ~/.fzf.bash
export EDITOR=subl
export TERMINAL=lxterminal
export BROWSER=chromium
# f() find command in .bashrc
alias f='grep "^# " ~/.bashrc'
alias e='locate / | fzf'
alias ls='ls --color=auto -la'
alias ll='ls -lF --color=auto'
alias la='ls -alF --color=auto'
alias ls='ls -F --color=auto'
alias grep='grep --color=auto --exclude-dir=\.git'
# ge() grep then edit content in dir then open in sublime
ge(){
file=$(grep --line-buffered --color=auto -nrFI "$1" * | fzf -e | cut -d: -f1-2) && subl ${file}
}
# pk() process kill
pk() {
pid=$(ps -ef | sed 1d | fzf -m | awk '{print $2}')
if [ "x$pid" != "x" ]
then
kill -${1:-9} $pid
fi
}
# lf() - locate file from root folder
lf() {
file=$(locate / | fzf -e) && cd "$file"
}
# lf() - locate file from home folder
lh() {
file=$(locate ~ | fzf -e) && cd "$file"
}
# glog - git log browser (enter for show, ctrl-d for diff, ` toggles sort)
glog() {
local out shas sha q k
while out=$(
git log --graph --color=always \
--format="%C(auto)%h%d %s %C(black)%C(bold)%cr" "$@" |
fzf --ansi --multi --no-sort --reverse --query="$q" \
--print-query --expect=ctrl-d --toggle-sort=\`); do
q=$(head -1 <<< "$out")
k=$(head -2 <<< "$out" | tail -1)
shas=$(sed '1,2d;s/^[^a-z0-9]*//;/^$/d' <<< "$out" | awk '{print $1}')
[ -z "$shas" ] && continue
if [ "$k" = ctrl-d ]; then
git diff --color=always $shas | less -R
else
for sha in $shas; do
git show --color=always $sha | less -R
done
fi
done
}
# gco - checkout git commit
gco() {
local commits commit
commits=$(git log --pretty=oneline --abbrev-commit --reverse) &&
commit=$(echo "$commits" | fzf --tac +s +m -e) &&
git checkout $(echo "$commit" | sed "s/ .*//")
}
# gcot - git checkout branch/tag
gcot() {
local tags branches target
tags=$(
git tag | awk '{print "\x1b[31;1mtag\x1b[m\t" $1}') || return
branches=$(
git branch --all | grep -v HEAD |
sed "s/.* //" | sed "s#remotes/[^/]*/##" |
sort -u | awk '{print "\x1b[34;1mbranch\x1b[m\t" $1}') || return
target=$(
(echo "$tags"; echo "$branches") |
fzf-tmux -l30 -- --no-hscroll --ansi +m -d "\t" -n 2) || return
git checkout $(echo "$target" | awk '{print $2}')
}
# ftags - search ctags
ftags() {
local line
[ -e tags ] &&
line=$(
awk 'BEGIN { FS="\t" } !/^!/ {print toupper($4)"\t"$1"\t"$2"\t"$3}' tags |
cut -c1-80 | fzf --nth=1,2
) && $EDITOR $(cut -f3 <<< "$line") -c "set nocst" \
-c "silent tag $(cut -f2 <<< "$line")"
}
# fe [FUZZY PATTERN] - Open the selected file with the default editor
#- Bypass fuzzy finder if there's only one match (--select-1)
#- Exit if there's no match (--exit-0)
fe() {
local file
file=$(fzf --query="$1" --select-1 --exit-0)
[ -n "$file" ] && ${EDITOR:-vim} "$file"
}
#Modified version where you can press
#- CTRL-O to open with `open` command,
#- CTRL-E or Enter key to open with the $EDITOR
fo() {
local out file key
out=$(fzf-tmux --query="$1" --exit-0 --expect=ctrl-o,ctrl-e)
key=$(head -1 <<< "$out")
file=$(head -2 <<< "$out" | tail -1)
if [ -n "$file" ]; then
[ "$key" = ctrl-o ] && open "$file" || ${EDITOR:-vim} "$file"
fi
}
# cdf() - cd into the directory of the selected file
cdf() {
local file
local dir
file=$(fzf -e -q "$*") && dir=$(dirname "$file") && cd "$dir"
}
# cdh() - cd from home
cdh() {
local file
local dir
file=$(locate ~ | fzf -e -q "$*") && dir=$(dirname "$file") && cd "$dir"
}
# cdr - cd from root
cdr() {
local file
local dir
file=$(locate / | fzf -e -q "$*") && dir=$(dirname "$file") && cd "$dir"
}
# fs [FUZZY PATTERN] - Select selected tmux session
#- Bypass fuzzy finder if there's only one match (--select-1)
#- Exit if there's no match (--exit-0)
fs() {
local session
session=$(tmux list-sessions -F "#{session_name}" | \
fzf --query="$1" --select-1 --exit-0) &&
tmux switch-client -t "$session"
}
# ftpane - switch pane
ftpane () {
local panes current_window target target_window target_pane
panes=$(tmux list-panes -s -F '#I:#P - #{pane_current_path} #{pane_current_command}')
current_window=$(tmux display-message -p '#I')
target=$(echo "$panes" | fzf) || return
target_window=$(echo $target | awk 'BEGIN{FS=":|-"} {print$1}')
target_pane=$(echo $target | awk 'BEGIN{FS=":|-"} {print$2}' | cut -c 1)
if [[ $current_window -eq $target_window ]]; then
tmux select-pane -t ${target_window}.${target_pane}
else
tmux select-pane -t ${target_window}.${target_pane} &&
tmux select-window -t $target_window
fi
}
# 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}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment