I quickly want to switch between directories I previously visited without typing :cd and then the full pathname again and again. Is there a plugin or a way that can keep an account of the directories I visited during a vim session from which I can select and go to that directory instantly? I use neovim-qt on Windows 10.


I've made a start at dirstack-like set of commands; you could place the following in (e.g.) ~/.vim/plugin/dirstack.vim:

let g:dir_stack = []
command -complete=dir -nargs=1 -bang -bar Pushd
      \ call add(g:dir_stack, getcwd()) | execute 'cd<bang>' <q-args>
command -bang -bar Popd
      \ let dir = remove(g:dir_stack, -1) | execute 'cd<bang>' dir | unlet dir
command -bar Dirs echo g:dir_stack

I've paid no special attention to :tcd/:lcd (which should be doable, if you're brave enough), or to the special argument - (unecessary; :Popd == :cd -, except that :Popd won't save the last directory like :cd - will), or to :Pushd with no arguments (it's a shame that :cd behaves slightly differently on different platforms).

  • Nice. It would be nice to also have a directory selector and other options to support bash pushd +1 and so on in order to quickly change the current directory for an already stacked one (which becomes the new top of the stack) Sep 27 '20 at 23:30
  • @LucHermitte yeah there are definitely some missing features. Happy for anyone (me included) to build a plugin around it.
    – D. Ben Knoble
    Sep 28 '20 at 0:42
  • 1
    This got me inspired :) github.com/LucHermitte/lh-misc/blob/master/plugin/pushd.vim Sep 28 '20 at 23:27

While in bash I overuse pushd/popd and even a few other helpers on top on them, I don't use a similar workflow in Vim.

I guess it's mostly because I do file management (cp, links, removing, running complex tests...) in the console while I just use vim to edit files (and compile, git add/commit/diff/blame...). When I need to open a file from another project I have plugins that offer navigation (based of ctags or on LSP). And for Vim I have a command dedicated to open anything from the 'runtimepath'.

Once I'm in a buffer that belongs to a project, the current working directory is automatically set to the root directory of that said project (usually determined by where .git/ is, but other mechanisms are available as well). I do that through a feature from my (central) library plugin. And I never change manually the global current directory, not any local buffer directory.

  • Linux has so many useful pre-installed things. Wish they would have come pre-installed in Windows too. ;(
    – sussy_baka
    Sep 28 '20 at 6:30
  • Oh... I am sorry! They do come pre-installed in Windows too. I was not aware of these commands. So, Thankyou! Also, D. Ben Knoble' s answer was sufficient for my work. I just added a command to quickly go to any index of dir_stack.
    – sussy_baka
    Sep 28 '20 at 10:06
  • They are part of bash, and of a few other shells IIRC. Don't forget to update the stack top when you go to any directory indexed in the stack. Otherwise :Popd won't pop the current directory. Sep 28 '20 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.