Nerdtree: File coloring based on type.

Created on 31 Oct 2012  路  27Comments  路  Source: preservim/nerdtree

Is it possible to change the color of files based on their extension?

Most helpful comment

For anyone looking for this, I created this: https://github.com/tiagofumo/vim-nerdtree-syntax-highlight

Take a look at the screenshots and see if you like it. You can use it with vim-devicons or not. If you don't want to use it with vim-devicons, add the following line to your .vimrc:

let g:NERDTreeFileExtensionHighlightFullName = 1

All 27 comments

Of course, I think this is a good idea.

I think it would make sense to use the .dircolors file if it exists (probably only unix machines).

If .dircolors file does not exists, then let user custom color with regex pattern. Like that sort option.

wow pretty dam fast response!! :+1:

Were you talking of something like that ? :)

NERDTree has its own filetype - i.e. nerdtree. So you could just chuck something like this in ~/.vim/syntax/nerdtree.vim

syn match NERDTreeTxtFile #^\s\+.*txt$#
hi def link NERDTreeTxtFile error

This is a really crude way (could probably find a much better regex) to highlight .txt files with the error highlight group.

If you want to really define your own colors you could do this:

syn match NERDTreeTxtFile #^\s\+.*txt$#
highlight NERDTreeTxtFile ctermbg=blue ctermfg=magenta

Thoughts?

We could write a helper function to wrap this up - something like:

 function! NERDTreeHighlightFile(extension, fg, bg)
    exec 'autocmd filetype nerdtree syn match NERDTreeTxtFile #^\s\+.*'. a:extension .'$#'
    exec 'autocmd filetype nerdtree highlight NERDTreeTxtFile ctermbg='. a:bg .' ctermfg='. a:fg .' guibg='. a:bg .' guifg='. a:fg
endfunction

call NERDTreeHighlightFile('txt', 'blue', 'black')

This is just an hacky example implementation written in 2 minutes - there is a lot wrong with it. But you get an idea of the interface.

Anyway, for now just use the first method i described and post anything you do back here. Ill be interested to see what comes up - if there is enough interest then we can look at how people are using the highlighting and come up with an API for it

@tUrG0n yes.

@scrooloose cool. I agreed.

@scrooloose ty for the detailed answer.

Based on it I modified it a bit into the following:

function! NERDTreeHighlightFile(extension, fg, bg)
 exec 'autocmd filetype nerdtree syn match ' . a:extension .' #^\s\+.*'. a:extension .'$#'
 exec 'autocmd filetype nerdtree highlight ' . a:extension .' ctermbg='. a:bg .' ctermfg='. a:fg .' guibg='. a:bg .' guifg='. a:fg
endfunction

Your original code did now allow for defining colors for various files because all were using NERDTreeTxtFile . So the only difference in this code is that match will be defined as the file's extension... ie txt instead of NERDTreeTxtFile.

I quickly hacked the following since it matches the files i'm using in my projects:

" NERDTress File highlighting
function! NERDTreeHighlightFile(extension, fg, bg)
 exec 'autocmd filetype nerdtree syn match ' . a:extension .' #^\s\+.*'. a:extension .'$#'
 exec 'autocmd filetype nerdtree highlight ' . a:extension .' ctermbg='. a:bg .' ctermfg='. a:fg .' guibg='. a:bg .' guifg='. a:fg
endfunction

call NERDTreeHighlightFile('jade', 'green', 'black')
call NERDTreeHighlightFile('html', 'green', 'black')
call NERDTreeHighlightFile('styl', 'green', 'black')
call NERDTreeHighlightFile('css', 'green', 'black')
call NERDTreeHighlightFile('coffee', 'cyan', 'black')

It would be interesting to consider dircolors instead :)

Cheers!

It would be rather easy to strip the color from the dircolors file since relevant lines would like like .mp3 00;36. The problem then becomes finding a good translation between ANSI and vim color. I do know that some scripts for this have already been made.

I think use Vim color function wrapper instead of .dircolors is better, because not everyone has a .dircolors.

@NagatoPain i think the idea is that there would be a second function that parses .dircolors and calls off to the highlight wrapper function :)

@tUrG0n much nicer :) There are lots of other changes that could be made to the body - e.g. store the extention/colors in a hash and just add a single autocmd to set up the highlights. I think the interface is the important thing at the moment though - just to try and scope out what behaviour people want.

@scrooloose I agreed, two solution with one option to specify.

So would that be incorporated into the repo? If so, how should we proceed? I never create a vim plugin per-say haha! (only played with the vimrc!)

If NERDTree read from dircolors that would definitely be very cool

I am using Iterm2 + Solarized Dark theme. Can't get highlighting based on file type to work. I tried all variations including
autocmd VimEnter * call NERDTreeHighlightFile('jade', 'green', 'none', 'green', '#151515')

Tried the one in vim-devicons FAQ as well

" NERDTress File highlighting
function! NERDTreeHighlightFile(extension, fg, bg, guifg, guibg)
exec 'autocmd FileType nerdtree highlight ' . a:extension .' ctermbg='. a:bg .' ctermfg='. a:fg .' guibg='. a:guibg .' guifg='. a:guifg
exec 'autocmd FileType nerdtree syn match ' . a:extension .' #^\s\+.*'. a:extension .'$#'
endfunction

call NERDTreeHighlightFile('jade', 'green', 'none', 'green', '#151515')
call NERDTreeHighlightFile('ini', 'yellow', 'none', 'yellow', '#151515')
call NERDTreeHighlightFile('md', 'blue', 'none', '#3366FF', '#151515')
call NERDTreeHighlightFile('yml', 'yellow', 'none', 'yellow', '#151515')
call NERDTreeHighlightFile('config', 'yellow', 'none', 'yellow', '#151515')
call NERDTreeHighlightFile('conf', 'yellow', 'none', 'yellow', '#151515')
call NERDTreeHighlightFile('json', 'yellow', 'none', 'yellow', '#151515')
call NERDTreeHighlightFile('html', 'yellow', 'none', 'yellow', '#151515')
call NERDTreeHighlightFile('styl', 'cyan', 'none', 'cyan', '#151515')
call NERDTreeHighlightFile('css', 'cyan', 'none', 'cyan', '#151515')
call NERDTreeHighlightFile('coffee', 'Red', 'none', 'red', '#151515')
call NERDTreeHighlightFile('js', 'Red', 'none', '#ffa500', '#151515')
call NERDTreeHighlightFile('php', 'Magenta', 'none', '#ff00ff', '#151515')
call NERDTreeHighlightFile('ds_store', 'Gray', 'none', '#686868', '#151515')
call NERDTreeHighlightFile('gitconfig', 'Gray', 'none', '#686868', '#151515')
call NERDTreeHighlightFile('gitignore', 'Gray', 'none', '#686868', '#151515')
call NERDTreeHighlightFile('bashrc', 'Gray', 'none', '#686868', '#151515')
call NERDTreeHighlightFile('bashprofile', 'Gray', 'none', '#686868', '#151515')

@atoder Look at solarized.vim, and figure out what colour in what mode you want.

If you want a funny potpourri of colors, this somewhat resembles the colors github uses for it's languages.

" source files
call NERDTreeHighlightFile('.c', '11', 'NONE', 'NONE', 'NONE')
call NERDTreeHighlightFile('h', '3', 'NONE', 'NONE', 'NONE')
call NERDTreeHighlightFile('cc', '5', 'NONE', 'NONE', 'NONE')
call NERDTreeHighlightFile('mm', '4', 'NONE', 'NONE', 'NONE')
" shell scripts
call NERDTreeHighlightFile('sh', '2', 'NONE', 'NONE', 'NONE')
call NERDTreeHighlightFile('bash', '2', 'NONE', 'NONE', 'NONE')
call NERDTreeHighlightFile('zsh', '2', 'NONE', 'NONE', 'NONE')
" makefiles
call NERDTreeHighlightFile('mk', '13', 'NONE', 'NONE', 'NONE')
call NERDTreeHighlightFile('makefile', '13', 'NONE', 'NONE', 'NONE')
call NERDTreeHighlightFile('Makefile', '13', 'NONE', 'NONE', 'NONE')

Obviously only has support for languages / files I regularly use, extend as you wish. And you might want to add colors for GUI mode, if you use Vim in a GUI. (Who does that?)

For anyone looking for this, I created this: https://github.com/tiagofumo/vim-nerdtree-syntax-highlight

Take a look at the screenshots and see if you like it. You can use it with vim-devicons or not. If you don't want to use it with vim-devicons, add the following line to your .vimrc:

let g:NERDTreeFileExtensionHighlightFullName = 1

@tiagofumo wow, that looks amazing! Thanks for sharing :)

What does everyone here think? With @tiagofumo's NERDTree plugin, can we consider this issue closed?

I just tested it out and it makes the cursor navigation really slow when you have a medium amount of dirs! 馃 That said, the autocmd method above only works initially, but if you toggle the NerdTree window the colors reset. I found a workaround and that is to put the original commands in the after/syntax/ directory

Here is a basic syntax file I got working for nerdtree colors. Make sure you put in after/syntax/ dir. https://github.com/cmcginty/dotfiles/blob/master/vim/vim/after/syntax/nerdtree.vim

@PhilRunninger
"Plugin 'tiagofumo/vim-nerdtree-syntax-highlight'
SUPER SLOWS down Scrolling in NERD tree.

When I was testing it.. the Nerd Tree was scrolling super slow.. as soon as I removed it.. it got fast again..

TOTAL      COUNT  MATCH   SLOWEST     AVERAGE   NAME               PATTERN
  0.017599   5081   4845    0.000262    0.000003  NERDTreeDir        [^鈻锯柛 ].*/
  0.014696   1982   0       0.000127    0.000007  nerdtreePatternMatchLabel_requirejs \v\c[a-zA-Z0-9_#-+*%!~(){}&.$@]*require[a-zA-Z0-9_#-+*%!~(){}&.$@]*\.js\*$
  0.013565   1982   0       0.000144    0.000007  nerdtreePatternMatchLabel_materializejs \v\c[a-zA-Z0-9_#-+*%!~(){}&.$@]*materialize[a-zA-Z0-9_#-+*%!~(){}&.$@]*\.js\*$
  0.013483   1982   0       0.000230    0.000007  nerdtreePatternMatchLabel_angularjs \v\c[a-zA-Z0-9_#-+*%!~(){}&.$@]*angular[a-zA-Z0-9_#-+*%!~(){}&.$@]*\.js\*$
  0.013425   1982   0       0.000337    0.000007  nerdtreePatternMatchLabel_materializejs \v\c[a-zA-Z0-9_#-+*%!~(){}&.$@]*materialize[a-zA-Z0-9_#-+*%!~(){}&.$@]*\.js$
  0.013409   1982   0       0.000062    0.000007  nerdtreePatternMatchLabel_mootoolsjs \v\c[a-zA-Z0-9_#-+*%!~(){}&.$@]*mootools[a-zA-Z0-9_#-+*%!~(){}&.$@]*\.js$
  0.013386   1982   0       0.000034    0.000007  nerdtreePatternMatchLabel_requirejs \v\c[a-zA-Z0-9_#-+*%!~(){}&.$@]*require[a-zA-Z0-9_#-+*%!~(){}&.$@]*\.js$
  0.013306   1982   0       0.000093    0.000007  nerdtreePatternMatchLabel_backbonejs \v\c[a-zA-Z0-9_#-+*%!~(){}&.$@]*backbone[a-zA-Z0-9_#-+*%!~(){}&.$@]*\.js$
....

@atoder,
you can configure to use the plugin only for the extensions/rules that you need, and that can make scrolling a little bit faster. In the README file, it is stated:

Warning: This is sort of a hack and has some limitations.

I would love to merge a pull request with a fix for this performance issue. Feel free to contribute.

I am going to close this issue. @tiagofumo's plugin is a good start. Good luck to those who choose to tackle improving its performance.

It would be rather easy to strip the color from the dircolors file since relevant lines would like like .mp3 00;36. The problem then becomes finding a good translation between ANSI and vim color. I do know that some scripts for this have already been made.

Any update on this part? I'd love to integrate LS_COLORS or ~/.dircolors seamlessly into NERDTree.

I've not heard of anyone tackling the .dircolors integration. As NERDTree is already quite bloated, this would be a perfect candidate to be written as a separate plugin, like @tiagofumo has done.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jordwalke picture jordwalke  路  4Comments

nicknisi picture nicknisi  路  5Comments

DapperFox picture DapperFox  路  4Comments

NikosEfthias picture NikosEfthias  路  3Comments

sunnyguan picture sunnyguan  路  4Comments