Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom script for git blame from vim

Tags:

vim

I want a minimum way of using git blame from vim (I don't want to use the whole Fugitive plugin). What I have right now is this:

This function is from the vim help page and enables me to open shell commands in a scratch buffer.

function! s:ExecuteInShell(command)
  let command = join(map(split(a:command), 'expand(v:val)'))
  let winnr = bufwinnr('^' . command . '$')
  silent! execute  winnr < 0 ? 'botright new ' . fnameescape(command) : winnr . 'wincmd w'
  setlocal buftype=nowrite bufhidden=wipe nobuflisted noswapfile nowrap number
  echo 'Execute ' . command . '...'
  silent! execute 'silent %!'. command
  silent! execute 'resize ' . line('$')
  silent! redraw
  silent! execute 'au BufUnload <buffer> execute bufwinnr(' . bufnr('#') . ') . ''wincmd w'''
  silent! execute 'nnoremap <silent> <buffer> <LocalLeader>r :call <SID>ExecuteInShell(''' . command . ''')<CR>'
  echo 'Shell command ' . command . ' executed.'
endfunction
command! -complete=shellcmd -nargs=+ Shell call s:ExecuteInShell(<q-args>)

Together with the above function I would like to do:

noremap <leader>b :Shell git blame -L line(".") - 5, line(".") + 5 %<cr>

to get a git blame window for the rows around the cursor position in the current buffer.

Now I have two questions:

1: How can I make the scratch buffer that opens read-only so I can close it using only q? I would like to make this change in the function so that all: Shell commands can be closed with q.

2: How can i get line(".") - 5 expand into current line - 5 row number?

like image 991
user3139545 Avatar asked Sep 10 '25 22:09

user3139545


1 Answers

To make a buffer read-only and not modifiable, you can put

setlocal readonly nomodifiable

at the end of your function.

In the case of your next question, you can use execute and eval

noremap <leader>b :execute "Shell git blame -L " . eval(line(".")-5)) . ",+10 %"<cr>

I recommend to read these descriptions, and help in general:

  • :h execute
  • :h eval
  • :h readonly
  • :h nomodifiable

Also here is the link to the mentioned function on wikia.

like image 137
ryuichiro Avatar answered Sep 13 '25 23:09

ryuichiro