From 7ea697b1d3e84ecac33111976e72a3cc3e0a65ac Mon Sep 17 00:00:00 2001 From: connermcnicholas <38020254+conner-mcnicholas@users.noreply.github.com> Date: Wed, 23 Dec 2020 05:30:19 -0800 Subject: [PATCH 1/2] Update bash_tricks.sh Your original code for vim failed to achieve the desired results on 2 different linux boxes of mine. I'd be happy for you to show me the error of my ways, but until then I thought I'd offer a version that does work. --- bash_tricks.sh | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/bash_tricks.sh b/bash_tricks.sh index ddb43a4..4b0e6ad 100644 --- a/bash_tricks.sh +++ b/bash_tricks.sh @@ -116,11 +116,18 @@ gobook() { # Have vim inspect command history # Requires line numbering to be turned on in your .gitconfig # git config --global grep.lineNumber true -vim () { - last_command=$(history | tail -n 2 | head -n 1) - if [[ $last_command =~ 'git grep' ]] && [[ "$*" =~ :[0-9]+:$ ]]; then - line_number=$(echo $* | awk -F: '{print $(NF-1)}') - /usr/bin/vim +${line_number} ${*%:${line_number}:} +vim () { + last_command=$(history | tail -n 2 | head -n 1) #The string like '1234 git grep -n foo' is not helpful alone + + remleadws="${last_command#*" "}" #These two lines iteratively remove the added leading process number + 2 spaces, plus a + remtrailtws="${remleadws%*" "}" #trailing space from the tail, yielding a clean string once again capable of eval + + file_name="$(eval $remtws | awk -F: '{print $(NF-2)}')" #the colon seperated result of running eval against the scrubbed string + line_number="$(eval $remtws | awk -F: '{print $(NF-1)}')" #allow isolation of the file_name from the line_number values + + if [[ $last_command =~ 'git grep' ]] && [[ $line_number =~ [0-9] ]] #2nd test in if clause previously yielded false inaccurately + then + /usr/bin/vim +${line_number} ${file_name} #With file_name and line_number already defined, this command is simpler logically else /usr/bin/vim "$@" fi From a0acbb866e17b633bfffd6f6cadc1db8b4cd2232 Mon Sep 17 00:00:00 2001 From: connermcnicholas <38020254+conner-mcnicholas@users.noreply.github.com> Date: Mon, 28 Dec 2020 13:34:31 -0800 Subject: [PATCH 2/2] Update bash_tricks.sh --- bash_tricks.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bash_tricks.sh b/bash_tricks.sh index 4b0e6ad..59f1d76 100644 --- a/bash_tricks.sh +++ b/bash_tricks.sh @@ -122,8 +122,8 @@ vim () { remleadws="${last_command#*" "}" #These two lines iteratively remove the added leading process number + 2 spaces, plus a remtrailtws="${remleadws%*" "}" #trailing space from the tail, yielding a clean string once again capable of eval - file_name="$(eval $remtws | awk -F: '{print $(NF-2)}')" #the colon seperated result of running eval against the scrubbed string - line_number="$(eval $remtws | awk -F: '{print $(NF-1)}')" #allow isolation of the file_name from the line_number values + file_name="$(eval $remtrailws | awk -F: '{print $(NF-2)}')" #the colon seperated result of running eval against the scrubbed string + line_number="$(eval $remtrailws | awk -F: '{print $(NF-1)}')" #allow isolation of the file_name from the line_number values if [[ $last_command =~ 'git grep' ]] && [[ $line_number =~ [0-9] ]] #2nd test in if clause previously yielded false inaccurately then