As I understand, in Linux directories are mostly normal files w/ some special handling (e.g. 'rm' a file is ok; 'rm' a dir will complain w/o -r).

When I execute the following bash commands:

mkdir foo; touch foo/f1.txt; touch foo/f2.txt; vim foo

Vim shows the contents of the foo directory-file as:

" ============================================================================
" Netrw Directory Listing                                        (netrw v136)
"   /home/capdigi/foo
"   Sorted by      name
"   Sort sequence: [\/]$,\<core\%(\.\d\+\)\=\>,\.h$,\.c$,\.cpp$,*,\.o$,\.obj$,\.info$,\.swp$,\.bak$,\~$
"   Quick Help: <F1>:help  -:go up dir  D:delete  R:rename  s:sort-by  x:exec
" ============================================================================

My questions are:

1) Why is netrw used in the context of this example? According to http://www.vim.org/scripts/script.php?script_id=1075, netrw "supports reading and writing files across networks". Is this just the component that vim uses for uniform local/network directory access?

2) Why is netrw needed at all? Since directories are "mostly" normal files, can't they just be opened as a normal file without any intermediary layer? Aka why all the fanciness?


In the historical Unix system, directories were implemented as normal files with a special mode indicating that they were directories. This is no longer true on many modern filesystems and operating systems. When it comes to the on-disk structures, a directory may or may not be represented as a file-like blob of storage, depending on the filesystem. When it comes to the operating system interfaces, there are separate system calls to access directories and regular files: opendir, readdir, rewinddir, closedir corresponding to open, read, rewind, close respectively.

Software that reads a directory must use the directory-specific interfaces. When you open a directory in Vim, it doesn't load its contents, the way it would for a file. Vim doesn't have any native handling for directories: if you run vim --noplugin on a directory, Vim complains that "foo" is a directory and won't save the buffer for that same reason.

The primary goal of netrw is to access remote files (over a variety of network protocols). Since you can't easily go and run commands like ls and cp in the directories containing these remote files, netrw includes code to browse directories and manipulate them — it's a file manager as well as a remote file browser. These file manager capacities make sense locally as well as remotely, so netrw registers itself as a handler for local directories in addition to remote files and directories.


Access to the raw directory data is not possible. Write access is not permitted because this could lead to filesystem inconsistencies. Read access is not provided because the format depends on the filesystem. (So, it's better to enforce some level of abstraction) See also: How are directories implemented in Unix filesystems?

On unix, vim will use the opendir, readdir, closedir interface (see manpages) to read directory entries one by one, without being able to access the raw data. I guess Netrw is just a higher level abstraction for this interface, which extends to various 'non-unix' filesystems.

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.