| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
A Vim plugin which shows a git diff in the 'gutter' (sign column). It shows which lines have been added, modified, or removed. You can also preview, stage, and undo individual hunks. The plugin also provides a hunk text object.
The signs are always up to date and the plugin never saves your buffer.
Features:
Constraints:
In the screenshot above you can see:
Before installation, please check your Vim supports signs by running :echo has('signs'). 1 means you're all set; 0 means you need to install a Vim with signs support. If you're compiling Vim yourself you need the 'big' or 'huge' feature set. MacVim supports signs.
You install vim-gitgutter like any other vim plugin.
cd ~/.vim/bundle git clone git://github.com/airblade/vim-gitgutter.git
Edit your plugin manifest (voom edit) and add:
airblade/vim-gitgutter
Place this in your .vimrc:
Plug 'airblade/vim-gitgutter'Then run the following in Vim:
:source % :PlugInstall
Place this in your .vimrc:
NeoBundle 'airblade/vim-gitgutter'Then run the following in Vim:
:source % :NeoBundleInstall
Copy vim-gitgutter's subdirectories into your vim configuration directory:
cd /tmp && git clone git://github.com/airblade/vim-gitgutter.git cp -r vim-gitgutter/* ~/.vim/
See :help add-global-plugin.
When you make a change to a file tracked by git, the diff markers should appear automatically. The delay is governed by vim's updatetime option; the default value is 4000, i.e. 4 seconds, but I suggest reducing it to around 100ms (add set updatetime=100 to your vimrc).
You can jump between hunks with [c and ]c. You can preview, stage, and undo hunks with <leader>hp, <leader>hs, and <leader>hu respectively.
You cannot unstage a staged hunk.
You can explicitly turn vim-gitgutter off and on (defaults to on):
To toggle vim-gitgutter per buffer:
You can turn the signs on and off (defaults to on):
And you can turn line highlighting on and off (defaults to off):
Note that if you have line highlighting on and signs off, you will have an empty sign column – more accurately, a sign column with invisible signs. This is because line highlighting requires signs and Vim always shows the sign column even if the signs are invisible.
If you switch off both line highlighting and signs, you won't see the sign column. That is unless you configure the sign column always to be there (see Sign Column section).
To keep your Vim snappy, vim-gitgutter will suppress the signs when a file has more than 500 changes. As soon as the number of changes falls below the limit vim-gitgutter will show the signs again. You can configure the threshold with:
let g:gitgutter_max_signs = 500 " default valueYou can jump between hunks:
Both of those take a preceding count.
To set your own mappings for these, for example ]h and [h:
nmap ]h <Plug>GitGutterNextHunk
nmap [h <Plug>GitGutterPrevHunkYou can stage or undo an individual hunk when your cursor is in it:
See the FAQ if you want to unstage staged changes.
The . command will work with both these if you install repeat.vim.
To set your own mappings for these, for example if you prefer the mnemonics hunk-add and hunk-revert:
nmap <Leader>ha <Plug>GitGutterStageHunk
nmap <Leader>hr <Plug>GitGutterUndoHunkAnd you can preview a hunk's changes with <Leader>hp. You can of course change this mapping, e.g:
nmap <Leader>hv <Plug>GitGutterPreviewHunkA hunk text object is provided which works in visual and operator-pending modes.
To re-map these, for example to ih and ah:
omap ih <Plug>GitGutterTextObjectInnerPending
omap ah <Plug>GitGutterTextObjectOuterPending
xmap ih <Plug>GitGutterTextObjectInnerVisual
xmap ah <Plug>GitGutterTextObjectOuterVisualIf you don't want vim-gitgutter to set up any mappings at all, use this:
let g:gitgutter_map_keys = 0Finally, you can force vim-gitgutter to update its signs across all visible buffers with :GitGutterAll.
See the customisation section below for how to change the defaults.
Use the GitGutterFold command to fold all unchanged lines, leaving just the hunks visible. Use zr to unfold 3 lines of context above and below a hunk.
Execute GitGutterFold a second time to restore the previous view.
You can customise:
Please note that vim-gitgutter won't override any colours or highlights you've set in your colorscheme.
By default vim-gitgutter will make the sign column look like the line number column.
To customise your sign column's background color, first tell vim-gitgutter to leave it alone:
let g:gitgutter_override_sign_column_highlight = 0And then either update your colorscheme's SignColumn highlight group or set it in your vimrc:
highlight SignColumn ctermbg=whatever " terminal Vim
highlight SignColumn guibg=whatever " gVim/MacVimBy default the sign column will appear when there are signs to show and disappear when there aren't. To always have the sign column, add to your vimrc:
if exists('&signcolumn') " Vim 7.4.2201
set signcolumn=yes
else
let g:gitgutter_sign_column_always = 1
endifBy default vim-gitgutter uses your colourscheme's Diff* highlight groups' foreground colours for the signs' foreground colours. For example, your DiffAdd foreground colour will be used for the + sign's foreground colour.
The signs' background colours will all be set to the sign column's background colour.
If you don't like the default colours, you can either fix your colourscheme's Diff* highlights or configure your own GitGutter* highlight groups. These groups are:
GitGutterAdd " an added line (default: links to DiffAdd)
GitGutterChange " a changed line (default: links to DiffChange)
GitGutterDelete " at least one removed line (default: links to DiffDelete)
GitGutterChangeDelete " a changed line followed by at least one removed line (default: links to GitGutterChange)You can either set these with highlight GitGutterAdd {key}={arg}... or link them to existing highlight groups with, say, highlight link GitGutterAdd MyDiffAdd.
To get vim-gitgutter's original colours (based on git-diff's colours in my terminal):
highlight GitGutterAdd guifg=#009900 guibg=<X> ctermfg=2 ctermbg=<Y>
highlight GitGutterChange guifg=#bbbb00 guibg=<X> ctermfg=3 ctermbg=<Y>
highlight GitGutterDelete guifg=#ff2222 guibg=<X> ctermfg=1 ctermbg=<Y>– where you would replace <X> and <Y> with the background colour of your SignColumn in the gui and the terminal respectively. For example, with the solarized colorscheme and a dark background, guibg=#073642 and ctermbg=0.
To customise the symbols, add the following to your ~/.vimrc:
let g:gitgutter_sign_added = 'xx'
let g:gitgutter_sign_modified = 'yy'
let g:gitgutter_sign_removed = 'zz'
let g:gitgutter_sign_removed_first_line = '^^'
let g:gitgutter_sign_modified_removed = 'ww'Similarly to the signs' colours, set up the following highlight groups in your colorscheme or ~/.vimrc:
GitGutterAddLine " default: links to DiffAdd
GitGutterChangeLine " default: links to DiffChange
GitGutterDeleteLine " default: links to DiffDelete
GitGutterChangeDeleteLine " default: links to GitGutterChangeLineDefault, i.e. DiffChangeFor example, in some colorschemes the DiffText highlight group is easier to read than DiffChange. You could use it like this:
highlight link GitGutterChangeLine DiffTextBy default buffers are diffed against the index. However you can diff against any commit by setting:
let g:gitgutter_diff_base = '<commit SHA>'If you want to pass extra arguments to git when running git diff, do so like this:
let g:gitgutter_git_args = '--git-dir-""'If you want to pass extra arguments to git diff, for example to ignore whitespace, do so like this:
let g:gitgutter_diff_args = '-w'To disable all key mappings:
let g:gitgutter_map_keys = 0See above for configuring maps for hunk-jumping and staging/undoing.
If you use an alternative to grep, you can tell vim-gitgutter to use it here.
" Default:
let g:gitgutter_grep = 'grep'Add let g:gitgutter_enabled = 0 to your ~/.vimrc.
Add let g:gitgutter_signs = 0 to your ~/.vimrc.
Add let g:gitgutter_highlight_lines = 1 to your ~/.vimrc.
By default diffs are run asynchronously. To run diffs synchronously instead:
let g:gitgutter_async = 0You can map an operator to do whatever you want to every line in a hunk.
Let's say, for example, you want to remove trailing whitespace.
function! CleanUp(...)
if a:0 " opfunc
let [first, last] = [line("'["), line("']")]
else
let [first, last] = [line("'<"), line("'>")]
endif
for lnum in range(first, last)
let line = getline(lnum)
" clean up the text, e.g.:
let line = substitute(line, '\s\+$', '', '')
call setline(lnum, line)
endfor
endfunction
nmap <silent> <Leader>x :set opfunc=CleanUp<CR>g@Then place your cursor in a hunk and type \xic (assuming a leader of \).
Alternatively you could place your cursor in a hunk, type vic to select it, then :call CleanUp().
You can write a command to do whatever you want to every changed line in a file.
function! GlobalChangedLines(ex_cmd)
for hunk in GitGutterGetHunks()
for lnum in range(hunk[2], hunk[2]+hunk[3]-1)
let cursor = getcurpos()
silent! execute lnum.a:ex_cmd
call setpos('.', cursor)
endfor
endfor
endfunction
command -nargs=1 Glines call GlobalChangedLines(<q-args>)Let's say, for example, you want to remove trailing whitespace from all changed lines:
:Glines s/\s\+$//]c and [c jump from one hunk to the next in the current buffer. You can use this code to jump to the next hunk no matter which buffer it's in.
function! NextHunkAllBuffers()
let line = line('.')
GitGutterNextHunk
if line('.') != line
return
endif
let bufnr = bufnr('')
while 1
bnext
if bufnr('') == bufnr
return
endif
if !empty(GitGutterGetHunks())
normal! 1G
GitGutterNextHunk
return
endif
endwhile
endfunction
function! PrevHunkAllBuffers()
let line = line('.')
GitGutterPrevHunk
if line('.') != line
return
endif
let bufnr = bufnr('')
while 1
bprevious
if bufnr('') == bufnr
return
endif
if !empty(GitGutterGetHunks())
normal! G
GitGutterPrevHunk
return
endif
endwhile
endfunction
nmap <silent> ]c :call NextHunkAllBuffers()<CR>
nmap <silent> [c :call PrevHunkAllBuffers()<CR>How can I turn off realtime updates?
Add this to your vim configuration (in an /after/plugin directory):
" .vim/after/plugin/gitgutter.vim
autocmd! gitgutter CursorHold,CursorHoldII turned off realtime updates, how can I have signs updated when I save a file?
If you really want to update the signs when you save a file, add this to your vimrc:
autocmd BufWritePost * GitGutterWhy can't I unstage staged changes?
This plugin is for showing changes between the working tree and the index (and staging/undoing those changes). Unstaging a staged hunk would require showing changes between the index and HEAD, which is out of scope.
Why are the colours in the sign column weird?
Your colorscheme is configuring the SignColumn highlight group weirdly. Please see the section above on customising the sign column.
What happens if I also use another plugin which uses signs (e.g. Syntastic)?
Vim only allows one sign per line. Before adding a sign to a line, vim-gitgutter checks whether a sign has already been added by somebody else. If so it doesn't do anything. In other words vim-gitgutter won't overwrite another plugin's signs. It also won't remove another plugin's signs.
Here are some things you can check:
If this plugin has helped you, or you'd like to learn more about Vim, why not check out this screencast I wrote for PeepCode:
This was one of PeepCode's all-time top three bestsellers and is now available at Pluralsight.
You can read reviews on my website.
Copyright Andrew Stewart, AirBlade Software Ltd. Released under the MIT licence.
| Back | FazBrowse Home | New Git URL |