❏ 站外平台:

不喜欢 IDE?试试看 grepgitvi

作者: Yedidyah Bar David 译者: LCTT geekpi

| 2020-02-26 11:49      

一个简单又原始的脚本来用 Vim 打开你选择的文件。

像大多数开发者一样,我整天都在搜索和阅读源码。就我个人而言,我从来没有习惯过集成开发环境 (IDE),多年来,我主要使用 grep (找到文件),并复制/粘贴文件名来打开 Vi(m)。

最终,我写了这个脚本,并根据需要缓慢地对其进行了完善。

它依赖 Vimrlwrap,并使用 Apache 2.0 许可证开源。要使用该脚本,请将它放到 PATH 中,然后在文本目录下运行:

  1. grepgitvi <grep options> <grep/vim search pattern>

它将返回搜索结果的编号列表,并提示你输入结果编号并打开 Vim。退出 Vim 后,它将再次显示列表,直到你输入除结果编号以外的任何内容。你也可以使用向上和向下箭头键选择一个文件。(这对我来说)更容易找到我已经看过的结果。

与现代 IDE 甚至与 Vim 的更复杂的用法相比,它简单而原始,但它对我有用。

脚本

  1. #!/bin/bash
  2. # grepgitvi - grep source files, interactively open vim on results
  3. # Doesnt really have to do much with git, other than ignoring .git
  4. #
  5. # Copyright Yedidyah Bar David 2019
  6. #
  7. # SPDX-License-Identifier: Apache-2.0
  8. #
  9. # Requires vim and rlwrap
  10. #
  11. # Usage: grepgitvi <grep options> <grep/vim pattern>
  12. #
  13. TMPD=$(mktemp -d /tmp/grepgitvi.XXXXXX)
  14. UNCOLORED=${TMPD}/uncolored
  15. COLORED=${TMPD}/colored
  16. RLHIST=${TMPD}/readline-history
  17. [ -z "${DIRS}" ] && DIRS=.
  18. cleanup() {
  19. rm -rf "${TMPD}"
  20. }
  21. trap cleanup 0
  22. find ${DIRS} -iname .git -prune -o \! -iname "*.min.css*" -type f -print0 > ${TMPD}/allfiles
  23. cat ${TMPD}/allfiles | xargs -0 grep --color=always -n -H "$@" > $COLORED
  24. cat ${TMPD}/allfiles | xargs -0 grep -n -H "$@" > $UNCOLORED
  25. max=`cat $UNCOLORED | wc -l`
  26. pat="${@: -1}"
  27. inp=''
  28. while true; do
  29. echo "============================ grep results ==============================="
  30. cat $COLORED | nl
  31. echo "============================ grep results ==============================="
  32. prompt="Enter a number between 1 and $max or anything else to quit: "
  33. inp=$(rlwrap -H $RLHIST bash -c "read -p \"$prompt\" inp; echo \$inp")
  34. if ! echo "$inp" | grep -q '^[0-9][0-9]*$' || [ "$inp" -gt "$max" ]; then
  35. break
  36. fi
  37. filename=$(cat $UNCOLORED | awk -F: "NR==$inp"' {print $1}')
  38. linenum=$(cat $UNCOLORED | awk -F: "NR==$inp"' {print $2-1}')
  39. vim +:"$linenum" +"norm zz" +/"${pat}" "$filename"
  40. done

via: https://opensource.com/article/20/2/no-ide-script

作者:Yedidyah Bar David 选题:lujun9972 译者:geekpi 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出



最新评论

从 2025.1.15 起,不再提供评论功能
LCTT 译者
geekpi 💎💎💎💎
共计翻译: 2095.5 篇 | 共计贡献: 3750
贡献时间:2013-10-25 -> 2024-01-31
访问我的 LCTT 主页 | 在 GitHub 上关注我


返回顶部

分享到微信

打开微信,点击顶部的“╋”,
使用“扫一扫”将网页分享至微信。