luukvbaal / nnn.nvim

File manager for Neovim powered by nnn.
BSD 3-Clause "New" or "Revised" License
422 stars 9 forks source link

disable quitcd within neovim only #71

Closed gongfarmer closed 1 year ago

gongfarmer commented 1 year ago

In my ~/.zshrc, I have configured nnn to cd on quit. I like this feature in the terminal but not when I am using the nnn.nvim plugin within neovim.

To disable quitcd within neovim only, I have tried the following settings:

    quitcd = ""
    quitcd = "false"

Neither of these worked. Is there a way to do this?

gongfarmer commented 1 year ago

Fixed now. This was actually a problem with my neovim configuration.

My nnn.lua configuration file was not getting executed at all for some reason. I looked at your luukvbaal dotfiles repo and copied your configuration which puts the setup function right into the 'use' statement in plugins.lua, then I added quitcd = false to that.

Now it works as desired -- no more quitcd in either the picker or explorer.

gongfarmer commented 1 year ago

This is not fixed after all... my testing was flawed.

My solution (quitcd = false) is invalid. With this configuration, on quitting the NnnPicker, I get an error message: "attempt to concatenate field 'quitcd' (a boolean value)".

It also does not work to configure quitcd = "". That causes an even more confusing message after quitting NnnPicker: "Pattern not found: Users" (The "Users" string comes from my file path.)

These messages are both coming from nnn.lua:182:

    c(cfg.quitcd..f.fnameescape(dir))

This line concatenates the value of the quitcd field from the user's nvim configuration, with the filename returned by the picker.

Using the default where the quitcd field is set to "tcd", this combines with the filename returned by the picker to produce a string like "tcd/Users/fhanson/file.txt". This looks weird to me.. the lack of space between the function name "tcd" and the parameter looks like a bug. But I guess it isn't, because if I run that exact string in nvim with the :lua command, it works.

So how can I configure the quitcd field to disable the quitcd functionality? I can't use a boolean or empty string. The only thing I can put in there is the name of a neovim lua function that takes on arg. I tried setting quitcd to "print" but this caused an error message.

In the end I solved this by changing nnn.lua to check for a false or empty-string value in the quitcd field, and do nothing if either of those are found.

Please consider making a code change to allow the user to disable quitcd. I'm putting my working, tested solution here instead of in a PR since my lua is not that strong and there are probably better ways to achieve this.

diff --git a/lua/nnn.lua b/lua/nnn.lua
index c8acfb7..66c8722 100644
--- a/lua/nnn.lua
+++ b/lua/nnn.lua
@@ -179,7 +179,9 @@ local function on_exit(id, code)
                local fd, _ = io.open(nnntmpfile, "r")
                if fd then
                        local dir = fd:read():sub(5, -2)
-                       c(cfg.quitcd..f.fnameescape(dir))
+                       if (type(cfg.quitcd) == "string" and cfg.quitcd ~= "") then
+                               c(cfg.quitcd..f.fnameescape(dir))
+                       end
                        fd:close()
                        os.remove(nnntmpfile)
                end
luukvbaal commented 1 year ago

Sorry, I didn't look at this last time because you seemed to have resolved it on your own. I'll try to look at it this weekend.

luukvbaal commented 1 year ago

Hey, thanks for opening the issue. I agree that running [tl]cd by default is a bit unexpected. A user that has setup quitcd for use in their shell might not necessarily want this behavior in neovim.

https://github.com/luukvbaal/nnn.nvim/commit/b916ab434c115c300ac69a2a31599947c14ee405 requires users to explicitly set cfg.quitcd (breaking change).

gongfarmer commented 1 year ago

Yes I did find the original behaviour surprising. My workflow is to open vim in the project root dir, and search + open files under that dir. When the dir changes every time I open a buffer, suddenly some files become unavailable to search.

Thanks very much for the change!