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

bug: cd and tcd doesn't change directories #407

Closed
3 tasks done
esn89 opened this issue Jun 6, 2024 · 14 comments
Closed
3 tasks done

bug: cd and tcd doesn't change directories #407

esn89 opened this issue Jun 6, 2024 · 14 comments
Labels
bug Something isn't working

Comments

@esn89
Copy link

esn89 commented Jun 6, 2024

Did you check the docs and existing issues?

  • I have read the docs
  • I have searched the existing issues

Neovim version (nvim -v)

NVIM v0.10.0

Operating system/version

MacOS 14.5

Describe the bug

:cd and :tcd does not actually change directories.

Before, I am in ~/.config/nvim/lua/plugins:
with pwd showing that
before

Then, I do :tcd into the oil dir:

after.

The top right gets updated. But when I do pwd it is still in the same dir

What is the severity of this bug?

breaking (some functionality is broken)

Steps To Reproduce

    'stevearc/oil.nvim',
    opts = {},
    dependencies = { 'nvim-tree/nvim-web-devicons' },
    config = function()
        require('oil').setup({
            default_file_explorer = true,
            columns = {
                'icon',
            },
            keymaps = {
                ['<C-v>'] = 'actions.select_vsplit',
                ['<C-s>'] = 'actions.select_split',
                ['<C-f>'] = 'actions.preview_scroll_up',
                ['<C-b>'] = 'actions.preview_scroll_down',
                ['<C-i'] = 'actions.cd',
                ['<C-o'] = 'actions.tcd',
            },
            preview = {
                -- Width dimensions can be integers or a float between 0 and 1 (e.g. 0.4 for 40%)
                -- min_width and max_width can be a single value or a list of mixed integer/float types.
                -- max_width = {100, 0.8} means "the lesser of 100 columns or 80% of total"
                max_width = 0.9,
                -- min_width = {40, 0.4} means "the greater of 40 columns or 40% of total"
                min_width = { 40, 0.4 },
                -- optionally define an integer/float for the exact width of the preview window
                width = nil,
                -- Height dimensions can be integers or a float between 0 and 1 (e.g. 0.4 for 40%)
                -- min_height and max_height can be a single value or a list of mixed integer/float types.
                -- max_height = {80, 0.9} means "the lesser of 80 columns or 90% of total"
                max_height = 0.9,
                -- min_height = {5, 0.1} means "the greater of 5 columns or 10% of total"
                min_height = { 5, 0.1 },
                -- optionally define an integer/float for the exact height of the preview window
                height = nil,
                border = 'rounded',
                win_options = {
                    winblend = 0,
                },
                -- Whether the preview window is automatically updated when the cursor is moved
                update_on_cursor_moved = true,
            },
        })
    end,
}

Expected Behavior

I expect the directory to be changed and reflected in pwd

Directory structure

No response

Repro

return {
    'stevearc/oil.nvim',
    opts = {},
    dependencies = { 'nvim-tree/nvim-web-devicons' },
    config = function()
        require('oil').setup({
            default_file_explorer = true,
            columns = {
                'icon',
            },
            keymaps = {
                ['<C-v>'] = 'actions.select_vsplit',
                ['<C-s>'] = 'actions.select_split',
                ['<C-f>'] = 'actions.preview_scroll_up',
                ['<C-b>'] = 'actions.preview_scroll_down',
                ['<C-i'] = 'actions.cd',
                ['<C-o'] = 'actions.tcd',
            },
            preview = {
                -- Width dimensions can be integers or a float between 0 and 1 (e.g. 0.4 for 40%)
                -- min_width and max_width can be a single value or a list of mixed integer/float types.
                -- max_width = {100, 0.8} means "the lesser of 100 columns or 80% of total"
                max_width = 0.9,
                -- min_width = {40, 0.4} means "the greater of 40 columns or 40% of total"
                min_width = { 40, 0.4 },
                -- optionally define an integer/float for the exact width of the preview window
                width = nil,
                -- Height dimensions can be integers or a float between 0 and 1 (e.g. 0.4 for 40%)
                -- min_height and max_height can be a single value or a list of mixed integer/float types.
                -- max_height = {80, 0.9} means "the lesser of 80 columns or 90% of total"
                max_height = 0.9,
                -- min_height = {5, 0.1} means "the greater of 5 columns or 10% of total"
                min_height = { 5, 0.1 },
                -- optionally define an integer/float for the exact height of the preview window
                height = nil,
                border = 'rounded',
                win_options = {
                    winblend = 0,
                },
                -- Whether the preview window is automatically updated when the cursor is moved
                update_on_cursor_moved = true,
            },
        })
    end,
}

Did you check the bug with a clean config?

  • I have confirmed that the bug reproduces with nvim -u repro.lua using the repro.lua file above.
@esn89 esn89 added the bug Something isn't working label Jun 6, 2024
@stevearc
Copy link
Owner

stevearc commented Jun 6, 2024

I'm very confused by this bug report. You're mentioning using :tcd to change directories, but you're running pwd in the shell (:!pwd) instead of getting vim's cwd (:pwd or :=vim.fn.getcwd()). I also don't see how oil plays into this at all if you're just issuing vim commands.

Could you provide the exact steps you are taking, the exact commands you are running, and what the output is?

@stevearc stevearc added the question Further information is requested label Jun 6, 2024
@esn89
Copy link
Author

esn89 commented Jun 7, 2024

Hi @stevearc

What I mean by this is:
I had expected that the :tcd would also change the directory from where I opened up neovim.

So say I opened up vim in ~/Documents/Projects/app1, if I :tcd upwards, pwd should show ~/Documents/Projects

@github-actions github-actions bot removed the question Further information is requested label Jun 7, 2024
@stevearc
Copy link
Owner

stevearc commented Jun 8, 2024

This should absolutely be the case. If you run

  • nvim --clean
  • :pwd
  • :tcd ..
  • :pwd

I will be shocked if the second pwd doesn't show the parent directory of the first.

Again, I'm not quite sure what this has to do with oil.nvim?

@stevearc stevearc added the question Further information is requested label Jun 8, 2024
@eric-marin
Copy link

eric-marin commented Jun 8, 2024

I have a similar problem.
Sometimes when i use the Oil keymap for actions.cd it does nothing

@github-actions github-actions bot removed the question Further information is requested label Jun 8, 2024
@esn89
Copy link
Author

esn89 commented Jun 8, 2024

@stevearc

Yes, when I run your following 4 commands, it does EXACTLY what I need!

However, in oil.nvim, not so much. And similar to @eric-marin , actions.cd does nothing for me as well.

@stevearc
Copy link
Owner

stevearc commented Jun 9, 2024

In that case I will ask for what is requested in the bug report form:

  1. a complete repro.lua file that is a full, minimal config. A template is provided to make it easy for you.
  2. steps for recreating the state of the filesystem that you are using
  3. clear step-by-step instructions for reproducing the bug

@stevearc stevearc added the question Further information is requested label Jun 9, 2024
@github-actions github-actions bot removed the question Further information is requested label Jun 9, 2024
@eric-marin
Copy link

eric-marin commented Jun 9, 2024

For me, the bug only occurs when I open a file directly from dashboard-nvim, so it might be a dashboard issue.

@stevearc stevearc added the question Further information is requested label Jun 9, 2024
@esn89
Copy link
Author

esn89 commented Jun 10, 2024

@stevearc

cat repro.lua

-- save as repro.lua
-- run with nvim -u repro.lua
-- DO NOT change the paths
local root = vim.fn.fnamemodify('./.repro', ':p')

-- set stdpaths to use .repro
for _, name in ipairs({ 'config', 'data', 'state', 'runtime', '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',
        '--single-branch',
        'https://github.com/folke/lazy.nvim.git',
        lazypath,
    })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
    'folke/tokyonight.nvim',
    {
        'stevearc/oil.nvim',
        config = function()
            require('oil').setup({
                -- add any needed settings here
                {
                    default_file_explorer = true,
                    columns = {
                        'icon',
                    },
                    keymaps = {
                        ['<C-v>'] = 'actions.select_vsplit',
                        ['<C-s>'] = 'actions.select_split',
                        ['<C-f>'] = 'actions.preview_scroll_up',
                        ['<C-b>'] = 'actions.preview_scroll_down',
                        ['<C-i'] = 'actions.cd',
                        ['<C-o'] = 'actions.tcd',
                    },
                    preview = {
                        -- Width dimensions can be integers or a float between 0 and 1 (e.g. 0.4 for 40%)
                        -- min_width and max_width can be a single value or a list of mixed integer/float types.
                        -- max_width = {100, 0.8} means "the lesser of 100 columns or 80% of total"
                        max_width = 0.9,
                        -- min_width = {40, 0.4} means "the greater of 40 columns or 40% of total"
                        min_width = { 40, 0.4 },
                        -- optionally define an integer/float for the exact width of the preview window
                        width = nil,
                        -- Height dimensions can be integers or a float between 0 and 1 (e.g. 0.4 for 40%)
                        -- min_height and max_height can be a single value or a list of mixed integer/float types.
                        -- max_height = {80, 0.9} means "the lesser of 80 columns or 90% of total"
                        max_height = 0.9,
                        -- min_height = {5, 0.1} means "the greater of 5 columns or 10% of total"
                        min_height = { 5, 0.1 },
                        -- optionally define an integer/float for the exact height of the preview window
                        height = nil,
                        border = 'rounded',
                        win_options = {
                            winblend = 0,
                        },
                        -- Whether the preview window is automatically updated when the cursor is moved
                        update_on_cursor_moved = true,
                    },
                },
            })
        end,
    },
    -- add any other plugins here
}
require('lazy').setup(plugins, {
    root = root .. '/plugins',
})

vim.cmd.colorscheme('tokyonight')
-- add anything else here

Here I entered into ~/venv:

img
And oil says that I am in venv.

So I tried hitting the ` and the ~ and it does nothing.

So I just hit enter and went inside of it. And that is when I did :pwd:

img

But still shows that I am in ~

@github-actions github-actions bot removed the question Further information is requested label Jun 10, 2024
@stevearc
Copy link
Owner

Here are my steps with the repro file you gave

  1. cd /private/tmp
  2. mkdir -p testdir
  3. nvim -u repro.lua .
  4. :pwd

/private/tmp

  1. scroll to testdir/ and <CR> to enter the directory
  2. Use ` shortcut to cd to the current directory
  3. :pwd

/private/tmp/testdir

  1. :!pwd

/private/tmp/testdir

Nothing surprising, everything working as intended. Can you provide equally detailed steps that you are taking, with the associated output?

@stevearc stevearc added the question Further information is requested label Jun 11, 2024
@esn89
Copy link
Author

esn89 commented Jun 11, 2024

Aaaaaaaah. Okay, so this is me using it wrong.

I did not know that I had to first ENTER the directory with , then use `.

I thought that by pressing `, it will enter & :cd into it.

My bad.

@esn89 esn89 closed this as completed Jun 11, 2024
@github-actions github-actions bot removed the question Further information is requested label Jun 11, 2024
@lcpichette
Copy link

I believe I am following the steps listed here in this issue and I'm experiencing this same issue. Perhaps I'm being dense, but I tried those steps, in a multitude of permutations, but I think I probably just have some critical misunderstanding of something here.

[email protected]

I'm not sure if it's preferred I open a new ticket or extend this semi-recent one that is directly relevant to me. I'll start here and create a new ticket if requested.

Thank you for your time and assistance.

@stevearc
Copy link
Owner

@lcpichette :cd and :tcd change the working directory of vim. They do not (and in fact, cannot) change the working directory of your shell process after you exit vim.

@lcpichette
Copy link

Ah, I apologize for my misunderstanding. I saw cd and had assumed that it was running cd. But you were more referring to it generally as a change of directory. Thank you for the swift and helpful response!

I think if it did have this ability, which now I understand it cannot, it oil.nvim would be a real competitor to yazi and other file managers. I definitely prefer it for file navigation and management, as I can use my other neovim plugins for enhanced searching and navigation.

Oh well, thanks again!

@lcpichette
Copy link

lcpichette commented Jan 14, 2025

Update: I was able to accomplish this by adding a script to my shell's profile, and an autocmd in my init.lua. I'll post it below in case anyone's interested in using oil.nvim as their file manager.

~/.config/fish/config.fish:

abbr -a nv "nvimcd"
...
# Store `pwd` after we exit neovim + use it after neovim exit to `cd`
function nvimcd
    set tmpfile (mktemp)
    set -x NVIM_LASTDIR_FILE $tmpfile

    nvim $argv
    if test -f $tmpfile
        set lastdir (cat $tmpfile)
        rm -f $tmpfile

        if test -n "$lastdir" -a -d "$lastdir"
            cd $lastdir
        end
    end
end

# Run oil.nvim on shell start
if status --is-interactive
    nvim .
end

~/.config/nvim/init.lua:

-- Stores pwd upon exit
if vim.env.NVIM_LASTDIR_FILE then
  vim.api.nvim_create_autocmd("VimLeavePre", {
    callback = function()
      local cwd = vim.fn.getcwd()
      vim.fn.writefile({ cwd }, vim.env.NVIM_LASTDIR_FILE)
    end,
  })
end

I also have oil run whenever I open directories:
~/.config/nvim/lua/plugins/search.lua:

return {
  ...
  -- OIL: Better file searching and manipulation
  {
    "stevearc/oil.nvim",
    config = function()
      require("oil").setup({
        -- Customize your configuration here
        view_options = {
          show_hidden = true, -- Show hidden files
        },
      })

      -- Automatically open `oil.nvim` for directories
      vim.api.nvim_create_autocmd("VimEnter", {
        callback = function()
          local path = vim.fn.expand("%:p")
          if vim.fn.isdirectory(path) == 1 then
            require("oil").open()
          end
        end,
      })
    end,
  },
}

Here's a video showing it working:

[email protected]

Hope this is able to help someone! Love oil, glad I was able to find a workaround to using it as my defacto file manager.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants