Quick vim svn blame snippet

written about about 1 year ago |
6 comments

Just to prove the theory that every incredibly incomprehensible piece of code you come across is probably your own, I penned a quick svn blame vim command.

vmap gl :<C-U>!svn blame <C-R>=expand("%:p") <CR> \| sed -n <C-R>=line("'<") <CR>,<C-R>=line("'>") <CR>p <CR>

Just highlight the disgusting code in question, and bask in your own shame!

Comments

said about about 1 year ago
Posted by author

Good tip! I’ve adjusted it for cvs for my $job.

said about about 1 year ago
Posted by author

Thanks for this great feature.

Why don’t tried to display it to the left of the line directly in VIM like line number ? With a map to a key that activate or desactivate the feature. I will continue my searches for this !

Cheers !

said about about 1 year ago
Posted by author

Why don’t tried to display it to the left of the line directly in VIM like line number?

It’s much easier to get the output that’s there. It would be great to get the information on the sidebar, but I’m not sure how feasible it would be. Definitely let me know if you figure it out.

Bob
said about about 1 year ago
Posted by author

Hi,
I don’t know if you’ll find this useful, as it works better in a console vim rather than a gvim, but here you are anyway:

function SvnBlameCurrentFile()
  let fileName = expand('%')
  set nonu
  set scrollbind
  set nowrap
  15vnew
  set nu
  "Set filetype so that any special syntax files can be included.
  setf blame
  execute 'read !svn blame '.fileName.' | sed -n -e "s/\s\+\([0-9]\+\)\s\+\(\S\+\).*/\2 (\1)/p"'
  "Delete first line, which is empty.
  1d
  set scrollbind
  set nowrap
  "Switching in and out of the window seems to force an auto-resize.
  wincmd w
  wincmd w
  wincmd w
endf
map gl :call SvnBlameCurrentFile()

This will split the window, and display the blame markup in a small window on the left. If you want the blame file highlighted, defined a syntax file for ‘blame’.

Bob
said about 11 months ago
Posted by author

A slightly better version of the above script, that preserves line-numbering, and toggles on and off when called:

""
" Boinks up a blame window for the currently loaded file.
" @return void
""
function SvnBlame_blameCurrentFile()
  let fileName = expand('%')
  if match(fileName, "_svnBlame$") == -1
    let blameBufferName = fileName."_svnBlame"
    let blameBuffer = bufnr(blameBufferName)
    if blameBuffer == -1
      call SvnBlame_openBlameBuffer(fileName, blameBufferName)
    else
      wincmd w
      call SvnBlame_execPreserveNum('bwipeout '.blameBuffer)
    endif
  else
    " We're in a blame buffer, so close it.
    call SvnBlame_execPreserveNum('bwipeout '.fileName)
  endif
endfunction

""
" Execs the given command, preserving numbering
" @param string commandString
" @return void
function SvnBlame_execPreserveNum(commandString)
if &number == 1
let numbered = 1
else
let numbered = 0
endif
exe a:commandString
if numbered == 1
set nu
endif
endfunction

""
" Opens up a blame buffer called blameBufferName for the given fileName
" @param string fileName The filename to blame
" @param string blameBufferName The name to use for the blame buffer
" @return void
function SvnBlame_openBlameBuffer(fileName, blameBufferName)
set scrollbind
set nowrap
call SvnBlame_execPreserveNum("15vnew ".a:blameBufferName)
" Current window is now the blame buffer
setlocal buftype=nofile
setlocal nobuflisted
setlocal bufhidden=hide
setlocal noswapfile
setlocal autoread
set scrollbind
set nowrap
"Set filetype so that any special syntax files can be included.
setf blame
execute ‘silent read !svn blame ’.a:fileName.’ | sed -n -e “s/\s\\([0-9]\\)\s\\(\S\\).*/\2 (\1)/p”’
"Delete first line, which is empty.
1d
"Make the buffer non-modifiable.
setlocal nomodifiable
"Switching out, in then out of the blame buffer seems to force an auto-resize.
wincmd w
wincmd w
wincmd w
set nonu
endfunction

map gl :call SvnBlame_blameCurrentFile()

Jim Blomo
said about 4 months ago
Posted by author

Thanks, SvnBlame_blameCurrentFile() is just what I was looking for. The sed command didn’t work for me, and I don’t mind the order svn gives, so I changed that line to:

execute ‘read !svn blame ’.a:fileName.’ | tr -s " " “\t” | cut -f2-3’

Leave a comment:

(click here for formatting tips)