oil.nvim icon indicating copy to clipboard operation
oil.nvim copied to clipboard

[question] When toggling preview, how to vertical resize width of the selector?

Open Sebastian-Nielsen opened this issue 11 months ago • 3 comments

The following idea should give an idea of what I am trying to do. The issue is that the vim.api.nvim_command("vertical resize 40") doesnt seem to work.

			keymaps = {
				['<C-p>'] = {
					callback = function()
						local oil = require("oil")
						oil.open_preview({ vertical = true, split = 'botright' })
						vim.api.nvim_command("vertical resize 40")
					end
				}
			},

I am trying to go from this:

Image

to this:

Image

Sebastian-Nielsen avatar Mar 25 '25 21:03 Sebastian-Nielsen

First thing to try is put the resize in a callback, because oil.open_preview (like many oil functions, unfortunately) is async.

keymaps = {
	['<C-p>'] = {
		callback = function()
			local oil = require("oil")
			oil.open_preview({ vertical = true, split = 'botright' }, function(err)
				if not err then
					vim.api.nvim_command("vertical resize 40")
				end
			end)

		end
	}
},

stevearc avatar Mar 30 '25 21:03 stevearc

Can you also resize the preview on floating mode? Not sure how to get the preview window reference.

rafaelpirolla avatar Apr 17 '25 07:04 rafaelpirolla

Sorry, I was so thrilled it worked that I forgot to reply.

Also, I got another question if you dont mind (last question I promise): Is there a way to know whether the previewer is currently open or not? I am trying to make <c-p> toggle the previewer on/off.

Sebastian-Nielsen avatar Apr 17 '25 10:04 Sebastian-Nielsen

@Sebastian-Nielsen the default preview keymap should be a toggle, but to find out if the preview window is open you can just iterate through all the windows in the tab and check if any of them are the preview window. You can see where we're doing that here (with some extra logic to tell if the preview window is being managed by oil or some other plugin).

https://github.com/stevearc/oil.nvim/blob/5b6068aad7d2057dd399fac73b7fb2cdf23ccd6e/lua/oil/util.lua#L704-L718

@rafaelpirolla you can also use this to get a reference to the preview window when it's floating

stevearc avatar Jun 01 '25 18:06 stevearc