Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

File coloring based on type. #201

Closed
ghost opened this issue Oct 31, 2012 · 27 comments
Closed

File coloring based on type. #201

ghost opened this issue Oct 31, 2012 · 27 comments

Comments

@ghost
Copy link

ghost commented Oct 31, 2012

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

@stardiviner
Copy link

Of course, I think this is a good idea.

@derekbrokeit
Copy link

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

@stardiviner
Copy link

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

@ghost
Copy link
Author

ghost commented Oct 31, 2012

wow pretty dam fast response!! 👍

Were you talking of something like that ? :)

@scrooloose
Copy link
Collaborator

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

@stardiviner
Copy link

@Turg0n yes.

@stardiviner
Copy link

@scrooloose cool. I agreed.

@ghost
Copy link
Author

ghost commented Oct 31, 2012

@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!

@derekbrokeit
Copy link

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.

@stardiviner
Copy link

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

@scrooloose
Copy link
Collaborator

@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.

@stardiviner
Copy link

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

@ghost
Copy link
Author

ghost commented Nov 5, 2012

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!)

@nomcopter
Copy link

If NERDTree read from dircolors that would definitely be very cool

@atoder
Copy link

atoder commented Mar 16, 2016

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')

@Leandros
Copy link
Contributor

Leandros commented Apr 24, 2016

@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?)

@tiagofumo
Copy link

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

@EdvinOlofsson
Copy link

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

@PhilRunninger
Copy link
Member

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

@cmcginty
Copy link

cmcginty commented Jan 11, 2017

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

@ryanoasis
Copy link

FYI, related solutions: ryanoasis/vim-devicons#158

@cmcginty
Copy link

cmcginty commented Jan 12, 2017

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

@atoder
Copy link

atoder commented Feb 28, 2017

@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$
....

@tiagofumo
Copy link

@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.

@PhilRunninger
Copy link
Member

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.

@hozza
Copy link

hozza commented Jul 5, 2019

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.

@PhilRunninger
Copy link
Member

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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests