vim - netrw

Netrw

Vim documentation: http://vimdoc.sourceforge.net/htmldoc/pi_netrw.html#netrw

Open question - netrw - always fix the width at 25%. Right now, it is at 25% on start-up but when a new buffer is opened, its size is changing.

Always show line numbers in netrw:

" netrw_bufsettings - you can control netrw's buffer settings; change
" these if you want to change line number displays, relative line number
" displays and other settings in netrw menu.
let g:netrw_bufsettings = 'noma nomod nu nobl nowrap ro'

The file explorer is just another Vim buffer, so you can navigate up and down with k and j keys, or jump to the bottom with G, etc. If it is a large file listing, you may be quicker finding your target by searching for it.

There are a handful of useful commands for opening the file explorer - either in the current window or a split, focusing on the project root, or the directory of the most recent file edited. This table summarizes:

lazy mnemonic open file explorer
:e. :edit . at current working directory
:sp. :split . in split at current working directory
:vs. :vsplit . in vertical split at current working directory
:E : Explore at directory of current file
:Se :Sexplore in split at directory of current file
:Vex :Vexplore in vertical split at directory of current file

Manipulating the filesystem

The file explorer includes commands for creating new files and directories, as well as renaming or deleting existing ones. This table summarizes these:

command action
% create a new file
d create a new directory
R rename the file/directory under the cursor
D Delete the file/directory under the cursor

Invoking netrw can be achieved in three ways

command action
:Explore (:E) opens netrw in the current window
:Sexplore (:Sex) opens netrw in a horizontal split
:Vexplore (:Vex) opens netrw in a vertical split
:Texplore (:Tex) opens netrw in a new tab (use Cntrl PgUp, Cntrl PgDn to switch between tabs)

NERDtree like setup

If NERDtree is your thing, netrw can give you a similar experience with the following settings

let g:netrw_banner = 0 (To remove the directory banner)
let g:netrw_liststyle = 3
let g:netrw_browse_split = 4
let g:netrw_altv = 1
let g:netrw_winsize = 25
augroup ProjectDrawer
  autocmd!
  autocmd VimEnter * :Vexplore
augroup END

Vim also supports arbitrary commands to be run following !. For a quick directory listing the following works… :! ls -lF For a more complex command other commands like ack, grep or find can be used.


There are a number of ways to open files in vim and if that is what you use netrw for.

Using find within vim can open files and supports tab completion. :find path/to/file.txt

To open a file in a vertical split use the following. This also supports tab completion. :vs path/to/file.txt

To open a file in a horizontal split use the following. This also supports tab completion. :sp path/to/file.txt

To open a file in a new tab use the following. This also supports tab completion. :tabnew path/to/file.txt


How do I configure .vimrc so that line numbers display in netrw in Vim?

I’m using netrw to read directory listings in Vim, and I would like to display line numbers in my netrw tabs (so I can use :24 to navigate through directory listings faster). I’m using “set number” in my vimrc to enable line numbers when editing files, but this does not display line numbers in netrw.

When in netrw, if I type the command “:set number”, the line numbers display, but as soon as I change directories the line numbers go away.

Is there a configuration option I can put in .vimrc that will make line numbers show up in netrw windows?

From autoload/netrw.vim in the runtime:

call s:NetrwInit(“g:netrw_bufsettings” , “noma nomod nonu nobl nowrap ro”)

s:NetrwInit overrides a variable only if it is not defined.

Therefore put let g:netrw_bufsettings = ’noma nomod nu nobl nowrap ro’ in your vimrc and it should work.


Further Reading :help netrw :help :edit :help :Explore


Vim netrw customizations

"nnoremap <leader>u :UndotreeShow<CR>
"open netrw to the left with a size 30
"Not using this anymore because I am using NerdTree now.

" NETRW CUSTOMIZATIONS

"let g:netrw_liststyle = 3 " custom settings for the netrw file/directory menu...
                          " available netrw_liststyle options -
                          " 1 (thin), 2 (long), 3 (wide), 4 (tree)
"let g:netrw_browse_split = 3 " netrw_browse_split| option - zero by default
                             " used to cause the opening of files to be done in a new window or tab instead of the default.
                             " When the option is one or two, the splitting will be taken horizontally or vertically, respectively.
                             " When the option is set to three, a <cr> will cause the file to appear in a new tab.
"let g:netrw_banner = 0 " enable/suppress the banner. =0: suppress the banner =1: banner is enabled (default)
"let g:netrw_winsize = 25 " netrw_winsize -
                         " specify initial size of new windows made with "o" (see |netrw-o|), "v" (see |netrw-v|),
                         " |:Hexplore| or |:Vexplore|.
                         " The g:netrw_winsize is an integer describing
                         " the percentage of the current netrw buffer's window to be used for the new window.
"let g:netrw_bufsettings = 'noma nomod nu nobl nowrap ro' " netrw_bufsettings - you can control netrw's buffer settings; change
                                                         " these if you want to change line number displays, relative line number
                                                           " displays and other settings in netrw menu.
Map Action
<F1> Causes Netrw to issue help
<cr> Netrw will enter the directory or read the file
<del> Netrw will attempt to remove the file/directory
- Makes Netrw go up one directory
a Toggles between normal display, hiding (suppress display of files matching g:netrw_list_hide) showing (display only files which match g:netrw_list_hide)
c Make browsing directory the current directory
C Setting the editing window
d Make a directory
D Attempt to remove the file(s)/directory(ies)
gb Go to previous bookmarked directory
gh Quick hide/unhide of dot-files
<c-h> Edit file hiding list
i Cycle between thin, long, wide and tree listings
<c-l> Causes Netrw to refresh the directory listing
mb Bookmark current directory
mc Copy marked files to marked-file target directory
md Apply diff to marked files (up to 3)
me Place marked files on arg list and edit them
mf Mark a file
mh Toggle marked file suffices’ presence on hiding list
mm Move marked files to marked-file target directory
mp Print marked files
mr Mark files satisfying a shell-style regexp
mt Current browsing directory becomes markfile target
mT Apply ctags to marked files
mu Unmark all marked files
mx Apply arbitrary shell command to marked files
mz Compress/decompress marked files
o Enter the file/directory under the cursor in a new browser window. A horizontal split is used.
O Obtain a file specified by cursor
p Preview the file
P Browse in the previously used window
qb List bookmarked directories and history
qf Display information on file
r Reverse sorting order
R Rename the designed file(s)/directory(ies)
s Select sorting style: by name, time or file size
S Specify suffix priority for name-sorting
t Enter the file/directory under the cursor in a new tab
u Change to recently-visited directory
U Change to subsequently-visited directory
v Enter the file/directory under the cursor in a new browser window. A vertical split is used.
x View file with an associated program
X Execute filename under cursor via system()
% Open a new file in netrw’s current directory

Links to this note