146

When I edit files in my ~/.vim, the .netrwhist file would mysteriously be changed, too.

It's content:

let g:netrw_dirhistmax  =10
let g:netrw_dirhist_cnt =6
...and so on...

What does this file do? Is it important?

Lai Yu-Hsuan
  • 27,509
  • 28
  • 97
  • 164

4 Answers4

129

netrw is a kind of vim plugin/script which supports reading and writing files across networks. .netrwhist is a history file which maintains all the directories that were modified. So whenever you modify the contents of ~/.vim it adds one entry in .netrwhist

A sample .netrwhist is as shown

let g:netrw_dirhistmax  =10
let g:netrw_dirhist_cnt =6
let g:netrw_dirhist_1='/Users/wolever/EnSi/repos/web/env/web/lib/python2.6/site-packages/django'
let g:netrw_dirhist_2='/private/tmp/b/.hg/attic'
let g:netrw_dirhist_3='/Users/wolever/code/sandbox/pydhcplib-0.6.2/pydhcplib'
let g:netrw_dirhist_4='/Users/wolever/EnSi/repos/common/env/common/bin'
let g:netrw_dirhist_5='/Users/wolever/EnSi/repos/common/explode'
let g:netrw_dirhist_6='/Users/wolever/Sites/massuni-wiki/conf'

netrw_dirhistmax indicates the maximum number of modified directories it stores in the history file. ie Max History Size. netrw_dirhist_cnt indicates the current history count of modified directories.

If you want to disable netrw to generate history file, then see this.

Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
19

In addition, if one sets g:netrw_dirhistmax to zero, netrw will save no history or bookmarks:

:let g:netrw_dirhistmax = 0

Doing this will not cause any prior .netrwhist or .netrwbook files to be deleted, however.

glts
  • 21,808
  • 12
  • 73
  • 94
user21497
  • 1,061
  • 8
  • 9
  • 3
    @glts but...what's the downside of netrw saving no history bookmarks? What's different about my experience using Vim instead? – Han Seoul-Oh Mar 26 '20 at 06:26
  • @HanSeoul-Oh, see `:h netrw-u`, `:h netrw-U`, and `:h netrw-Th`. – ma11hew28 Apr 07 '21 at 15:10
  • Re: ma11hew28 those help texts refer to a `u`/`U` command to go back and forward through the bookmarks and the `Th` seems to be some sort of marking or targetting of one of the directories. I didn't look too closely but it seems the benefit of these bookmarks are just for that manual navigation benefit. Deleting them will not affect performance or anything, you just won't have the bookmarks remembering your previous directories. – NeilG Jun 23 '22 at 01:05
13

In addition, in case you want vim to respect the XDG base directory specifications in order to prevent your home folder from being littered up by dotfiles like ~/.vim, you may want to split cached files and history files from your configuration (which usually resides in the runtime path). So for example, to store .netrwhist in ~/.cache/vim, you may want to try

let g:netrw_home=$XDG_CACHE_HOME.'/vim'
ayekat
  • 333
  • 4
  • 9
  • If your .vim folder is somewhere other than .vim, presumably this new location is on vim's runtimepath. g:netrw_home is set to the first directory on the runtimepath by default, which in turn is by default $HOME/.vim. So if you (ayekat) have your runtimepath set up properly, you should not have to modify g:netrw_home. – user21497 Nov 14 '14 at 16:37
  • 1
    @user21497 This is true. On the other hand, if you wish to separate your configuration from things like swap files, undo files, or (as here) `netrwhist`, properly setting the runtimepath only partly solves the problem. But I'll edit the answer to make this a little more clear. – ayekat Nov 21 '14 at 13:14
5

From the netrw reference manual:

                        *.netrwhist*
See |g:netrw_dirhistmax| for how to control the quantity of history stack
slots.  The file ".netrwhist" holds history when netrw (and vim) is not
active.  By default, it's stored on the first directory on the user's
|'runtimepath'|.

In my case, the first path in runtimepath is ~/.vim (check with :echo &runtimepath). I'm good with that, so I don't need to change g:netrw_home.


  *g:netrw_dirhistmax*            =10: controls maximum quantity of past
                                       history.  May be zero to supppress
                                       history.

So, yeah, let g:netrw_dirhistmax=0 will stop writing to the history file.

ericbn
  • 10,163
  • 3
  • 47
  • 55