Autoload netrw when starting vim

1.6k Views Asked by At

I want netrw to autoload when I launch vim using the terminal. Completely new to linux/ubuntu. Is there any way of doing that?

3

There are 3 best solutions below

0
On BEST ANSWER

Adding the following to your .vimrc (Vim's configuration file, located in the root of your home directory) will cause Vim to automatically load Netrw after starting up.

" Open Netrw after Vim starts up
augroup InitNetrw
  autocmd!
  autocmd VimEnter * :silent! Explore
augroup END

A problem with the preceding approach, as implemented, is that Netrw will also load when you use Vim with an argument to open a specific file. A workaround is to use the following modification, based on the suggested approach in Netrw's documentation (:help netrw-activate).

" Checks if there is a file open after Vim starts up,
" and if not, open the current working directory in Netrw.
augroup InitNetrw
  autocmd!
  autocmd VimEnter * if expand("%") == "" | edit . | endif
augroup END

The following pages have more details on autocommands and the .vimrc configuration file.

0
On

And the following code block in your vimrc:

set autochdir
let g:netrw_browse_split=4
augroup InitNetrw
    autocmd!
    autocmd VimEnter * if argc() == 0 | Lexplore! | endif
augroupend

Kind of does what @dannyadam suggested. But opens the netrw pane as a side bar on the right. If you want to be on the right use Lexplore without the bang(!).

0
On

Nvim with netrwPlugin

The original question relates to vim, but I was searching for the same question for implementing the modern Nvim API for the same request.

Here are the steps that I followed

  1. Ensure that you have already loaded the netrwPlugin (I think some package managers by default, remove this plugin at startup). In my case I used lazy-nvim and I have in the config: disabled_plugins = {"gzip", "matchit", "matchparen", "netrwPlugin","tarPlugin","tohtml", "tutor", "zipPlugin"}

  2. Use the nvim_create_autocmd

-- Moder Lua auto-command for launching netrwPlugin which shipped with the nvim
local mygroup = vim.api.nvim_create_augroup("loading_netrwPlugin", {clear = true})
vim.api.nvim_create_autocmd({"VimEnter"}, {
  pattern = {"*"},
  command  = ":silent! Explore",
  group = mygroup
})

  1. Put it in your init.lua file.

Or, simply use:

vim.cmd([[
augroup loading_netrwPlugin
autocmd!
autocmd VimEnter * :silent! Explore
augroup END
]])


Using netrwPluing with Lazy PluginManger

When I enter nvim, it should launch the netrwPlugin if I pass a file_name, it should automatically open the filename. We have to add more checking, as the netrwPlugin will loaded even when the Lazy pop-up menu is loaded, and that will create a conflict, we can prevent that as shown below

local mygroup = vim.api.nvim_create_augroup("loading_netrwPlugin",
                                            {clear = true})
vim.api.nvim_create_autocmd({"VimEnter"}, {
    pattern = {"*"},
    callback = function()
        -- Getting the file name that you pass when you launch nvim,
        local current_file = vim.fn.expand("%")
        -- if we have already file_name, then, we edit it
        if current_file ~= "" then
            vim.cmd(":silent! edit " .. current_file)
        else
            -- We will check if the window (buffer) is the lazy nvim, as it conflict if the buffer (popup menu) is lazy
            local lazy_popup_buf_exists = false
            -- We will get list of all current opened buffers
            local buf_list = vim.api.nvim_list_bufs()
            for _, buf in ipairs(buf_list) do
                -- We will obtain from the table only the filetype
                local buf_ft = vim.api.nvim_buf_get_option(buf, 'filetype')
                if buf_ft == "lazy" then
                    lazy_popup_buf_exists = true
                end
            end -- Check if vim-floaterm is loaded
            local has_floaterm, _ = pcall(require, 'floaterm')
            if not lazy_popup_buf_exists and not has_floaterm then
                -- Then we can safely loading netrwPlugin at startup
                vim.cmd(":silent! Explore")
            end
        end
    end,
    group = mygroup
})

  • This is valid with the assumption, no startup dashboard plugin is installed (e.g. dashboard-nvim, alpha ..etc.)