fzf

https://github.com/junegunn/fzf/blob/master/ADVANCED.md

fzf 筛选 + 搜索

vim or code 打开文件

  1. #!/usr/bin/env bash
  2. # 1. Search for text in files using Ripgrep
  3. # 2. Interactively narrow down the list using fzf
  4. # 3. Open the file in Vim
  5. export FZF_DEFAULT_COMMAND='fdfind --type f'
  6. function __bat_line_range() {
  7. local line_range=10
  8. local start_line=`expr $1 - $line_range`
  9. [ $start_line -le 1 ] && start_line=1
  10. echo $start_line:`expr $1 + $line_range`
  11. }
  12. export -f __bat_line_range
  13. if [ $# -ge 1 ]; then
  14. # 使用 rg 搜索过滤一下
  15. IFS=: read -ra selected < <(
  16. rg --color=always --line-number --no-heading --smart-case "$@" |
  17. fzf --ansi \
  18. --delimiter : \
  19. --preview 'bat {1} --highlight-line {2} --line-range `__bat_line_range {2}`' \
  20. )
  21. else
  22. # 没有参数, 直接fzf 过滤
  23. IFS=: read -ra selected < <(fzf --preview "bat --style=numbers --color=always --line-range :20 {}")
  24. fi
  25. function __fzf_openfile() {
  26. echo `realpath $1`
  27. if code -v > /dev/null 2>&1; then
  28. code --goto "$1":"$2"
  29. else
  30. vim "$1" +"$2"
  31. fi
  32. echo
  33. }
  34. [ -n "${selected[0]}" ] && __fzf_openfile "${selected[0]}" "${selected[1]}"

ranger

https://github.com/ranger/ranger

设置默认配置

  1. zcz@zcz-pc:~/work$ ranger --copy-config=all
  2. creating: /home/zcz/.config/ranger/rifle.conf
  3. creating: /home/zcz/.config/ranger/commands.py
  4. creating: /home/zcz/.config/ranger/commands_full.py
  5. creating: /home/zcz/.config/ranger/rc.conf
  6. creating: /home/zcz/.config/ranger/scope.sh

绑定fzf

https://github.com/ranger/ranger/wiki/Custom-Commands

/home/zcz/.config/ranger/commands.py
fzf_select 关键词

Q键退出

https://github.com/ranger/ranger/wiki/Integration-with-other-programs#changing-directories
下面的函数放到启动脚本中

  1. #!/bin/bash
  2. function r {
  3. export EDITOR=vim
  4. if code -v > /dev/null 2>&1; then
  5. export EDITOR=code
  6. fi
  7. local IFS=$'\t\n'
  8. local tempfile="$(mktemp -t tmp.XXXXXX)"
  9. local ranger_cmd=(
  10. command
  11. ranger
  12. --cmd="map Q chain shell echo %d > "$tempfile"; quitall"
  13. )
  14. ${ranger_cmd[@]} "$@"
  15. if [[ -f "$tempfile" ]] && [[ "$(cat -- "$tempfile")" != "$(echo -n `pwd`)" ]]; then
  16. cd -- "$(cat "$tempfile")" || return
  17. fi
  18. command rm -f -- "$tempfile" 2>/dev/null
  19. }

ranger 修改编辑器

code 是vscde 的命令行, 不建议直接在 启动脚本中使用, 可以在 启动脚本中包装一个 函数。 在函数中使用这个命令。

  1. export EDITOR=vim
  2. if code -v > /dev/null 2>&1; then
  3. export EDITOR=code
  4. fi

https://unix.stackexchange.com/questions/22796/can-i-export-functions-in-bash