Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature request: ability to close oil buffer on select action #121

Closed
1 task done
dhruvmanila opened this issue Jun 6, 2023 · 5 comments
Closed
1 task done

feature request: ability to close oil buffer on select action #121

dhruvmanila opened this issue Jun 6, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@dhruvmanila
Copy link

Did you check existing requests?

  • I have searched the existing issues

Describe the feature

When opening an entry in either split or a new tab, I want the oil buffer to close automatically.

Provide background

Most of the times whenever I'm opening an entry in some a new window or tab, I don't want the oil buffer to be around. I've to move to that window and close it.

An example workflow is that I'm working on 2 files and I opened the second one in a vertical split. The first file isn't visible as of now unless I quit the oil buffer manually. That requires moving to that window, closing the oil buffer and moving back to the second split.

Additional details

I thought something like this should work but it doesn't:

local function split_close()
	local oil = require('oil')
	oil.select { vertical = true }
	oil.close()
end
@dhruvmanila dhruvmanila added the enhancement New feature or request label Jun 6, 2023
@dhruvmanila
Copy link
Author

I've found a workaround for this:

-- Helper function to close the oil buffer after a selection.
---@param select_opts table options to pass to `oil.select`
---@return function #function to invoke the action
local function select_close(select_opts)
  return function()
    local oil = require 'oil'
    local oilwin = vim.api.nvim_get_current_win()
    oil.select(select_opts)
    vim.api.nvim_win_call(oilwin, function()
      oil.close()
    end)
  end
end

require('oil').setup(
  keymaps = {
    ['<C-s>'] = select_close { horizontal = true },
    ['<C-v>'] = select_close { vertical = true },
    ['<C-t>'] = select_close { tab = true },
  }
)

Feel free to close the issue if you think this feature isn't required in core :)

@stevearc
Copy link
Owner

I've added this as part of the core API. You can pass in oil.select({close = true}) and it will close the oil buffer in the original window (no-op unless you opened in a split or tab).

@dhruvmanila
Copy link
Author

Thanks! 😄

@divramod
Copy link

divramod commented Nov 16, 2023

@stevearc i am struggeling to get it running.

it would be nice to have one example in the docs on how to run a custom function for a keymap.

how could it look like?

["<C-v>"] = {
      callback = function()
        require("oil.actions").select_vsplit({ close = true })
      end,
      desc = "select_vsplit",
      mode = "n",
},

@ziimir
Copy link

ziimir commented May 16, 2024

@stevearc i am struggeling to get it running.

it would be nice to have one example in the docs on how to run a custom function for a keymap.

how could it look like?

["<C-v>"] = {
      callback = function()
        require("oil.actions").select_vsplit({ close = true })
      end,
      desc = "select_vsplit",
      mode = "n",
},

it will be

keymaps = {
  ["<C-v>"] = {
    callback = function()
      require('oil').select({ vertical = true, close = true })
    end,
    desc = "select_vsplit",
    mode = "n",
  },
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants