不喜歡 IDE?試試看 grepgitvi

不喜歡 IDE?試試看 grepgitvi

一個簡單又原始的腳本來用 Vim 打開你選擇的文件。-- Yedidyah Bar David(作者)


像大多數開發者一樣,我整天都在搜索和閱讀源碼。就我個人而言,我從來沒有習慣過集成開發環境 (IDE),多年來,我主要使用 grep (找到文件),並複製/粘貼文件名來打開 Vi(m)。

最終,我寫了這個腳本,並根據需要緩慢地對其進行了完善。

它依賴 Vim 和 rlwrap ,並使用 Apache 2.0 許可證開源。要使用該腳本,請 將它放到 PATH 中 ,然後在文本目錄下運行:

<code>grepgitvi <grep> <grep>/<grep>/<code>

它將返回搜索結果的編號列表,並提示你輸入結果編號並打開 Vim。退出 Vim 後,它將再次顯示列表,直到你輸入除結果編號以外的任何內容。你也可以使用向上和向下箭頭鍵選擇一個文件。(這對我來說)更容易找到我已經看過的結果。

與現代 IDE 甚至與 Vim 的更復雜的用法相比,它簡單而原始,但它對我有用。

腳本

<code>#!/bin/bash

# grepgitvi - grep source files, interactively open vim on results
# Doesnt really have to do much with git, other than ignoring .git
#
# Copyright Yedidyah Bar David 2019
#
# SPDX-License-Identifier: Apache-2.0
#
# Requires vim and rlwrap
#
# Usage: grepgitvi <grep> <grep>
#

TMPD=$(mktemp -d /tmp/grepgitvi.XXXXXX)
UNCOLORED=${TMPD}/uncolored
COLORED=${TMPD}/colored

RLHIST=${TMPD}/readline-history

[ -z "${DIRS}" ] && DIRS=.

cleanup() {
        rm -rf "${TMPD}"
}

trap cleanup 0

find ${DIRS} -iname .git -prune -o \\! -iname "*.min.css*" -type f -print0 > ${TMPD}/allfiles

cat ${TMPD}/allfiles | xargs -0 grep --color=always -n -H "$@" > $COLORED
cat ${TMPD}/allfiles | xargs -0 grep -n -H "$@" > $UNCOLORED

max=`cat $UNCOLORED | wc -l`
pat="${@: -1}"

inp=''
while true; do
        echo "============================ grep results ==============================="
        cat $COLORED | nl
        echo "============================ grep results ==============================="
        prompt="Enter a number between 1 and $max or anything else to quit: "
        inp=$(rlwrap -H $RLHIST bash -c "read -p \"$prompt\" inp; echo \\$inp")
        if ! echo "$inp" | grep -q '^[0-9][0-9]*$' || [ "$inp" -gt "$max" ]; then
                break
        fi

        filename=$(cat $UNCOLORED | awk -F: "NR==$inp"' {print $1}')
        linenum=$(cat $UNCOLORED | awk -F: "NR==$inp"' {print $2-1}')
        vim +:"$linenum" +"norm zz" +/"${pat}" "$filename"

done/<grep>/<grep>/<code>

via: opensource.com

作者: Yedidyah Bar David 選題: lujun9972 譯者: geekpi 校對: wxy

本文由 LCTT 原創編譯, Linux中國 榮譽推出


分享到:


相關文章: