Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update bash_tricks.sh #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions bash_tricks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 $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
/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
Expand Down