DEV Community

Felix
Felix

Posted on • Edited on

neovim and lazygit, perfect harmony

I enjoy using both lazygit and nvim, I even have the lazygit.nvim plugin, but a couple things are stopping my setup from being better

  1. When I open lazygit with the LazyGit command, it does not open at the current file in my buffer I opened from

  2. When I want to edit a file highlighted in lazygit, I cannot do it in the window I opened lazy git from, only in the current floating pane

This can be improved!

First, I added a new keymap to my nvim config to launch lazygit and then do a search for the filename from the current buffer

vim.keymap.set("n", "<leader>lg", function()
    --  get file name with extension
    local file = vim.fn.expand("%:t")
    vim.cmd("LazyGit")

    -- Wait a bit for LazyGit to load
    vim.defer_fn(function()
        -- search for the file, highlight, and exit search mode in lazygit
        vim.api.nvim_feedkeys("/" .. file, "t", true)
        vim.api.nvim_input("<CR>")
        vim.api.nvim_input("<ESC>")
    end, 150) -- (milliseconds)
end, { desc = "[g]it" })
Enter fullscreen mode Exit fullscreen mode

And then in my lazygit config, I used nvr (neovim remote) to launch when hitting e to edit the file

os:
  editPreset: 'nvim'
  # Using -l here to change to the previous window via ":wincmd p".
  edit: "nvr -l --remote {{filename}}"
Enter fullscreen mode Exit fullscreen mode

Pretty cool stuff!


Edit as of 27 January 2026

This stopped working as elegantly for me, the edit command was working, but would not close LazyGit and I had to close it manually (gross).

I have fixed it, if you are having issues try this workaround:

 lazygit config.yml

os:
  editPreset: 'nvim'
  edit: "if [ -n \"$NVIM\" ]; then nvim --server $NVIM --remote-send '<C-\\><C-n><cmd>close<cr>' && nvim --server $NVIM --remote {{filename}}; else nvim {{filename}}; fi"
  editInTerminal: true
Enter fullscreen mode Exit fullscreen mode

Looks a little hacky but it works!


Top comments (2)

Collapse
 
ddebajyati profile image
Debajyati Dey

Wow!

Collapse
 
craig_stevenson_3fe273a02 profile image
Craig stevenson

Legend! Thank you