6

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

  • 1
    See if this vimcast is helpful – D. Ben Knoble Jan 18 '18 at 13:21
  • Also, what happens if you experiment a bit? – D. Ben Knoble Jan 18 '18 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 '18 at 10:48
  • @DavidBenKnoble, forgot to mention, i'm under neovim, it's not booting out to the terminal – megas Jan 20 '18 at 17:40
11

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.

Long Answer: Don't use p!

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

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

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

Now, you can hit D on any file to bring up a :Gdiff 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 :Gdiff), 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

  • I wish neovim could go to terminal, much easier for me – megas Jan 20 '18 at 17:56
  • @megas Yeah I'm not sure how fugtive handles neovim (it's not called out in the docs). My other comment still remains--what happens if you try pushing the 'regular' buttons (y,n,q) ? (Or adapt to a different way of 'patching') – D. Ben Knoble Jan 20 '18 at 17:59
  • 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 '18 at 18:06
  • 3
    Much helpful, the :diffput and :diffget part @D.BenKnoble – klaus May 14 '18 at 13:48
  • 1
    I just learned a ton. Thank you @D.BenKnoble. – Jesse Atkinson Dec 10 '18 at 21:49
4

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 :Gstatus command received a lot of attention.

In the :Gstatus 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 :Gstatus 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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