Hot answers tagged

67 votes
Accepted

How do I open a file from another git branch?

You can use :Gedit/:Gsplit/:Gvsplit/... with the form {revision}:{filename} :Gedit branch:/foo/bar.c Note: If the file is the same as the current file you can abbreviate the command like so: :Gsplit ...
29 votes

Git Fugitive how to git add a visually selected chunk of code

fugitive.vim now allows for staging a selection of a hunk with visual mode. Open the git summary with :Git (or :G) Expand the file which contains the lines you want to stage with > (or = to toggle)...
  • 423
25 votes
Accepted

Git Fugitive how to git add a visually selected chunk of code

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. Run :Gdiff command. It will open a split with version of current file ...
  • 1,648
19 votes

How do I open a file from another git branch?

This is a little more broad than what OP asked, but for people not wanting to use plugins, and possibly other revision control systems, this little snippet tends to work fairly well: :new :r! git ...
  • 8,112
19 votes
Accepted

Easiest way to switch git branches

To summarize the answers, here are the ways to accomplish this: Vanilla Terminal Vim in a shell with job control Press CTRL-Z in normal mode will drop you back in to the shell and suspend Vim. After ...
  • 1,152
13 votes

Easiest way to switch git branches

Use the vim-merginal plugin (fugitive extension). It offers interactive TUI for: Viewing the list of branches Checking out branches from that list Creating new branches Deleting branches Merging ...
  • 584
13 votes

Git Fugitive how to git add a visually selected chunk of code

Vimcasts has a great series on Fugitive.vim. The episode Fugitive.vim - working with the git index would be the the most helpful for your question. You can use :Gdiff to stage only portions of a file....
11 votes
Accepted

Keeping personal vimrc and syntax files etc in source control?

Since 7.4, all your configuration files (even your vimrc) are expected to be in this directory: %userprofile%\vimfiles\ But the name and location of that directory only matter to Vim. From Git's ...
  • 33.4k
11 votes

Git Fugitive how to git add a visually selected chunk of code

Stage Complete or Partial Hunks with vim-gitgutter Stage Complete Hunk <Leader>hs or :GitGutterStageHunk Stage Part of an Additions-only Hunk (since 8/2019) Visual mode {Visual}<Leader&...
  • 4,110
10 votes
Accepted

Git mergetool vimdiff command

I used the trick from this SO question to see what the command line was, and got this: :exe '!tr "\0" " " </proc/' . getpid() . '/cmdline' :!tr "\0" " " </proc/23932/cmdline gvim -f -d -c ...
  • 24k
9 votes
Accepted

How to detect whether swp files contain unsaved changes?

vim -r at the command line will list all swap files in the current directory and temporary directories, and whether they contain any unsaved changes. Look for the line that says modified: no/YES. I ...
  • 990
9 votes
Accepted

Open git tracked files inside editor

You could use the args command instead: command G execute ":args `git diff --name-only`" The first file should be opened, and if you run :ls after executing this, you should see other files listed ...
  • 24k
8 votes

Easiest way to switch git branches

You should embrace your terminal. If you use CTRL-Z, it will background Vim (or whichever process you are currently running), then you can run any commands you want, the fg to bring the process back ...
8 votes
Accepted

VimDiff incorrectly showing all lines as changed

I think the most likely culprit is another buffer still in diff-mode. This can happen if you're looking at multiple diffs, one after another, and don't turn off diff-mode between diffs. Disable diff-...
  • 7,290
8 votes

How to detect whether swp files contain unsaved changes?

Is it possible to only have .*.swp-files, when file on disk and file in vim are different? Yes. The snippet below (adapted from tpope's vimrc) will disable the swap file for a buffer when it isn'...
  • 10.4k
8 votes
Accepted

Populate a git-commit-template with variables

It's possible with some scripting. When editing a commit message, a temporary file .git/COMMIT_EDITMSG is created with the contents of the commit.template file. With autocmd we can execute a script ...
  • 6,420
7 votes
Accepted

Can I detect whether current vim instance is launched by git?

$ git config --global core.editor "vim -Nu NONE" tells Git to use Vim without sourcing your vimrc (-u NONE) and in "nocompatible" mode (-N). And, well… you could even add something to start directly ...
  • 33.4k
7 votes

Easiest way to switch git branches

The fugitive Git checkout <branch> has a downside that it doesn't autocomplete the branch name. Using fzf.vim I've created this command: function! s:changebranch(branch) execute 'Git ...
  • 171
7 votes
Accepted

Warning when opening commit message with Neovim

If I run :set spell, I get: spellfile#LoadFile(): There is no writable spell directory Shall I create /Users/adamharris/.nvim/spell (Y)es, [N]o: Select y Cannot find spell file for "en" in utf-8 ...
  • 1,717
7 votes

Git Fugitive how to git add a visually selected chunk of code

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 ...
  • 29k
7 votes
Accepted

How to load list of files in commit into quickfix

You could use the following function which has the advantage of not changing the state of your local git repo: function! CommitQF(...) " Get the commit hash if it was specified let commit = a:...
  • 46.8k
7 votes
Accepted

IDE-like inline diff highlighting in Vim

You can do this with the vim-gitgutter plugin. If you're not using git, then Signify has a similar feature. After installing vim-gitgutter, you can switch the highlighting on and off with the ...
  • 29k
6 votes

Is there a way to hook ctag changes to git branch checkouts and pulls?

Depending on how you update your ./tags file: If you do not commit your ./tags file to your branch/repository you can use a git hook that calls ctags -R . on each pull/checkout you do - this way your ...
  • 396
6 votes

Using vim diff to compare the working directory to the HEAD in git?

To view unstaged modifications to your git working tree in vimdiff, try: git difftool --tool=vimdiff or for staged changes git difftool --tool=vimdiff [ --staged | --cached ] These commands are ...
6 votes

Turn Vim's multiple undo history into git commits?

Here's a quick and dirty function that does what you request: function! UndoCommits(steps) for i in range(a:steps) undo endfor for i in range(a:steps) write ...
  • 29k
5 votes
Accepted

How do I get back to the current working copy while viewing git revisions?

The Gedit command is here for you. It takes an argument which is the revision you want to use, using the same logic as git-rev-parse (meaning you can use it without argument to get back to your ...
  • 15.3k
5 votes
Accepted

Vim Function to Send Highlighted Text to a Git Commit

GitLocations won't work as expected, since you'll be calling Vim's :find, not the find command. You might try something like: return system('find $HOME -name ".git" -printf "%h\n"') And you could ...
  • 24k
5 votes

How to load list of files in commit into quickfix

Here are two related approaches. Both use git-* scripts; if you put them on your PATH, you can invoke them as if they were subcommands of git. Use the contrib script git-jump The git-jump script ...
  • 21.8k
5 votes
Accepted

Create a specific .vimrc for git commits usage?

It is strange that the error just appear with git commit -- I guess you have two versions of Vim installed, and the one used by git is older/have less features and thus complain about that option. ...
  • 6,470
5 votes
Accepted

`vimdiff` nor `nvim -d` are working as an external diff tool for git

I tried git config --global diff.tool vimdiff3 git config --global difftool.vimdiff3.path nvim git config --global merge.tool vimdiff3 git config --global mergetool.vimdiff3.path It worked like a ...
  • 175

Only top scored, non community-wiki answers of a minimum length are eligible