3

I am using Neovim v0.10.0 and have the following config for git difftool:

[diff]
    tool = nvimdiff
[difftool]
    prompt = False
    trustExitCode = true
[difftool "nvimdiff"]
    cmd = nvim -d \"$LOCAL\" \"$REMOTE\"

And git mergetool:

[merge]
    tool = nvimdiff
[mergetool]
    prompt = false
    keepBackup = false

Also in init.lua, I define this global variable:

vim.g.diffmode = vim.api.nvim_get_option_value('diff', { win = 0 })
print('Diff mode: ' .. tostring(vim.g.diffmode)) // for debugging

I saw that the print statement showed vim.g.diffmode to be true only when I used git difftool, but false when I used git mergetool.

However, when I run this in the command mode:

:lua print(vim.api.nvim_get_option_value('diff', {win=0}))

It showed true in every windows opened by git difftool or git mergetool.

What am I missing?

Update

Seems like with the git version I use (2.43.2), by default, nvimdiff mergetool does not pass -d to Neovim as an argument, after customizing the command to launch Neovim, like so:

[mergetool "nvimdiff"]
    cmd = nvim -d -c '4wincmd w | wincmd J' \"$LOCAL\" \"$BASE\" \"$REMOTE\" \"$MERGED\"

I got it to work.

5
  • init.lua run at a very early stage of initialization when the windows are not all created. Maybe should you tell us what you try to achieve such that we can help you better. Commented Jun 23 at 12:17
  • @VivianDeSmedt I am trying to load nvim-lspconfig and ftplugin/java.lua only when nvim is not opened in diff mode. The reason why I define a global flag for diff mode is because after many trials and errors, I found that when I do git difftool on a java file, the ftplugin/java.lua runs twice and the the vim.api.nvim_get_option_value('diff', {win=0}) only returns true on the first run. Defining a global flag like I mentioned solved this but not the git mergetool case.
    – Lân
    Commented Jun 23 at 13:14
  • Do you still have something open in your question? How can we help you further? Otherwise maybe could you accept one of the solution using the v button next to the arrow voting buttons. It allow the question to rest :-) Commented Jun 26 at 12:56
  • 1
    @VivianDeSmedt I have figured out the problem and updated the question with the resolution.
    – Lân
    Commented Jun 28 at 14:27
  • Thanks for the feedback. We like that answered question get an accepted solution such that they can rest :-). You could accept of the solution using the v button next to the arrow voting button or publish your own and accept it yourself (you'll have to wait two days for that :-|). Thanks again for the feedback! Commented Jun 28 at 15:17

1 Answer 1

0

The init.lua is processed at a very early stage when Neovim is not completely initialized. The script that organizes the merge windows didn't run yet. There is very high chance that only the default window is created leading:

vim.api.nvim_get_option_value('diff', { win = 0 })

to return 0.

To detect the mode in which Neovim is started in the init.lua file I would recommend you to check the command line arguments table (vim.v.argv).

Here is a lua function that you can call in init.lua and that will return true if Neovim is started in diff mode.

function IsDiffMode()
    for index, arg in pairs(vim.v.argv) do
        if arg=='-d' then
            return true
        end
    end
    return false
end

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.