From d057fa03857e33109fa18f885570bd3124ffd108 Mon Sep 17 00:00:00 2001 From: Sanchayan Maity Date: Sat, 14 May 2022 19:56:49 +0530 Subject: [PATCH] nvim: fugitive: Execute commands while keeping current alt file Keep current alternate file where possible for git commands executed via fugitive. --- nvim/.config/nvim/after/plugin/fugitive.vim | 18 ++++---- nvim/.config/nvim/autoload/git.vim | 50 ++++++++++----------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/nvim/.config/nvim/after/plugin/fugitive.vim b/nvim/.config/nvim/after/plugin/fugitive.vim index 582b978..03b8add 100644 --- a/nvim/.config/nvim/after/plugin/fugitive.vim +++ b/nvim/.config/nvim/after/plugin/fugitive.vim @@ -1,8 +1,8 @@ nnoremap ga :Git add % nnoremap gA :Git reset HEAD % xnoremap gb :Git blame -nnoremap gb :Git branch -a -nnoremap gB :Git branch +nnoremap gb :keepalt Git branch -a +nnoremap gB :keepalt Git branch nnoremap gc :Git checkout -b nnoremap gC :Git checkout nnoremap gd :GtabeditGit diff %only @@ -12,27 +12,27 @@ nnoremap gE :sp:Gedit :%:p nnoremap gf :Git fetch --all --tags -f nnoremap gF :call git#git_fetch_origin_merge() nnoremap gg :Git -nnoremap gh :Git log --grep= -nnoremap gH :Git log -G +nnoremap gh :keepalt Git log --grep= +nnoremap gH :keepalt Git log -G nnoremap gk :call git#git_review() nnoremap gK :call git#git_review_fileview() -nnoremap gl :Git log --stat % -nnoremap gL :Git log --stat -n 100 +nnoremap gl :keepalt Git log --stat % +nnoremap gL :keepalt Git log --stat -n 100 xnoremap gl :call git#git_log_range() xnoremap gL :call git#git_log_named_block() nnoremap gm :0,3Git blamej nnoremap gM :call git#git_merge_origin() nnoremap gn :Git branch -m nnoremap go :call git#git_log_compare() -nnoremap gr :Ggrep! -q +nnoremap gr :keepalt Ggrep! -q nnoremap gR :call git#git_rebase_origin() nnoremap g- :call git#git_stash():e nnoremap g+ :Git stash pop stash@ nnoremap gs :Git stash list nnoremap gS :Git stash -- % -nnoremap gt :Git reflog +nnoremap gt :keepalt Git reflog nnoremap gw :Gwrite -nnoremap G :Git +nnoremap G :keepalt Git nnoremap gp :call git#git_push_to_upstream() nnoremap gP git#git_push() diff --git a/nvim/.config/nvim/autoload/git.vim b/nvim/.config/nvim/autoload/git.vim index 364edb8..5d84822 100644 --- a/nvim/.config/nvim/autoload/git.vim +++ b/nvim/.config/nvim/autoload/git.vim @@ -9,15 +9,15 @@ function! git#git_branch_delete() abort let branch = split(line, "/") if len(branch) == 1 " Handles the case for local branch - execute "Git branch -D " . branch[0] + execute "keepalt Git branch -D " . branch[0] else " Handles the case for remote remotes/remote_name/branch_name let remote = branch[1] let branch = branch[2] - execute "Git push -d " . remote . " " . branch - execute "Git branch -D " . branch + execute "keepalt Git push -d " . remote . " " . branch + execute "keepalt Git branch -D " . branch endif - silent execute "bw" + silent execute "keepalt bw" endfunction " Checkout the branch on the current line in the output of Git branch -a. @@ -26,14 +26,14 @@ function! git#git_branch_checkout() abort let branch = split(line, "/") if len(branch) == 1 " Handles the case for local branch - silent execute "Git checkout " . branch[0] + silent execute "keepalt Git checkout " . branch[0] else " Handles the case for remote remotes/remote_name/branch_name let remote = join(branch[1:2], "/") - silent execute "Git checkout " . remote - silent execute "Git checkout -b " . branch[2] + silent execute "keepalt Git checkout " . remote + silent execute "keepalt Git checkout -b " . branch[2] endif - silent execute "bw" + silent execute "keepalt bw" endfunction " Populate the command line with git command to rename branch on current line. @@ -51,11 +51,11 @@ function! git#git_branch_log() abort silent execute "bw" if len(branch) == 1 " Handles the case for local branch - execute "Git log " . branch[0] + execute "keepalt Git log " . branch[0] else " Handles the case for remote remotes/remote_name/branch_name let remote = join(branch[1:2], "/") - execute "Git log " . remote + execute "keepalt Git log " . remote endif endfunction @@ -69,11 +69,11 @@ function! git#git_branch_log_pretty() abort silent execute "bw" if len(branch) == 1 " Handles the case for local branch - execute "Git log --pretty=oneline --no-merges " . branch[0] + execute "keepalt Git log --pretty=oneline --no-merges " . branch[0] else " Handles the case for remote remotes/remote_name/branch_name let remote = join(branch[1:2], "/") - execute "Git log --pretty=oneline --no-merges " . remote + execute "keepalt Git log --pretty=oneline --no-merges " . remote endif endfunction @@ -83,7 +83,7 @@ function! git#git_cherry_pick() abort " A line in Git log is of the form commit let line = trim(getline('.')) let commit = split(line, " ") - silent execute "Git cherry-pick " . commit[1] + silent execute "keepalt Git cherry-pick " . commit[1] endfunction " Git cherry-pick a range of commits. To be run on output of Git log @@ -97,8 +97,8 @@ function! git#git_cherry_pick_range() abort endif let oldCommit = split(lines[0], " ")[:0][0] let newCommit = split(lines[-1], " ")[:0][0] - execute "Git cherry-pick " . oldCommit . "^.." . newCommit - silent execute "bw" + execute "keepalt Git cherry-pick " . oldCommit . "^.." . newCommit + silent execute "keepalt bw" endfunction " Run Git diff on the commit in the line which we are at. To be used after @@ -124,7 +124,7 @@ function! git#git_format_patch() abort " A line in Git log is of the form commit let line = trim(getline('.')) let commit = split(line, " ") - execute "Git format-patch " . commit[1] . "~1" + execute "keepalt Git format-patch " . commit[1] . "~1" endfunction " Git format-patch a range of commits. To be run on output of Git log @@ -138,8 +138,8 @@ function! git#git_format_patch_range() abort endif let oldCommit = split(lines[0], " ")[:0][0] let newCommit = split(lines[-1], " ")[:0][0] - execute "Git format-patch " . oldCommit . "~1.." . newCommit - silent execute "bw" + execute "keepalt Git format-patch " . oldCommit . "~1.." . newCommit + silent execute "keepalt bw" endfunction " The next two functions allow scoping diffs by line range of a file and named @@ -152,14 +152,14 @@ endfunction function! git#git_log_range() abort let [lnum1, col1] = getpos("'<")[1:2] let [lnum2, col2] = getpos("'>")[1:2] - silent execute "Git log --no-patch -L " . lnum1 . "," . lnum2 . ":%" + silent execute "keepalt Git log --no-patch -L " . lnum1 . "," . lnum2 . ":%" endfunction " Taken from https://learnvimscriptthehardway.stevelosh.com/chapters/33.html. " Modified and stripped to do what I needed. function! git#git_log_named_block() abort normal! `y - silent execute "Git log --no-patch -L :" . trim(shellescape(@@)) . ":%" + silent execute "keepalt Git log --no-patch -L :" . trim(shellescape(@@)) . ":%" endfunction " Compares current branch against master. For eg. git log master..current. @@ -171,7 +171,7 @@ function! git#git_log_compare() abort if (are_we_on_default == 0) echom "We are already on default branch. Nothing to compare." else - silent execute "Git log " . default[3] . ".." . current + silent execute "keepalt Git log " . default[3] . ".." . current endif endfunction @@ -236,14 +236,14 @@ function! git#git_rebase_branch() abort let branch = split(line, "/") if len(branch) == 1 " Handles the case for local branch - execute "Git rebase " . branch[0] + execute "keepalt Git rebase " . branch[0] else " Handles the case for remote remotes/remote_name/branch_name let remote = branch[1] let branch = branch[2] - execute "Git rebase " . remote . "/" . branch + execute "keepalt Git rebase " . remote . "/" . branch endif - silent execute "bw" + silent execute "keepalt bw" endfunction " Git reflog should be run first before invoking this @@ -261,7 +261,7 @@ function! git#git_reflog_restore() abort call inputrestore() let to_restore = match(restore, 'y') if (to_restore == 0) - execute "Git reset --hard " . sl2[1] + execute "keepalt Git reset --hard " . sl2[1] else echom "Not restoring to " . line endif