21

This screen I got after running the :Gstatus command:

enter image description here

Then I moved the cursor to init.vim file and pressed the p key, i.e. run git add –patch for the current file.

Now I have this screen:

enter image description here

As you can see, here's the usual interaction for git add patch in a terminal. In the terminal I can press y to add the current chunk of code to the stage area, press n to skip the current one and so on.

But here in fugitive.vim I can't figure out what I should press. Should I press :w to add the current chunk to stage? Or should I press -, or :q to skip?

P.S. I'm using Neovim

4
  • 2
    See if this vimcast is helpful
    – D. Ben Knoble
    Jan 18, 2018 at 13:21
  • Also, what happens if you experiment a bit?
    – D. Ben Knoble
    Jan 18, 2018 at 19:05
  • When I try this in my vim (not nvim) I get booted out to the full terminal, then when finished it takes me back to vim.
    – D. Ben Knoble
    Jan 20, 2018 at 10:48
  • @DavidBenKnoble, forgot to mention, i'm under neovim, it's not booting out to the terminal
    – megas
    Jan 20, 2018 at 17:40

2 Answers 2

29

Short Answer

Just use the normal interactive buttons. In (regular) vim, pressing p drops you into the interactive terminal patch, so it's the interface you're used to.

For neovim users: Just enter insert mode (i), then it works as a terminal, the cursor goes to 'stage this hunk...' line.

In newer versions, you can use P instead, and the interface is builtin (not an external terminal).

Long Answer: Don't use p!

If you're already using , I'd highly recommend learning how to use it properly powerfully.

From the :Git window, you can trigger a number of commands.

Pro tip: ) moves to the next file in the window (and ( previous). g? brings up a list of other shortcuts.

Another answer covers recent updates to the status window that may be even easier to use for patch-like adding than the diff method below, using - and =. This is my preferred method.

You can hit dd on any file to bring up a :Gdiffsplit window. This window will show you the differences between the file on disk and the file in the git index. You add pieces to the git index (simulating git add --patch), but in a much more "vim-like" way.

Pro tip: in any vimdiff window (including :Gdiffsplit), use ]c and [c to jump between diffs

The working copy is always on the right, and the version in the index is always on the left. Note that updating the index ('patching' pieces of the file) is as simple as pulling changes from the working copy to the index. We have the commands do and dp for this. Which to run depends on where you are.

  • do (or 'diff-obtain'--:diffget) pulls the nearby diff over from the other side
  • dp (or 'diff-put'--:diffput) puts the nearby diff in the other side

The result? From the working copy, use dp to add changes you want to stage, or do to reset changes. (You can actually also run these from the index, but it's slightly less intuitive to me.)

:diffget and :diffput work with ranges (read: visual mode), if you don't want to stage an entire diff hunk at once.

Tip: if the colors look weird, try :diffupdate

I added changes to the index, now what?

Write the index file (e.g. :w) and watch the status window update!

References

8
  • I wish neovim could go to terminal, much easier for me
    – megas
    Jan 20, 2018 at 17:56
  • 2
    Finally I found the answer! Just enter insert mode (i), then it works as a terminal, the cursor goes to 'stage this hunk...' line
    – megas
    Jan 20, 2018 at 18:06
  • 3
    Much helpful, the :diffput and :diffget part @D.BenKnoble
    – 3N4N
    May 14, 2018 at 13:48
  • 3
    I just learned a ton. Thank you @D.BenKnoble. Dec 10, 2018 at 21:49
  • 2
    With the new update, I can use :Git to enter the status window and then trigger inline diff with = and add hunk to stage by s while cursor on hunk
    – Hritik
    Nov 13, 2023 at 19:10
13

Here's another way to do it, all within fugitive. I believe some of this is new with the 3.0 release on Aug 20, 2019. Someone had created a "What's New?" video, which I sadly can't find now. The :Git command received a lot of attention.

In the :Git window, you can use these subcommands. There are many others, but these serve me well.

  • <C-N> or <C-P> to jump to the next/previous file (as mentioned above)
  • - on a file, stages (or unstages) the entire file.
  • = shows the git diff of the file your cursor is on.
  • - on a hunk, stages (or unstages) the hunk.
  • - in a visual selection, stages (or unstages) the selected lines in the hunk.
  • cvc - commits the staged changes in verbose mode. I like the last chance it gives me to verify the right changes have been staged, and it helps inform the commit message.

I really like the inline diffing in the :Git window. For me, it's easier than dealing with the separate windows and the do and dp commands.

Before I learned this, I would always go to the shell and do a git commit -pv, but now I can stay entirely in Vim.

4
  • :Gstatus has been replaced with :Git status and the above behavior does not seem to still exist.
    – Moberg
    Dec 28, 2021 at 10:04
  • 1
    Actually, it's the new :Git command that opens the window. :Git status just echoes the status to the screen without opening a new window. I'll edit.
    – Phil R
    Dec 30, 2021 at 18:16
  • Thanks, I saw a lot of references to :Gstatus but no mention that the interactive window was now accessed through :Git, great! :)
    – Moberg
    Jan 1, 2022 at 21:37
  • Didn't know about the "visual selection + - operator", but it's exactly what I needed. Thanks!
    – toraritte
    Apr 23, 2023 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.