1

At the moment when I run nvim path/to/folder/ it opens up the netrw window for it (more accurately, the Dirvish window for it), I'd like this to not be the case.

Ideal behaviour is that:

  • when I do nvim path/to/file.js it opens the file, as normal, and
  • when I do nvim path/to/folder/ it opens the normal empty buffer (the one with the copyright statement and so on) but with the CWD set to path/to/folder/.

This is basically to mimic a bit more the Atom behaviour, where as soon as I open the folder (usually with nvim .)

I can do fuzzy matching for the files right away, Dirvish has bound Ctrl-p (my fuzzy match key) to something else so I can't use it in that case.

2
  • Why would you want an empty screen? You are specifically opening a directory with vim. If you want to change directory, just do CWD
    – 3N4N
    Commented Apr 3, 2018 at 13:47
  • @EnanAjmain as stated in the last paragraph, I want an empty screen so that I can fuzzy search the files right away, at the moment Dirvish taking control of Ctrl-p doesn't permit me to do this.
    – greduan
    Commented Apr 4, 2018 at 15:46

2 Answers 2

0

Actually the problem was a misunderstanding of behaviour from my end.

In Atom, one does atom . to open the current folder. In vim/nvim/vimr one can simply call it without a path and the current folder will be opened as CWD by default.

So the solution is to simply call the command without any path.

Also, I apologize that the question is a bit badly worded and lacks accurate information.

2
  • Your question mentions using nvim /path/to/folder: if you actually need this, you could try nvim +"cd /path/to/folder"
    – Rich
    Commented Apr 4, 2018 at 16:33
  • 1
    In my defense, it is a bit hard to imagine not knowing to open vim without . as argument when you especially know that one -_-
    – 3N4N
    Commented Apr 4, 2018 at 17:13
0

I know this can be off topic but I'd work around the issue by redefining vim in a bash function. It checks whether the last argument is a file or a directory and acts correspondingly.

The bash version:

vim() {
    if [[ $# == 0 ]]; then
        # No arguments: just run vim
        /usr/bin/vim
    elif [[ -d ${!#} ]]; then
        # Last argument is directory: just cd there and open vim without a file name
        cd ${!#}
        /usr/bin/vim ${@:1:$#-1} # strip the last argument
        cd -
    else
        # Last argument is not a directory: act same as vim
        /usr/bin/vim $@
    fi
}

Still this doesnt work when multiple files are to be opened, like vim t.txt /usr/share ../README.

The zsh version

vim() {
    if [[ $# == 0 ]]; then
        # No arguments: just run vim
        /usr/bin/vim
    elif [[ -d ${@:$#:1} ]]; then
        # Last argument is directory: just cd there and open vim without a file name
        cd ${@:$#:1}
        /usr/bin/vim ${@:1:($# -1)} # strip the last argument
        cd -
    else
        # Last argument is regular file: just open that file
        /usr/bin/vim $@
    fi
}
1
  • 2
    FWIW I would use a subshell (cd dir && vim …). Trying to reset the current directory afterward could be error-prone.
    – D. Ben Knoble
    Commented Mar 30, 2023 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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