Vim NERDTree: show only .txt files?
Asked Answered
J

3

7

Using NERDTree plugin, I want to view only *.txt files. There is a NERDTreeIgnore variable, but I want something like NERDTreeWhitelistFilter.

Is there a way to whitelist what I see?

Junina answered 27/6, 2011 at 12:6 Comment(0)
T
4

This is what you want:

:let NERDTreeIgnore += ['\(\.txt\)\@<!$[[file]]']
Tulipwood answered 1/7, 2012 at 1:55 Comment(5)
What does $[[file]] mean, Where can I get the information about it? Thank you for your response.Deimos
@AylwynLake: please type :help 'NERDTreeIgnore' in you vim.Tulipwood
Yes, I had checked the help file. And There is no any [file] defined. Maybe My version 4.2.0 not contain this keyword?Deimos
@AylwynLake take a look at github.com/scrooloose/nerdtree/blob/master/doc/NERD_tree.txtTulipwood
Thank you very much. I download it from vim.org/scripts/script.php?script_id=1658 , and that version does not provide this feature. My problem solves.Deimos
C
5

I've been playing around with this — it's an interesting problem. Maybe you could try out this regular expression for ignoring files?

Edit: talked with my co-worker. Here's the correct regular expression (my original one matched "txt" at the beginning of a file name, too).

^(?!.*\.txt$).*
Colous answered 27/6, 2011 at 17:1 Comment(5)
I think that's the way to go. Ignore everything that does not end in txt, in other words.Carnap
didn't work. I typed :let NERDTreeIgnore=^(?!.*\.txt$).* and it gave the following error: E15: Invalid expression: ^(?!.*\.txt$).*Junina
stevereads.com/weblog/2007/03/26/… shows how to do vim lookbehinds. When focused on the NERDTree, I manually search ^\(.*\.txt$\)\@!.* and n will proceed through every single line except txt files -- and that's exactly what I want. but ... when I type :let NERDTreeIgnore=^\(.*\.txt$\)\@!.* it gives me the same error as previous comment. So we're getting closer. We now have the regex in "vim-speak", but I can't seem to set NERDTreeIgnore to it (or anything else, so I'm doing something wrong).Junina
AAaagh!! So I finally set the variable right, but it still won't work. I type :let NERDTreeIgnore=['^\(.*\.txt$\)\@!.*'] (and I checked three times to make sure I typed it right), but after refreshing, it shows nothing. But manually searching that regex worked (see last post). I typed :let NERDTreeIgnore=['resx'] and it properly ignored all .resx files too :(. This is way too hard.Junina
But how to ignore anyfile except those end with '.cpp' or '.h'. I try let NERDTreeIgnore=['^(.*\.cpp\|h$)\@!.*'], it not works. I also want this regex not ignore directories.Deimos
T
4

This is what you want:

:let NERDTreeIgnore += ['\(\.txt\)\@<!$[[file]]']
Tulipwood answered 1/7, 2012 at 1:55 Comment(5)
What does $[[file]] mean, Where can I get the information about it? Thank you for your response.Deimos
@AylwynLake: please type :help 'NERDTreeIgnore' in you vim.Tulipwood
Yes, I had checked the help file. And There is no any [file] defined. Maybe My version 4.2.0 not contain this keyword?Deimos
@AylwynLake take a look at github.com/scrooloose/nerdtree/blob/master/doc/NERD_tree.txtTulipwood
Thank you very much. I download it from vim.org/scripts/script.php?script_id=1658 , and that version does not provide this feature. My problem solves.Deimos
Z
0

based on @kev great answer you can also make a toggle function.


function NERDRefreshIfOpened()
    if g:NERDTree.IsOpen()
            :NERDTreeRefreshRoot
    endif
endfunction

function NERDTxtOnlyToggle()
    let regex='\(\.txt\)\@<!$[[file]]'
    if index(g:NERDTreeIgnore, regex) >= 0
        call filter(g:NERDTreeIgnore, {idx, val -> val != regex})
        call NERDRefreshIfOpened()
    else
        let g:NERDTreeIgnore += [regex]
        call NERDRefreshIfOpened()
    endif
endfunction
Zelaya answered 20/2, 2023 at 17:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.