Skip to content

Instantly share code, notes, and snippets.

@shabanali-faghani
Last active January 6, 2021 11:55
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 shabanali-faghani/4758a25401f651aa19b2c7ffc3234513 to your computer and use it in GitHub Desktop.
Save shabanali-faghani/4758a25401f651aa19b2c7ffc3234513 to your computer and use it in GitHub Desktop.
#install script for servers not connected to the Internet from current local install
#-------------------------------------------------------
#!/bin/bash
set -x
scp -r ~/softwares/.fzf ${2:-root}@$1:/usr/local/bin/
ssh ${2:-root}@$1 'echo "[ -f /usr/local/bin/.fzf/.fzf.bash ] && source /usr/local/bin/.fzf/.fzf.bash" >> /etc/bashrc'
ssh ${2:-root}@$1 'chmod +x /usr/local/bin/.fzf/bin/fzf'
#-------------------------------------------------------
HISTSIZE=2000
nl ~/.bash_history | sort -k2 -k 1,1nr | uniq -f1 | sort -n | cut -f2 > ~/.bash_history_temp
mv -f ~/.bash_history_temp ~/.bash_history
HISTCONTROL=ignoreboth:erasedups
#HISTCONTROL=ignoredups:erasedups
#shopt -s histappend
#PROMPT_COMMAND="history -n; history -w; history -c; history -r; $PROMPT_COMMAND"
#bind '"\e[A": " \C-e\C-u\C-y\ey\C-u`__fzf_history__`\e\C-e\er\e^"'
bind '"\e[B": " \C-e\C-u\C-y\ey\C-u`__fzf_history__`\e\C-e\er\e^"'
bind '"\eOB": " \C-e\C-u\C-y\ey\C-u`__fzf_history__`\e\C-e\er\e^"'
__fzf_select_dir ()
{
builtin typeset READLINE_LINE_NEW="$(
command find -L . \( -path '*/\.*' -o -fstype dev -o -fstype proc \) \
-prune \
-o -type f -print \
-o -type d -print \
-o -type l -print 2>/dev/null \
| command sed 1d \
| command cut -b3- \
| env fzf -m
)"
if
[[ -n $READLINE_LINE_NEW ]]
then
builtin bind '"\er": redraw-current-line'
builtin bind '"\e^": magic-space'
READLINE_LINE=${READLINE_LINE:+${READLINE_LINE:0:READLINE_POINT}}${READLINE_LINE_NEW}${READLINE_LINE:+${READLINE_LINE:READLINE_POINT}}
READLINE_POINT=$(( READLINE_POINT + ${#READLINE_LINE_NEW} ))
else
builtin bind '"\er":'
builtin bind '"\e^":'
fi
}
builtin bind -x '"\C-x1": __fzf_select_dir'
builtin bind '"\C-t": "\C-x1\e^\er"'
# 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
}
fps() {
if [ "$UID" != "0" ]; then
ps -f -u $UID | sed 1d | fzf -m
else
ps -ef | sed 1d | fzf -m
fi
}
fns() {
netstat -nlpt | fzf -m
}
fnso() {
netstat -aon | fzf -m
}
# 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
IFS=$'\n' 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
}
# cf - fuzzy cd from anywhere
# ex: cf word1 word2 ... (even part of a file name)
# zsh autoload function
cf() {
local file
file="$(locate -Ai -0 $@ | grep -z -vE '~$' | fzf --read0 -0 -1)"
if [[ -n $file ]]
then
if [[ -d $file ]]
then
cd -- $file
else
cd -- ${file:h}
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment