Is there any reason that
:set number doesn't work?
Edit: I see what you mean @user3813057. Netrw has a lot of control over its buffer, so we have to be a bit tricky when doing things that it doesn't support (or in your case, that it doesn't support yet). I do have a couple of ways of doing this, but they're all kind of hacky. So take it or leave it I guess. :)
If you do anything based on an autocmd, it's likely that netrw is doing something as well and will overwrite whatever you did. So we either have to base our operation on something else or make sure that we get the last say in setting options. I have a way to do both, but again, they're pretty hacky.
First method: Timer
Let's start with the simpler of the two:
autocmd CursorHold * if (&filetype == 'netrw' && &number == 0) | set number | endif
set updatetime=100: this times (in milliseconds) how long it's been
since the user has typed something. If it gets to zero, it writes the
swp file to the disk and calls the
autocmd CursorHold ...: This line runs a command everytime the
CursorHold event is triggered. It checks if it's in a netrw buffer
and the number option is off. If so, it sets the option.
This works pretty well, but as stated above, it's also changing how often the swp file gets written to disk, and if anything else is using the
CursorHold event, it could mess that up too. You can mess with the amount of time a bit and see what works best for you.
Second method: Controlling netrw
The next way is something that I'm doing currently in my vimrc, but some might be queasy about because we're actually grabbing the reins of netrw and doing a lot of the work for our self. I'm doing this because I wanted netrw to point at the file I just came from instead of starting at the beginning of the list every time. However, we can use this same technique to turn on the number option.
command! Ex :call Netrw()
This part is not so bad. It simply overrides the
:Ex command to make sure that once the
Explore operation is complete it sets the number option. The only problem is this will only work for the first page of netrw. As soon as you navigate to a different directory, netrw will turn off line numbers again. To complicate matters, netrw is using its own internal functions, making harder to replicate. But let's try anyway. :)
autocmd filetype netrw call Remap_netrw()
noremap <buffer> - :call NumberExplore()<CR>
noremap <buffer> <CR> :call NumberInspect()<CR>
This is a good start, we now have control over what
<CR> do when in a netrw buffer. Now we can rewrite them.
if (expand('%') != '')
if (expand('%') != '')
let file = getline('.')
" Get line that cursor is on
if (file =~ '/$')
" If the line ends in a / then treat it like a directory
exec "call netrw#LocalBrowseCheck('" . fnamemodify(file, ":p"). "')"
" If the line doesn't end in a / then treat it like a file
exec "edit ". fnamemodify(file, ":p")
A few notes for clarification:
netrw#LocalBrowseCheck is netrw's internal navigation function.
- We use
cd % to make sure all paths are correct when navigating.
exec is a function that runs other functions. It allows us to use
parameters that are evaluated at runtime.
fnamemodify() is a great function for extending file names to their full path.
- In the expression
% stands for the current file, the
:p fills the whole absolute path,
:h removes the last slash or directory/file name. So in our case the first
:h removes the slash and the second one removes the directory name making us go up one directory.
- On a side note, make sure you have
syntax on. I'm not sure why, but this doesn't work without it.
Now whenever you navigate, it will call netrw's internal navigation functions and then tops it off with a
set number giving us the desired result. ^.^
Now I know this was an extremely long-winded answer to something that you just wanted a quick and dirty solution for. Unfortunately, the best solution is only enabled in a later version of netrw (as you noted).
Third method: Editing netrw
There are some obvious downsides to this method, namely updating netrw would undo it, and your install of netrw will not be "official", making it hard for any outsiders to help should there a be a problem. Nonetheless it is pretty simple and therefore worth mentioning. @user3813057 pointed out that you can simply edit netrw.vim found under
Vim/vim##/autoload/netrw.vim. There are two places where netrw specifies
nonu. You can either take this command out to have full control over the numbering, or you can change it to
nu to make netrw always turn on the numbers.