3

I like to use vim's built-in netrw plugin to list files of project. Sometimes when I expand a folder to see its content I need to fold it back and go to the next folder. For example, at the right side of the screen I want to fold components folder and easily go to elementcss.

enter image description here

Is it possible?

UPDATE

I need to fold when my cursor inside of components folder. Because I can just press enter to fold it.

Timur Fayzrakhmanov
  • 17,967
  • 20
  • 64
  • 95

2 Answers2

4

I'm assuming that you're using netrw's tree listing mode: with the cursor atop a directory name, [return] will toggle between displaying the directory's contents and not doing so.

The (shift+return) mapping, present since netrw v151 (Mar '14) will also do what you're asking for (squeeze the parent directory). Its restricted to use with gvim since most terminals won't pass a shift-return along to vim.

user21497
  • 1,061
  • 8
  • 9
0

I also needed to collapse a parent folder, so I created this function and mapped it to the x key.

" map x key to collapse parent folder
autocmd FileType netrw nmap <buffer> x :call NetrwCollapse()<CR>

function! NetrwCollapse()
    redir => cnt
        silent .s/|//gn
    redir END
    let lvl = substitute(cnt, '\n', '', '')[0:0] - 1
    exec '?^\(| \)\{' . lvl . '\}\w'
    exec "normal \<CR>"
endfunction

Edit: I have changed the mapping to say autocmd FileType instead of autocmd filetype because some systems require the capitalization.

Edit2: Changed the mapping to only call <CR> once. Then added a normal <CR> to the end of the function, so it is called from there. Works better on newer systems.

unione
  • 371
  • 2
  • 6
  • This overrides the default mapping for `x` for opening html, jpeg, etc. files (`:help netrw-x`) – darw Feb 11 '21 at 18:53