Skip to content

Commit

Permalink
feat: can open a directory from the command line
Browse files Browse the repository at this point in the history
When starting neovim from the command line, yazi would not open. E.g.
`nvim .` would not open yazi. This commit adds the ability to open yazi
from the command line.

This might also be supported in other plugins the user may have. I had
to disable the functionality in `neo-tree.nvim` to get this to work.

```lua
{
  "nvim-neo-tree/neo-tree.nvim",
  opts = {
    filesystem = {
      hijack_netrw_behavior = "disabled",
    },
  },
}
```

See:
mikavilpas/dotfiles@3b5bc69#diff-8be1bcf06549d609352b9b2f97e7ceb027155091fe4067d0e9e70e1dee85ed04R36
  • Loading branch information
mikavilpas committed Apr 26, 2024
1 parent 9b80f3e commit c32b990
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
26 changes: 18 additions & 8 deletions lua/yazi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ function M.setup(opts)
vim.tbl_deep_extend('force', configModule.default(), M.config, opts or {})

if M.config.open_for_directories == true then
---@param file string
---@param bufnr number
local function open_yazi_in_directory(file, bufnr)
if vim.fn.isdirectory(file) == 1 then
-- A buffer was opened for a directory.
-- Remove the buffer as we want to show yazi instead
vim.api.nvim_buf_delete(bufnr, { force = true })
M.yazi(M.config, file)
end
end

local yazi_augroup = vim.api.nvim_create_augroup('yazi', { clear = true })

-- disable netrw, the built-in file explorer
Expand All @@ -79,18 +90,17 @@ function M.setup(opts)
-- executed before starting to edit a new buffer.
vim.api.nvim_create_autocmd('BufAdd', {
pattern = '*',
---@param ev yazi.AutoCmdEvent
callback = function(ev)
local file = ev.file
if vim.fn.isdirectory(file) == 1 then
local bufnr = ev.buf
-- A buffer was opened for a directory.
-- Remove the buffer as we want to show yazi instead
vim.api.nvim_buf_delete(bufnr, { force = true })
M.yazi(M.config, file)
end
open_yazi_in_directory(ev.file, ev.buf)
end,
group = yazi_augroup,
})

-- When opening neovim with "nvim ." or "nvim <directory>", the current
-- buffer is already open at this point. If we have already opened a
-- directory, display yazi instead.
open_yazi_in_directory(vim.fn.expand('%:p'), vim.api.nvim_get_current_buf())
end
end

Expand Down
9 changes: 9 additions & 0 deletions lua/yazi/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@
---@field public timestamp string
---@field public id string
---@field public data {urls: string[]}

---@class yazi.AutoCmdEvent # the nvim_create_autocmd() event object copied from the nvim help docs
---@field public id number
---@field public event string
---@field public group number | nil
---@field public match string
---@field public buf number
---@field public file string
---@field public data any

0 comments on commit c32b990

Please sign in to comment.