3

I use the ranger plugin wich I can trigger with a shortcut. But when I open a directory I would like to use the plugin instead of netrw. So I have found that the filetype of directories is netrw, using set filetypeon a directory, and decided to put an autocommand in my vimrc:

autocmd FileType netrw :Ranger<CR>

But it doesn't work. I can't find anything on the web on autocmds on directory. Anyone has an idea on how to achieve this?

4

I had a look at how Dirvish did it, and tested this:

augroup ranger
    au!
    au VimEnter * sil! au! FileExplorer *
    au BufEnter * if s:isdir(expand('%')) | bd | exe 'Ranger' | endif
augroup END

fu! s:isdir(dir) abort
    return !empty(a:dir) && (isdirectory(a:dir) ||
                \ (!empty($SYSTEMDRIVE) && isdirectory('/'.tolower($SYSTEMDRIVE[0]).a:dir)))
endfu

It seems to work but I don't know how reliable it is.

s:isdir() is a function which tests whether the path of the current buffer matches a directory.
The VimEnter autocmd seems to get rid of netrw.
The BufEnter autocmd deletes the buffer (if you want to wipe it completely, replace bd with bw), then calls the ranger file manager.

In the Dirvish plugin, before calling the :Dirvish command, the BufEnter autocmd redraws the screen and echo an empty string (redraw | echo ''). I don't know why it does this, so I didn't copied it, but if you have some issue (maybe with rendering artifacts, sometimes :redraw! is used to get rid of them), try to add these commands back:

au BufEnter * if s:isdir(expand('%'))
                  \| bd | redraw | echo '' | exe 'Ranger' | endif
| improve this answer | |
2

The vim-ranger plugin provides a similar integration with ranger, and accomplishes this out of the box. You can look at the code to see how it does that if you're interesting in duplicating this behavior with the other plugin.


Update: I realized that disabling netrw as vim-ranger plugin disables gx to view files (I hadn't realized that this was part of netrw).

As a result, I've gone back to using ranger.vim with the above accepted answer solution (the Dirvish mimicry) to auto open directories with ranger. This keeps the useful netrw functionality intact and seems to be the best option.

| improve this answer | |
1
  • Vim's default file explorer, the one you get when you do $ vim ., is Netrw. See :help netrw.
  • Netrw is written as a plugin but it is very tightly integrated into Vim. Forcing another plugin in place of netrw won't be as simple as an autocommand. If you are curious you can take a look at how some popular alternative file explorers do that: Dirvish and NERDTree.

As a very lightweight alternative, you could create a simple alias:

$ alias vim.='vim +Ranger'

which allows you to do:

$ vim.
$ vim. path/to/some/dir

But at this point… why not simply press E in ranger?

| improve this answer | |
  • Vim-ranger is a plugin than can open ranger within vim (maybe it's neovim only, I'm not sure wether thin can open terminal session), as a replacement of netrw. I just run the command :Ranger. I just want to make it so that the Ranger command would run when I accendentally open a directory instead of using :Ranger, insted of opening netrw. There's a trick from the NERDToggle github to launch it when passing a directory from a command line, but that's just one case... – Nicolas Scotto Di Perto Dec 1 '16 at 22:16
  • Yes I know what vim-ranger is. – romainl Dec 2 '16 at 7:06
0

Disabling netrw is easy: put let g:loaded_netrw= 1 and let g:netrw_loaded_netrwPlugin= 1 into your .vimrc. That way netrw won't be loaded, none of its maps installed, etc.

| improve this answer | |

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.