neovim and lazygit, perfect harmony



This content originally appeared on DEV Community and was authored by Felix

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" })

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}}"

Pretty cool stuff!


This content originally appeared on DEV Community and was authored by Felix