我想要一个从vim使用git blame的最小方法(我不想使用整个Fugative插件)。我现在拥有的是:
此功能来自vim帮助页面,使我能够在暂存缓冲区中打开shellcomands。
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>)
与上述功能一起,我想做:
noremap <leader>b :Shell git blame -L line(".") - 5, line(".") + 5 %<cr>
获取当前缓冲区中光标位置周围行的git blame窗口。
现在我有两个问题:
1:如何使打开的暂存缓冲区只读,这样我只能用q关闭它?我想在函数中进行此更改,以便所有:Shell命令可以用q。
关闭2:我怎样才能获得一行(“。”) - 5扩展到当前行--5行号?
答案 0 :(得分:1)
要使缓冲区为只读且不可修改,可以输入
setlocal readonly nomodifiable
在你的功能结束时。
如果是您的下一个问题,您可以使用execute
和eval
noremap <leader>b :execute "Shell git blame -L " . eval(line(".")-5)) . ",+10 %"<cr>
我建议您阅读这些说明,并help
一般:
答案 1 :(得分:1)
我使用一个简单的技巧来获取我的vim git集成: 这解决了我的git commit / add,责备和操作。
map <F5> :!git add %;git commit -m "commit" %<CR>
map <F3> :!git blame % > %.blame<CR>:vsplit %.blame<CR>
map <F4> :!git log --abbrev-commit % > %.log<CR>:vsplit %.log<CR>
答案 2 :(得分:0)
我在这里有一些东西,可以复制脚本并将其添加到.vimrc文件中。
"======================================================
" Function runs git blame on file in current buffer and
" puts output into a new window
" move to current line in git output
" (can't edit output)
"======================================================
command! -nargs=* Blame call s:GitBlame()
function! s:GitBlame()
let cmdline = "git blame -w " . bufname("%")
let nline = line(".") + 1
botright new
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
execute "$read !" . cmdline
setlocal nomodifiable
execute "normal " . nline . "gg"
execute "set filetype=cpp"
endfunction
"======================================================
" function runs git show on report of git blame;
" the first token of a line is taken as SHA checsum of the
" git commit
"======================================================
command! -nargs=* GShow call s:GitShowFromBlame()
function! s:GitShowFromBlame()
let curline = getline( "." )
let tokens = split(curline)
let cmdline = "git show -w " . tokens[0]
"botright new
"topleft new
"vsplit new
"vnew new
vertical new
setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
execute "$read !" . cmdline
setlocal nomodifiable
execute "normal 1gg"
execute "set filetype=cpp"
endfunction