I would like to know if i could select some code in visual mode and
git add it ?
I checked with fugitive.vim but i didn't find how to do it.
Is it possible ? Or is there any other plugin to do it ?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.Sign up to join this community
There might be other ways, but this approach lets you to do more than adding a chunk, which is why I tend to use it quite a lot.
:Gdiff command. It will open a split with version of current file that's currently in the index to the left/top of the original window.
While in original window (right or bottom one), perform visual selection of changes you'd like to
:diffput command, which can operate on a range. (
dp key doesn't work in Visual mode.)
Close that second window saving changes to update file in the index. Here you can edit it the way you like before saving, it doesn't have to exactly match original or new version of the file.
:diffput might not always give you what you would expect depending on the structure of changes (especially if you're committing only part of contiguous block of changes). So don't forget to validate commit. In most practical situations it works fine though.
You can use
:Gdiff to stage only portions of a file. Running
:Gdiff will show you the current file "diff-ed" with the stage/index. You can simply use Vim's diff commands (
:diffget) to move the hunk to the stage/index. Then you write the stage/index file like you would any other buffer,
fugitive.vim now allows for staging a selection of a hunk with visual mode.
=to toggle). This will only show the changed hunks plus some extra lines of context above and below the changed lines.
vto start visual mode and select the lines you want to stage with
sto stage the visual selection (or
Stage Complete Hunk
Stage Part of an Additions-only Hunk (since 8/2019)
'<,'>are as always inserted for you)
Stage Part of any Hunk via Preview Window (since 8/2019)
As you specifically asked about solutions that use other plugins, I'll note that you can also stage visual selections using vimagit, which is a Git plugin (based on the other editor's popular Magit plugin) centered around viewing, staging, and commiting changes.
To stage a visual selection using Vimagit:
:Magit command to open a new buffer that displays all the changed hunks in your repository (not just in one file),
Locate the lines you want to stage and perform your
Press the "stage" mapping: