Skip to content

Help: Remap Ctrl-n to "switch to n-th buffer" #3746

Answered by dpetka2001
tf42src asked this question in Q&A
Discussion options

You must be logged in to vote

I put the following in ~/.config/nvim/lua/config/autocmds.lua and it seems to do what you want

vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, {
  group = vim.api.nvim_create_augroup("test_buffers", {}),
  desc = "test buffers",
  callback = function()
    local buffers = {}
    for _, buf in ipairs(vim.api.nvim_list_bufs()) do
      if vim.api.nvim_buf_is_valid(buf) and vim.bo[buf].buftype == "" then
        table.insert(buffers, buf)
      end
    end

    for k, v in ipairs(buffers) do
      vim.keymap.set("n", "<M-" .. k .. ">", "<cmd>buffer " .. v .. "<cr>", { desc = "go to buffer" })
    end
  end,
})

Test it out.

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@tf42src
Comment options

@tf42src
Comment options

@dpetka2001
Comment options

@dpetka2001
Comment options

Answer selected by tf42src
@dpetka2001
Comment options

@tf42src
Comment options

@tf42src
Comment options

@tf42src
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants