echasnovski / mini.nvim

Library of 40+ independent Lua modules improving overall Neovim (version 0.8 and higher) experience with minimal effort
MIT License
4.84k stars 183 forks source link

Change cwd on open #421

Closed shasherazi closed 1 year ago

shasherazi commented 1 year ago

Contributing guidelines

Module(s)

mini.files

Description

When I open neovim like nvim dotfiles/config/nvim from the home directory mini.files opens like this image

But the next time mini.nvim is opened, it shows this image

I used nvim-tree before and it changed the working directory to whichever folder was first opened and persisted. Can I have this feature in mini.files as well?

echasnovski commented 1 year ago

Thanks for the suggestion!

MiniFiles.open() by default uses current working directory as anchor (target) directory. When you open nvim dotfiles/config/nvim, MiniFiles.open() is called with 'dotfiles/config/nvim' path, but when you later call it (probably without arguments), it is called without arguments and uses current working directory (which by default is the one Neovim was opened in; ~ in your case).

You have several options:

Closing as not planned.

shasherazi commented 1 year ago

Hi, I tried the first two methods, and kinda both of them failed. For the first method, I need to open a file in that directory, and for the second method, it just selects the sensible root (the directory with .git in my case, where I am trying to open nvim in a subfolder).

Basically, what I wanted was to get the path argument when running nvim and change cwd to that. I have searched fairly long time but couldn't find a way. Guess I'll just stick to the 2nd method till a solution is found.

echasnovski commented 1 year ago

For the first method, I need to open a file in that directory,

Judging by your example, you've already inside target directory, so first method (with example buffer mapping from 'mini.files') should work (assuming directory is not empty, though).

Basically, what I wanted was to get the path argument when running nvim and change cwd to that. I have searched fairly long time but couldn't find a way. Guess I'll just stick to the 2nd method till a solution is found.

I am not sure if I understand correctly, but this basically sounds like a combination argv() and chdir() built-in functions. Try putting something like this inside your config startup files:

if vim.fn.argc(-1) > 0 then
  local path = vim.fn.argv(0)
  if vim.fn.isdirectory(path) == 0 then path = vim.fs.dirname(path) end
  vim.fn.chdir(path)
end
Parsifa1 commented 9 months ago

Contributing guidelines 贡献指南

Module(s) 模块

mini.files 迷你文件

Description 描述

When I open neovim like nvim dotfiles/config/nvim from the home directory mini.files opens like this当我从主目录 nvim dotfiles/config/nvim 打开neovim时 mini.files 像这样打开 image

But the next time mini.nvim is opened, it shows this但是下次打开 mini.nvim 时,它会显示这个 image

I used nvim-tree before and it changed the working directory to whichever folder was first opened and persisted. Can I have this feature in mini.files as well?我之前使用过 nvim-tree ,它将工作目录更改为首先打开并保留的文件夹。我也可以在 mini.files 中使用此功能吗?

vim.keymap.set("n", "<leader>e", ":lua MiniFiles.open(vim.api.nvim_buf_get_name(0))<CR>", {noremap = true,
silent = true,})

mayme you can try keymap like this(?

xiantang commented 9 months ago

try this { "l", ":lua MiniFiles.open(vim.api.nvim_buf_get_name(0)):lua MiniFiles.reveal_cwd()", desc = "smart location", },