Ease your git workflow within vim.
From a very single vim buffer, you can perform main git operations in few key press. To name a few:
- Visualize all diffs in your git repository.
- Stage file, hunks or even just parts of a hunk using a single key press.
- Write or amend your commit message and commit in the same buffer.
Take a look at TL;DR to start using it immediately. If you encounter any performance issue, take a look in the section performance.
Some screencasts:
- A simple asciicast
- A commented video presenting vimagit (thank you Mike Hartington!)
This workflow is 100% inspired from magnificent emacs Magit plugin.
- Preview all your git changes in one unique buffer, folded at hunk level.
- Interactively stage/unstage/discard changes with one key press.
- Stage/unstage at file/hunk/line level.
- Write the commit message in the same buffer.
- From a hunk in magit buffer, jump to the file at the diff position.
- 100% VimL plugin, no external dependency (except git of course).
- Enhanced by external plugins: vim-gitgutter vim-airline
- Stable. All features are tested in continuous integration.
More to come:
- Add a push function, taking care if needed about the remote repository and branch #24 .
- Handle commit fixup! and squash!, with a smart git log popup #23 .
- Handle stash: add, pop, apply, drop #26 .
- Stage multiple hunks or file by visually selecting them #83 .
- Go through history, cherry-pick changes.
- Make vimagit more efficient for huge repositories, with a lot of diffs.
- Something is missing? Open an issue!
Why should I use vimagit, there are already plethora git plugins for vim?
- fugitive is a very complete plugin, with a lot of functions. I use it for years, and it is a fundamental tool in my workflow. But visualize your changes and staged them in broad number of files is really a pain.
- vim-gitgutter is very well integrated into vim, but without the ability to commit stages, it stays an informational plugin.
This is the minimal required set of command you must know to start playing with vimagit. See Mappings for a complete description.
To simply test vimagit, modify/add/delete/rename some files in a git repository and open vim.
:Magit
Open magit buffer with :Magit command.<C-n>
Jump to next hunk with<C-n>, or move the cursor as you like. The cursor is on a hunk.S
While the cursor is on an unstaged hunk, pressSin Normal mode: the hunk is now staged, and appears in "Staged changes" section (you can also unstage a hunk from "Staged section" withS).CC
Once you have stage all the required changes, pressCC.- Section "Commit message" is shown.
- Type your commit message in this section.
- To commit, go back in Normal mode, and press
CC(or:wif you prefer).
You just created your first commit with vimagit!
For various reasons, vimagit may be slow to refresh. A refresh happends every time you stage, unstage, commit or refresh the vimagit buffer. Currently, vimagit is quite dumb: every time the buffer is refreshed, it dumps everything and reconstruct the entire buffer. It could be smarter, but there are a lot of corner cases and it is quite a big work.
vimagit tends to be slow when:
- there is a lot of diff lines
- there are long lines
Possible solution:
let g:magit_default_fold_level = 0
Change the default fold level. When fold level is set to 0, diff content are not print in the buffer. The buffer will show the files containing diffs. If you want to see the diff relative to file, move the cursor to the filename, and press<Enter>.
In a near future, vimagit may try to be smart, and adapt the foldlevel automatically, based on the bumber of diff lines.
Any contribution is welcomed. Contribution can be bug fix, new feature, but also feedback or even tutorial! Check contribution rules here.
Now that stage feature is quite mature, I would like to introduce more commands to vimagit. For this, user feedback is very important to me, to ensure that UI is appropriate for the most of users and that vimagit fits most of git workflows (by UI, I mean default mapping, user prompt, etc.).
Proper way to discuss is on gitter and on issues opened for the new features.
The next major release of vimagit will see 3 new important features. Interested users are encouraged to discuss the best way to design these new features:
- git push: push from magit buffer with
<CP>. magit will detect the default push branch; if there is not default, or if the user used another mapping, magit will provide a way to select remote branch to push #24 - git checkout: checkout a branch with
<CH>. Like for push, a UI must be designed to select the branch, with completion of course #141 - git stash: stage what you want (files, hunks, lines, exactly the same way as for a commit), and stash them #142
Thanks for your time.
This plugin follows the standard runtime path structure, and as such it can be installed with a variety of plugin managers:
- Pathogen
git clone https://github.com/jreybert/vimagit ~/.vim/bundle/vimagit
Remember to run :Helptags to generate help tags - NeoBundle
NeoBundle 'jreybert/vimagit' - Vundle
Plugin 'jreybert/vimagit' - Plug
Plug 'jreybert/vimagit' - VAM
call vam#ActivateAddons([ 'jreybert/vimagit' ]) - manual
copy all of the files into your ~/.vim directory
vimagit buffer has modes. Mappings may have different behavior, depending on current mode and cursor position.
For the moment, vimagit counts only two modes.
This is the default mode. In this mode, you can stage and unstage hunks, refresh vimagit buffer...
In this mode, "Commit message" section is open, you can write your commit message and validate your commit.
Commit mode has two flavors.
- Normal: current commit will be a new commit.
- Amend: current commit will be meld with previous commit.
- Previous commit message is shown in "Commit message" section.
- Use this flavor if you forgot something in the previous commit.
By the way, you can also perform all stage mode actions in commit mode.
IMPORTANT: mappings can have different meanings regarding the cursor position.
There are 5 sections:
- Info: this section display some information about the git repository, like the current branch and the HEAD commit.
- Commit message: this section appears in commit mode. It contains the message to be committed.
- Staged changes: this sections contains all staged files/hunks, ready to commit.
- Unstaged changes: this section contains all unstaged and untracked files/hunks.
- Stash list: this section contains all stahes.
- It is possible to modify the content to be staged or unstaged in magit buffer, with some limitations:
- only lines starting with a + sign can be modified
- no line can be deleted
It is possible to stage and unstage part of hunk, by different ways:
- By visually selecting some lines, then [un]staging the selection with S.
- By marking some lines "to be staged" with M, then [un]staging these selected lines with S.
- [Un]staging individual lines with L.
Visual selection and marked lines have some limitations for the moment:
- Selection/marks must be within a single hunk.
- Marks not within the hunk currently staged are lost during stage process magit buffer refresh.
Function to open magit buffer. This buffer will handle the git repository including focused file. It is possible to handle multiple git repositories within one vim instance.
It takes 3 parameters:
- orientation (mandatory): it can be
- 'v', curent window is split vertically, and magit is displayed in new buffer
- 'h', curent window is split horizontally, and magit is displayed in new buffer
- 'c', magit is displayed in current buffer
- show_all_files: define how file diffs are shown by default for this session (see g:magit_default_show_all_files)
- foldlevel: set default magit buffer foldlevel for this session (see g:magit_default_fold_level)
Open magit buffer in a vertical split (see details).
Open magit buffer in current window (see details).
You can create a bash alias like magit="vim -c MagitOnly"
For each mapping, user can redefine the behavior with its own mapping. Each variable is described in vimagit help like vimagit-g:magit_nameofmapping_mapping
Following mappings are broadly set, and are applied in all vim buffers.
Open Magit buffer
Following mappings are set locally, for magit buffer only, in normal mode.
Some mappings are set for the whole magit buffer, others are set for specific section only.
CC, :w :x :wq ZZ
- From stage mode, set commit mode in normal flavor and show empty "Commit message" section.
CA
- From stage mode or commit mode: set commit mode in amend flavor, and display "Commit message" section with previous commit message. Commit will be meld with previous commit.
CF
- From stage mode: amend the staged changes into the previous commit, without modifying previous commit message.
Ctrl+n,Ctrl+p
- Move to Next or Previous hunk.
Enter
- All files are folded by default. To see the changes in a file, move cursor to the filename line, and press Enter. You can close the changes display retyping Enter.
zo,zO
- Typing zo on a file will unhide its diffs.
zc,zC
- Typing zc on a file will hide its diffs.
R
- Refresh magit buffer
- , + , 0
- Shrink,enlarge,reset diff context
q
- Close the magit buffer
?
- Toggle help showing in magit buffer
S
- If cursor is in a hunk, stage/unstage hunk at cursor position.
- If cursor is in diff header, stage/unstage whole file at cursor position.
- If some lines in the hunk are selected (using v), stage only visual selected lines (only works for staging).
- If some lines in the hunk are marked (using M), stage only marked lines (only works for staging).
- When cursor is in "Unstaged changes" section, it will stage the hunk/file.
- On the other side, when cursor is in "Staged changes" section, it will unstage hunk/file.
F
- Stage/unstage the whole file at cursor position.
- When cursor is in "Unstaged changes" section, it will stage the file.
- On the other side, when cursor is in "Staged changes" section, it will unstage file.
L
- Stage the line under the cursor.
M
- Mark the line under the cursor "to be staged".
- If some lines in the hunk are selected (using v), mark selected lines "to be staged".
- To staged marked lines in a hunk, move cursor to this hunk and press S.
DDD
- If cursor is in a hunk, discard hunk at cursor position.
- If cursor is in diff header, discard whole file at cursor position.
- Only works in "Unstaged changes" section.
I
- Add the file under the cursor in .gitignore
E
If cursor is in a hunk, cursor will move in the file containing this hunk, at the line of the beginning of the hunk.
- if the file is already visible in a window, cursor moves to this window at the hunk line
- if there is more than one window open, cursor moves to last accessed window and open buffer at the hunk line
- if there is only magit window opened, split vertically, moves cursor to new split and open buffer at the hunk line
E means 'edit'.
❗ this function is extremely powerful, just give it a try!
CC, :w :x :wq ZZ
- From commit mode, commit all staged changes with commit flavor (normal or amend) with message in "Commit message" section.
CA
- From stage mode or commit mode: set commit mode in amend flavor, and display "Commit message" section with previous commit message. Commit will be meld with previous commit.
CU
- From commit mode: go back to stage mode (current commit message will be lost).
Since vimagit 1.7, jump mappings have changed:
