Skip to content

Instantly share code, notes, and snippets.

@sashalikesplanes
Last active October 24, 2023 15:06
Show Gist options
  • Save sashalikesplanes/dcbb4591deb96d704b7c37153182af1e to your computer and use it in GitHub Desktop.
Save sashalikesplanes/dcbb4591deb96d704b7c37153182af1e to your computer and use it in GitHub Desktop.
Harpoon X Bufferline Neovim
-- This example shows how to integrate Bufferline with Harpoon
-- The goal is to get bufferline to show only the files marked by harpoon, with the correct number
-- I use <C-w>{1..N} to navigate between the harpoon buffers, my harpoon config is also included
-- I have also setup an autocommand in order to open all the harpoon buffers when launching neovim, it is at the bottom of the file
-- NOTE: this guide relies on you using the lazy package manager
-- bufferline.nvim
-- helper functions:
local function get_marked_buffers()
local mark = require("harpoon.mark")
local harpoon_buffers = {}
local current_idx = 1
while mark.get_marked_file_name(current_idx) do
harpoon_buffers[current_idx] = mark.get_marked_file_name(current_idx)
current_idx = current_idx + 1
end
return harpoon_buffers
end
local function get_index_of_value(table, value)
local index = nil
for i, v in pairs(table) do
if v == value then
index = i
break
end
end
return index
end
-- config itself, the options required for the itegration are first, the rest are my personal settings
return {
"akinsho/bufferline.nvim",
dependencies = { "theprimeagen/harpoon" },
opts = {
options = {
-- INTEGRATION
numbers = function(buffer)
-- get the harpoon buffers and the name of the buffer
local buf_name = vim.fn.bufname(buffer.id)
local harpoon_buffers = get_marked_buffers()
return get_index_of_value(harpoon_buffers, buf_name) or ""
end,
custom_filter = function(buf_number)
-- get the harpoon buffers and the name of the buffer
local harpoon_buffers = get_marked_buffers()
local buf_name = vim.fn.bufname(buf_number)
local idx = get_index_of_value(harpoon_buffers, buf_name)
if idx then
return true
end
return false
end,
sort_by = function (buffer_a, buffer_b)
-- get the harpoon buffers and the names of the buffers
local harpoon_buffers = get_marked_buffers()
local buf_name_a = vim.fn.bufname(buffer_a.id)
local buf_name_b = vim.fn.bufname(buffer_b.id)
local idx_a = get_index_of_value(harpoon_buffers, buf_name_a)
local idx_b = get_index_of_value(harpoon_buffers, buf_name_b)
return idx_a < idx_b
end,
-- PERSONAL SETTINGS
-- stylua: ignore
close_command = function(n) require("mini.bufremove").delete(n, false) end,
-- stylua: ignore
right_mouse_command = function(n) require("mini.bufremove").delete(n, false) end,
diagnostics = "nvim_lsp",
always_show_bufferline = false,
diagnostics_indicator = function(_, _, diag)
local icons = require("lazyvim.config").icons.diagnostics
local ret = (diag.error and icons.Error .. diag.error .. " " or "")
.. (diag.warning and icons.Warn .. diag.warning or "")
return vim.trim(ret)
end,
},
},
event = "VeryLazy",
keys = {
{ "<leader>bp", "<Cmd>BufferLineTogglePin<CR>", desc = "Toggle pin" },
{ "<leader>bP", "<Cmd>BufferLineGroupClose ungrouped<CR>", desc = "Delete non-pinned buffers" },
},
}
-- harpoon.lua
local HARPOON_BUFFERS = 10
return {
"theprimeagen/harpoon",
config = function()
vim.keymap.set("n", "<leader>a", require("harpoon.mark").add_file, { desc = "[A]dd harpoon file" })
vim.keymap.set("n", "<C-w>e", require("harpoon.ui").toggle_quick_menu, { desc = "[W]indow [E]dit harpoon" })
for i = 1, HARPOON_BUFFERS do
vim.keymap.set("n", "<C-w>" .. i, function() require("harpoon.ui").nav_file(i) end,
{ desc = "Harpoon [W]indow " .. i })
end
end,
}
-- autocommands.lua
vim.api.nvim_create_autocmd("User", {
pattern = "LazyVimStarted",
once = true,
callback = function()
-- this needs to be deffered for some other stuff to load, I am not sure there is a better event
vim.defer_fn(function()
local mark = require("harpoon.mark")
local harpoon_buffers = {}
local current_idx = 1
while mark.get_marked_file_name(current_idx) do
harpoon_buffers[current_idx] = mark.get_marked_file_name(current_idx)
current_idx = current_idx + 1
end
-- for each of the harpoon buffers we open them
for _, buffer in reverse_iter(harpoon_buffers) do
vim.cmd("silent edit " .. buffer)
end
require("harpoon.ui").nav_file(1)
end, 10)
end,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment