2

I added let g:netrw_bufsettings = 'noma nomod nu nobl nowrap ro' and set nu to my vimrc on my Ubuntu machine. It can display line numbers in netrw (V149).

When I use the same vimrc on a remote server with netrw (V125), the line numbers disappear.

How can I make netrw (V125) display line numbers? There is no way to update netrw on the remote server.

:set number in netrw works, I wonder if there is anyway to set it in the .vimrc file.

  • 2
    I don't have any Vim that old on hand to test, but if Netrw sets the filetype to be netrw in V125, you could try au FileType netrw set nu, perhaps. – muru May 21 '16 at 18:49
  • 1
    I added that line to my vimrc, but it didn't work. – user3813057 May 21 '16 at 20:29
  • what's the output of vim --version? – muru May 21 '16 at 20:30
  • version is vim 7.0 – user3813057 May 21 '16 at 20:31
  • no, the full output please. Anyway, with a Vim that old, it won't matter, I suppose. – muru May 21 '16 at 20:31
4

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:

set updatetime=100
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 CursorHold autocmd.

  • 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()
function! Netrw()
   Explore
   set number
endfunction

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. :)

augroup netrw_remap
   autocmd!
   autocmd filetype netrw call Remap_netrw()
augroup END

function! Remap_netrw()
   noremap <buffer> -     :call NumberExplore()<CR>
   noremap <buffer> <CR>  :call NumberInspect()<CR>
endfunction

This is a good start, we now have control over what - and <CR> do when in a netrw buffer. Now we can rewrite them.

function! NumberExplore()
   if (expand('%') != '')
      cd %
   endif
   call netrw#LocalBrowseCheck(expand('%:p:h:h'))
   set number
endfunction

function! NumberInspect()
   if (expand('%') != '')
      cd %
   endif
   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"). "')"
   else
      " If the line doesn't end in a / then treat it like a file
      exec "edit ". fnamemodify(file, ":p")
   endif
   set number
endfunction

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 expand('%:p:h:h) the % 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.

  • :set number works. I wonder if there is any way to set the line number in the vimrc file. (question updated.) – user3813057 May 21 '16 at 18:05
  • In your vimrc file you can simply put set number. This will, of course put line numbers in all files, not just netrw. If you want just netrw you probably could set up some autocmds to set that option when a netrw buffer opens. – Tumbler41 May 22 '16 at 13:33
  • 2
    Actually I put set number in my vimrc file, and that works fine in most of the modes. But it does not work in netrw. And yes when I type :set nu explicitly in netrw, the line numbers appear. – user3813057 May 23 '16 at 16:54
  • @user3813057 netrw's filetype settings might unset number – muru May 26 '16 at 0:02
  • Thanks a lot for your detailed answer. I'll wait for another couple of days, and accept your answer if there is no better one. – user3813057 May 26 '16 at 1:57

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.