Giter Site home page Giter Site logo

hbac.nvim's Introduction

hbac.nvim

Heuristic buffer auto-close

overview

Automagically close the unedited buffers in your bufferlist when it becomes too long. The "edited" buffers remain untouched. For a buffer to be considered edited it is enough to enter insert mode once or modify it in any way.

description

You like using the buffer list, but you hate it when it has too many buffers, because you loose the overview for what files you are actually working on. Indeed, a lot of the times, when browsing code you want to look at some files, that you are not actively working on, like checking the definitions or going down the callstack when debugging. These files then pollute the bufferlist and make it harder to find ones you actually care about. Reddit user xmsxms posted a script that marks all once edited files in a session as important and provides a keybinding to close all the rest. In fact, I used some of his code in this plugin, and you can achieve the same effect as his script using hbac. The main feature of this plugin, however, is the automatic closing of buffers. If the number of buffers reaches a threshold (default is 10), the oldest unedited buffer will be closed once you open a new one.

installation

with packer.nvim

use { 'axkirillov/hbac.nvim' }

with lazy.nvim

{
  'axkirillov/hbac.nvim',
  config = true,
}

configuration

require("hbac").setup({
  autoclose     = true, -- set autoclose to false if you want to close manually
  threshold     = 10, -- hbac will start closing unedited buffers once that number is reached
  close_command = function(bufnr)
    vim.api.nvim_buf_delete(bufnr, {})
  end,
  close_buffers_with_windows = false, -- hbac will close buffers with associated windows if this option is `true`
  telescope = {
    -- See #telescope-configuration below
    },
})

usage

Let hbac do its magick ๐Ÿ˜Š

or

  • :Hbac toggle_pin - toggle a pin of the current buffer to prevent it from being auto-closed
  • :Hbac close_unpinned - close all unpinned buffers
  • :Hbac pin_all - pin all buffers
  • :Hbac unpin_all - unpin all buffers
  • :Hbac toggle_autoclose - toggle autoclose behavior
  • :Hbac telescope - open the telescope picker

or, if you prefer to use lua:

local hbac = require("hbac")
hbac.toggle_pin()
hbac.close_unpinned()
hbac.pin_all()
hbac.unpin_all()
hbac.toggle_autoclose()

Telescope extension

The plugin provides a telescope.nvim extension to view and manage the pin states of buffers. This requires telescope and its dependency plenary.nvim. nvim-web-devicons is also recommended.

Usage

To use the telescope picker, load the hbac telescope extension in your config file.

require('telescope').load_extension('hbac')

After loading the extension, you can execute the picker like so:

:Telescope hbac buffers

or if you prefer lua:

require('telescope').extensions.hbac.buffers()

The picker provides the following actions:

  • hbac_toggle_selections - toggle the pin state of the selected buffers (either single or multi-selections)
  • hbac_pin_all - pin all buffers
  • hbac_unpin_all - unpin all buffers
  • hbac_close_unpinned - close all unpinned buffers
  • hbac_delete_buffer - delete the selected buffers with the function set in opts.close_command (nvim_buf_delete by default`)

Telescope configuration

Hbac's telescope picker takes the options you would expect to pass to the builtin Telescope buffer picker, including those found in :h telescope.builtin.buffers() and those in :h telescope.setup() relevant to a buffer picker.

The defaults of telescope key of the setup table are:

-- These actions refresh the picker and the pin states/icons of the open buffers
-- Use these instead of e.g. `hbac.pin_all()`
local actions = require("hbac.telescope.actions")

telescope = {
    sort_mru = true,
    sort_lastused = true,
    selection_strategy = "row",
    use_default_mappings = true,  -- false to not include the mappings below
    mappings = {
      i = {
        ["<M-c>"] = actions.close_unpinned,
        ["<M-x>"] = actions.delete_buffer,
        ["<M-a>"] = actions.pin_all,
        ["<M-u>"] = actions.unpin_all,
        ["<M-y>"] = actions.toggle_pin,
      },
      n = {
        -- as above
      },
    },
    -- Pinned/unpinned icons and their hl groups. Defaults to nerdfont icons
    pin_icons = {
      pinned = { "๓ฐƒ ", hl = "DiagnosticOk" },
      unpinned = { "๓ฐคฑ ", hl = "DiagnosticError" },
    },
}

pin_icons is unique to Hbac, but the other keys are standard Telescope options. You can add other options and mappings to your setup config which will deep-extend the defaults. For example:

telescope = {
  layout_strategy = "vertical",
  sort_lastused = false,
  ignore_current_buffer = true,
  prompt_prefix = "Hbac! ",
  mappings = {
    i = {
      ["<CR>"] = telescope_actions.select_drop,
      ["<C-CR>"] = telescope_actions.select_default,
      ["<M-z>"] = function() print("Hello from Hbac!") end,
    },
  }
}

You can also pass options to the picker directly when calling its function. These options will deep-extend your setup table.

require("telescope").extensions.hbac.buffers({
  file_ignore_patterns = { ".json" },
  layout_strategy = "horizontal",
  -- etc.
})

There are some options that are not supported like bufnr_width and path_display. Please consider contributing or opening an issue if something essential is missing.

How to check the pin status of the current buffer

The state module exposes the is_pinned function, which returns the pin status of any buffer as a boolean value. You can use this check to display the pin status in your statusline or wherever you find convenient. Here is an example lualine.nvim integration:

lualine_c = {
    {
      function()
        local cur_buf = vim.api.nvim_get_current_buf()
        return require("hbac.state").is_pinned(cur_buf) and "๐Ÿ“" or ""
        -- tip: nerd fonts have pinned/unpinned icons!
      end,
      color = { fg = "#ef5f6b", gui = "bold" },
    }
  }

hbac.nvim's People

Contributors

al-ce avatar axkirillov avatar ditsuke avatar momepp avatar nimjii avatar tom-kuca avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.