ThePrimeagen / harpoon

MIT License
7.17k stars 384 forks source link

'Esc' in normal mode jumps to first harpoon entry #624

Open db757 opened 4 months ago

db757 commented 4 months ago

Whenever I have at least one item in the harpoon list, hitting Esc in normal mode in any buffer jumps to the buffer that's in the first harpoon entry. If there's no entries in the list then nothing happens (as expected).

Here's my lazy config:

return
{
    'ThePrimeagen/harpoon',
    branch = 'harpoon2',
    dependencies = { 'nvim-lua/plenary.nvim' },
    config = function()
                local harpoon = require('harpoon')
        harpoon:setup({
            settings = {
                save_on_toggle = true,
                sync_on_ui_close = true,
            },
        })

        local nmap = function(key, cmd, desc)
            local opts = { noremap = true, silent = true }
            opts.desc = desc
            vim.keymap.set('n', key, cmd, opts)
        end

        nmap('<leader>a', function() harpoon:list():add() end, 'Harpoon: Add buffer to list')
        nmap('<C-e>', function() harpoon.ui:toggle_quick_menu(harpoon:list()) end, 'Harpoon: Toggle quick menu')
        nmap('<C-]>', function() harpoon:list():next() end, 'Harpoon: Next entry')
        nmap('<C-[>', function() harpoon:list():prev() end, 'Harpoon: Previous entry')

        local group = vim.api.nvim_create_augroup('harpoonGroup', { clear = true })

        -- Save exact cursor position when leaving buffer
        vim.api.nvim_create_autocmd({ 'BufLeave', 'ExitPre' }, {
            pattern = '*',
            group = group,
            callback = function()
                local filename = vim.fn.expand('%:p:.')
                local harpoon_marks = harpoon:list().items
                for _, mark in ipairs(harpoon_marks) do
                    if mark.value == filename then
                        mark.context.row = vim.fn.line('.')
                        mark.context.col = vim.fn.col('.')
                        return
                    end
                end
            end,
        })

        vim.api.nvim_create_autocmd({ 'Filetype' }, {
            pattern = 'harpoon',
            group = group,
            callback = function()
                vim.opt.cursorline = true
            end
        })

        harpoon:extend({
            UI_CREATE = function(cx)
                vim.keymap.set('n', '<C-v>', function()
                    harpoon.ui:select_menu_item({ vsplit = true })
                end, { buffer = cx.bufnr })

                vim.keymap.set('n', '<C-x>', function()
                    harpoon.ui:select_menu_item({ split = true })
                end, { buffer = cx.bufnr })
            end,
        })
        end,
}
greyHairChooseLife commented 1 month ago

Just leaving, same thing happens to me. Really annoying. :(

greyHairChooseLife commented 1 month ago

@db757 Hello mate, I found an alternative approach. It may not perfectly fit your situation, but hopefully, it will help.

successful attempt

Avoid mapping it exactly as-is:

vim.keymap.set("n", "<C-[>", function() harpoon:list():prev() end)
vim.keymap.set("n", "<C-]>", function() harpoon:list():next() end)

but, for example, change it to:

vim.keymap.set("n", "<C-h>", function() harpoon:list():prev() end)
vim.keymap.set("n", "<C-l>", function() harpoon:list():next() end)

(BTW, I use the urxvt as terminal emulator)

failed attempt

I tried to remove the <Esc> keymap for normal mode. It seemed to work at first, but it broke the keymap for harpoon:list():prev() as well.

vim.keymap.del('n', '<Esc>')
vim.keymap.set("n", "<C-[>", function() harpoon:list():prev() end)   -- just broken 

-- However, I was still able to run `lua require('harpoon'):list():prev()` in ex mode, and it worked fine.

What's strange is that without the keymap vim.keymap.set("n", "<C-[>", function() harpoon:list():prev() end), deleting the keymap for <Esc> throws an error saying 'No such mapping'.

I didn't realize that there is no such keymap for <Esc> for normal mode by default.