Search code examples
luaneovim

How to enable keymap for Lazygit?


I am trying to enable a keymap for Lazygit. I have added the following into my tmux.conf:

set -g @plugin 'kdheepak/lazygit.nvim'

And have added the following to my keymaps.lua:

-- Keymap for launching Lazygit by hitting space+gg
vim.keymap.set('n', '<space>gg', ':LazyGit<CR>')

However, I get a pop-up saying LazyGit isn't an available command. If I open a terminal and simply run the Lazygit command, it does open Lazygit.

enter image description here

I know I am missing something, I just don't know what. I am using this guy's pre-built configuration for Tmux, Nvim and so on: https://github.com/omerxx/dotfiles

I don't want to be spoon-fed necessarily, but if someone could take a peek at the github repository, and tell me what I need to add where. I am very new to Nvim and all of that ecosystem, learning by troubleshooting, but I've been breaking my head over this the past few hours.

Thank you all.


Solution

  • If using Lazygit plugin:

    You would put this in your nvim/lua/plugins/lazy.lua file since your package manager is Lazy.

    # nvim/lua/plugins/lazy.lua
    
    require("lazy").setup({
        -- Rest of your requirements...
        {
            "kdheepak/lazygit.nvim",
            cmd = {
                "LazyGit",
                "LazyGitConfig",
                "LazyGitCurrentFile",
                "LazyGitFilter",
                "LazyGitFilterCurrentFile",
            },
            -- optional for floating window border decoration
            dependencies = {
                "nvim-lua/plenary.nvim",
            },
            -- setting the keybinding for LazyGit with 'keys' is recommended in
            -- order to load the plugin when the command is run for the first time
            keys = {
               { "<leader>lg", "<cmd>LazyGit<cr>", desc = "LazyGit" }
            }
        },
        -- Rest of your requirements...
    })
    

    Run :Lazy install at the end.

    If using Lazygit installed separately on the system:

    Running :terminal lazygit will open up Lazygit for you.

    vim.keymap.set('n', '<space>gg', ':terminal lazygit<CR>')
    

    Or in my personal config I run it on Togglterm in a floating/split terminal.

    local Terminal = require("toggleterm.terminal").Terminal
    
    -- Lazygit
    local lazygit = Terminal:new({
        cmd = "lazygit",
        hidden = true,
        direction = "float",
        close_on_exit = true,
    })
    
    function _LAZYGIT_TOGGLE()
        lazygit:toggle()
    end
    
    keymap("n", "<leader>tg", "<cmd>lua _LAZYGIT_TOGGLE()<CR>", opts)