mikavilpas / yazi.nvim

A Neovim Plugin for the yazi terminal file manager

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

When opening a directory, insert mode is not activated

mikavilpas opened this issue · comments

When you open yazi and then highlight (hover) a directory item, and press enter to open it, a new floating window with yazi is opened.

However, this new floating window does not currently seem to start in insert mode, which means keybindings don't work.

You can currently work around this limitation by pressing i to enter insert mode, but a better solution should be come up with.

This was very briefly discussed in #55

Tested this a bit this morning and for me it enters the insert mode, but the active window is not the yazi floating window but the window with empty buffer underneath it.
So for me it requires manually entering normal mode (with esc) , switching to yazi floating window and then entering insert mode. Then it works.

I've not done much Lua/Neovim code but I think if something like this would be run after opening directory it might help:

-- Check if the current buffer's filetype is 'yazi'
local current_ft = vim.bo.filetype
if current_ft ~= 'yazi' then
    -- Iterate through available windows
    for _, win in ipairs(vim.api.nvim_list_wins()) do
        local buf = vim.api.nvim_win_get_buf(win)
        -- Check if the buffer's filetype is 'yazi'
        local win_ft = vim.api.nvim_buf_get_option(buf, 'filetype')
        if win_ft == 'yazi' then
            -- Switch to the window and enter insert mode
            vim.api.nvim_set_current_win(win)
            vim.cmd('startinsert')
            break
        end
    end
end

I tried to insert the code and test it with the plugin but haven't found the right place to insert it to (maybe some kind of lifecycle issue with autogroups?).

I spent a couple of hours trying to crack this, but couldn't solve it yet.

Let's keep this open. A solution may be available later.

For now I think the best thing you can do is manually enter insert mode after opening a directory with i.

@tkivela hi, I just found a workaround/fix for this. Would you have time to verify it has been solved?
If there are issues remaining, I will reopen this issue.

For some reason it still starts with insert mode in [No Name] buffer like this
image
If I manually enter normal mode (with Esc) and the switch to Yazi window it works ok.

Hmm that's super strange. I noticed for me it now works after neovim has already started. But opening a directory from the command line does not work now.


Can you try the following? Create a file called repro.lua with the following contents:

Details

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify('./.repro', ':p')

-- set stdpaths to use .repro
for _, name in ipairs({ 'config', 'data', 'state', 'cache' }) do
  vim.env[('XDG_%s_HOME'):format(name:upper())] = root .. '/' .. name
end

-- bootstrap lazy
local lazypath = root .. '/plugins/lazy.nvim'
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    'git',
    'clone',
    '--filter=blob:none',
    'https://github.com/folke/lazy.nvim.git',
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)
vim.g.mapleader = ' '

-- install plugins
local plugins = {
  'folke/tokyonight.nvim',
  {
    'mikavilpas/yazi.nvim',
    dependencies = {
      'nvim-lua/plenary.nvim',
    },
    event = 'VeryLazy',
    keys = {
      {
        -- 👇 choose your own keymapping
        '<leader>fy',
        function()
          require('yazi').yazi()
        end,

        { desc = 'Open the file manager' },
      },
    },
    setup = function(_, opts)
      require('yazi').setup(opts)
    end,
    ---@type YaziConfig
    opts = {
      open_for_directories = true,
    },
  },
}
require('lazy').setup(plugins, {
  root = root .. '/plugins',
})

vim.cmd.colorscheme('tokyonight')

Next, start neovim with nvim -u repro.lua. It will launch in a sandbox environment and install minimal packages for this session only.

In the sandbox neovim session, launch yazi and open a directory in it with enter. Does it stay in insert mode when the new directory is displayed for you?

Yes the problem that I gave a screenshot is in the case neovim is opened from the terminal with a directory as a parameter and config option open_for_directories = truefor the plugin.

I can try the repro.lua script tomorrow if needed.

Ok, that sounds like I still need to work on that part 🤔

Good idea using repro config file!
When launching with nvim -u repro.lua and starting yazi.nvimwith the keymapping in the repro config (<leader>fy) the yazi window is in normal mode and stays in normal mode when entering directories. It works ok, no problems there. The parent window seems to be in TERMINALmode:
image

With nvim ./foodirectory -u repro.lua it now seems to open netrw instead of Yazi which is odd. With my own config it still opens Yazi window, but the focus in on wrong window and in insert mode. I have LazyVim "distro" as a base (https://www.lazyvim.org) so the difference might be in it. I tried to disable all my custom autocommands and even all set Vim options but it didn't make a difference.

Tested this a bit this morning and for me it enters the insert mode, but the active window is not the yazi floating window but the window with empty buffer underneath it. So for me it requires manually entering normal mode (with esc) , switching to yazi floating window and then entering insert mode. Then it works.

I've not done much Lua/Neovim code but I think if something like this would be run after opening directory it might help:

-- Check if the current buffer's filetype is 'yazi'
local current_ft = vim.bo.filetype
if current_ft ~= 'yazi' then
    -- Iterate through available windows
    for _, win in ipairs(vim.api.nvim_list_wins()) do
        local buf = vim.api.nvim_win_get_buf(win)
        -- Check if the buffer's filetype is 'yazi'
        local win_ft = vim.api.nvim_buf_get_option(buf, 'filetype')
        if win_ft == 'yazi' then
            -- Switch to the window and enter insert mode
            vim.api.nvim_set_current_win(win)
            vim.cmd('startinsert')
            break
        end
    end
end

I tried to insert the code and test it with the plugin but haven't found the right place to insert it to (maybe some kind of lifecycle issue with autogroups?).

in the file lua/yazi.lua, I put this piece of code inside of the vim.schedule function and it fixed this problem for me.

Hmm that seems like this might be some kind of tricky timing issue. @dinomon456 would you have any idea if this could be reproduced?

Can you try if you can reproduce it using the reproduction script here https://github.com/mikavilpas/yazi.nvim/blob/master/repro.lua

Hmm that seems like this might be some kind of tricky timing issue. @dinomon456 would you have any idea if this could be reproduced?

Can you try if you can reproduce it using the reproduction script here https://github.com/mikavilpas/yazi.nvim/blob/master/repro.lua

when i use the reproduction script with options open_for_directories = true and lazy = false and open a directory with nvim -u repro.lua directory/ I get put into insert mode in the no name buffer and I need to click on to the yazi buffer to use it.

feh_1289311_000001_2024-06-21-055345_2544x1395_scrot

Awesome, thanks! I am able to reproduce this now. I'll try to fix it.