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: Highlight the file name opened in the buffer when open its directory in oil.nvim #450

Open
1 task done
acdesouza opened this issue Jul 19, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@acdesouza
Copy link

Did you check existing requests?

  • I have searched the existing issues

Describe the feature

I want to use a keymap, like <Leader>o, to flip the card between the file and the file directory keeping the current file in buffer synced with the file in the oil directory list. Like this:

neovim_oil_nvim_open_and_select_file.mov

Provide background

I'm trying to get comfortable with the idea of the card-flip transition for directory explorer. I was using default Netrw for that, but I couldn't figure out how to highlight the current file in buffer when I flip the card to the directories view.

In projects with lots of directories and files, it is pretty easy to get lost.

Motivated by the concept of "edit your filesystem like a normal Neovim buffer" I'm trying oil.nvim. But, I also could not figure it out how to properly do this.

Current Solution

After some search I end up with the following config:

    vim.api.nvim_set_keymap('n', '<leader>o', ':lua open_oil_and_select_current_file()<CR>', { noremap = true, silent = true })

    function open_oil_and_select_current_file()
      local current_path = vim.fn.expand('%:p:h')
      local bufname = vim.fn.expand('%:t')


      require('oil').open(current_path)
      vim.defer_fn(function()
        -- Find and highlight the current buffer's file in netrw
        vim.cmd('/' .. bufname .. '$')
      end, 50)
    end

To flip the card back I use this autocmd:

    local augroup = vim.api.nvim_create_augroup -- Create/get autocommand group
    local autocmd = vim.api.nvim_create_autocmd -- Create autocommand
    augroup('netrw_oil_config', { clear = true })
    autocmd('Filetype', {
      group = 'netrw_oil_config',
      pattern = 'oil',
      callback = function()
        vim.api.nvim_buf_set_keymap(
          0,              -- In the current buffer
          "n",            -- at Normal mode
          "<Leader>o",    -- Map the <Leader> o keys
          "<C-o>", {      -- to close buffer without closing the window
            noremap = true, -- without recurcively expand it
            silent = true -- and without displaing it on command-line window
          })
      end
    })

What is the significance of this feature?

nice to have

Additional details

Is there another way to achieve this I could not find in the docs?

Is it a good candidate for the Receipts section in the Docs?

I'm using defer to ensure Oil is opened before search, otherwise I got an error the pattern wasn't found. Can we have a callback for the API method open, so I can ensure the search only happens after the Oil buffer is ready?

Am I missing anything?

@acdesouza acdesouza added the enhancement New feature or request label Jul 19, 2024
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

1 participant