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

Open browser with current file selected #13

Closed
rosshadden opened this issue Jan 10, 2022 · 9 comments
Closed

Open browser with current file selected #13

rosshadden opened this issue Jan 10, 2022 · 9 comments

Comments

@rosshadden
Copy link

This is an awesome plugin! I used to have a really big function I wrote that would open ranger, and you have implemented it even better.

I'm not sure how relevant this would be to other file managers, but one thing I had before (as can be seen in the link above, myselectFile argument) is opening ranger to the folder of the current file, with the current file selected.

Is this something possible to do with your plugin? I'm not necessarily requesting a feature as this may only prove to be possible or easy with a subset of your supported browsers (I know it is on vifm as well, as I used to use that one and do the same thing). But I would like some help making this work. Perhaps I can write something to do this in an on_open function?

I was playing around with the lua API and I will definitely be able to at least open the file browser in the current directory, and that will certainly be good enough as a fallback until/unless we can figure out how to also select the current file.

Thank you for your time.

@is0n
Copy link
Owner

is0n commented Jan 16, 2022

I've gotten this to work, although, it is very hacky. 😅

require('fm-nvim').setup{
	cmds = {
		ranger_cmd  = "ranger --selectfile=" .. vim.fn.expand("%:p")
	},

	on_open = {
		function()
			require('fm-nvim').setup{
				cmds = {
					ranger_cmd  = "ranger --selectfile=" .. vim.fn.expand("%:p")
				},
			}
		end
	}
}

@gkzhb
Copy link

gkzhb commented Apr 28, 2022

For lf, we can use require('fm-nvim').Lf(vim.fn.expand('%:p')) to achieve this.

As for ranger, maybe we can provide another function parameter for require('fm-nvim').Ranger() to pass in the selected file so that we can use this lua API to open ranger with current file selected?

@zhengpd
Copy link

zhengpd commented May 1, 2022

It would be best to implement it as an option, like:

require('fm-nvim').setup { 
  focus_selected_file = true
}

So that new users wouldn't have to know how to set up the on_open callback.

@zhengpd
Copy link

zhengpd commented May 1, 2022

The on_open tip from @is0n doesn't work for me. Don't know what's wrong yet. But I figured out a way to open Ranger & Xplr with current file selected. Try if you think it helps.

The lua part:

-- vim/lua/fm_open.lua
-- workaround for selected file issue: https://github.com/is0n/fm-nvim/issues/13
-- command! -nargs=? -complete=dir Ranger :lua require'fm_open'.open('Ranger', '<f-args>')

local M = {}
local fm_nvim = require('fm-nvim')

M.open = function(fn, dir)
  if not fm_nvim[fn] then
    print('Invalid file manager.')
    return false
  end

  local fpath = vim.fn.expand('%:p')

  local override_cmds = {
    ranger_cmd = 'ranger',
    xplr_cmd = 'xplr'
  }

  if dir == nil or dir == '' then
    -- If no dir arg passed, open fm with current file selected
    override_cmds = {
      ranger_cmd = 'ranger --selectfile ' .. fpath,
      xplr_cmd = 'xplr ' .. fpath
    }

    fm_nvim.setup { cmds = override_cmds }

    -- fn-nvim append '.' to ranger_cmd, which causes Ranger opens 2 paths
    -- so we pass the empty '' as dir arg to avoid this behavior
    dir = override_cmds[string.lower(fn) .. '_cmd'] and '' or '.'
  else
    -- If dir passed, undo the fpath cmds
    fm_nvim.setup { cmds = override_cmds }
  end

  fm_nvim[fn](dir)
end

return M

The vim commands part:

" vim/after/plugin/fm-nvim.vim

command! -nargs=? -complete=dir Ranger :lua require'fm_open'.open('Ranger', '<f-args>')
command! -nargs=? -complete=dir Xplr :lua require'fm_open'.open('Xplr', '<f-args>')

Still, I wish fm-nvim got a builtin option for selected file in future, maybe by rewriting the builtin functions like M.Ranger to do it.

@is0n
Copy link
Owner

is0n commented May 1, 2022

The on_open tip from @is0n doesn't work for me. Don't know what's wrong yet. But I figured out a way to open Ranger & Xplr with current file selected. Try if you think it helps.

This work is amazing! Support for Xplr was definitely a nice touch 👍🏽

In case you might want a cleaner approach to this, consider checking out tui-nvim. The README includes code that opens ranger with the current file selected in just a few lines.

@is0n is0n closed this as completed May 1, 2022
@zhengpd
Copy link

zhengpd commented May 2, 2022

@is0n tui-nvim is cool and more configurable 👍 Looking forward to the user-defined keybinds.

@zhengpd
Copy link

zhengpd commented May 2, 2022

Here is how I use tui-nvim now, replacing the fm-nvim. It's amazing that I can easily open any command now, not just file managers. Really cool!

" vim/plugin/tui-nvim-cmds.vim

command! Btop :lua require('tui-nvim-cmds').btop()<CR>

command! -nargs=? -complete=file Ranger :lua require('tui-nvim-cmds').ranger(<f-args>)<CR>
command! -nargs=? -complete=file Xplr :lua require('tui-nvim-cmds').xplr(<f-args>)<CR>
-- vim/lua/tui-nvim-cmds.lua

local M = {}

M.btop = function()
  local temp = '/tmp/tui-nvim-btop'

  require('tui-nvim'):new {
    cmd = 'btop',
    temp = temp,
    width = 1,
    height = 1,
  }
end

M.ranger = function(path)
  local temp = '/tmp/tui-nvim-ranger'

  if path == nil or path == '' then
    path = vim.fn.expand('%:p')
  end

  require('tui-nvim'):new {
    cmd = 'ranger --choosefiles=' .. temp .. ' --selectfile=' .. vim.fn.fnameescape(path),
    temp = temp,
    width = 1,
    height = 1,
  }
end

M.xplr = function(path)
  local temp = '/tmp/tui-nvim-xplr'

  if path == nil or path == '' then
    path = vim.fn.expand('%:p')
  end

  require('tui-nvim'):new {
    cmd = 'xplr ' .. vim.fn.fnameescape(path),
    temp = temp,
    width = 1,
    height = 1,
  }
end

return M

@zacgodinez
Copy link

Great package 😍
For those interested, I have solved this for my needs, by creating two keybinds for xplr.

["<leader>n"] = { "<cmd>Xplr<cr>", desc = "XPLR cwd" },
["<leader>m"] = { "<cmd>Xplr %:p:h<cr>", desc = "XPLR selected file" },

@avucic
Copy link

avucic commented Sep 11, 2023

For vifm currently I have this ugly way to open and select file

maps.n["<leader>fe"] = {
  function()
    local dir_arg = vim.fn.expand("%:p:h")
    local current_file = vim.fn.expand("%:p")
    if current_file ~= "" then
      dir_arg = "--select=" .. current_file
    end
    require("fm-nvim").Vifm(dir_arg)
  end,
}

when you run vifm with only vifm --select= it will fail. Maybe <fm>_cmd option in configuration can accept fn.

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

No branches or pull requests

6 participants