Search code examples
vimsyntaxfile-typecolor-scheme

vim - map keys to edit colorscheme and syntax files


I have mapped <Leader>ev to open .vimrc for editing using:

nmap <Leader>ev :e $MYVIMRC<CR>

I now wish to map <Leader>ec to edit currently used colorscheme file, and also map <Leader>es to edit current filetype syntax file. I don't want to hard code the paths (or part of the paths) as I will be moving around between environments. It will help me quickly adjust color/syntax in middle of editing.

Any help?


Solution

  • The paths from which Vim loads colorschemes, plugins, etc., are determined from the 'runtimepath' setting. The globpath() function can perform lookups from it. By taking the first match, we'll get the path to the current colorscheme:

    :echo get(split(globpath(&runtimepath, 'colors/' . g:colors_name . '.vim'), "\n"), 0, '')
    

    Something similar can be done for filetype plugins; the current one is in &filetype.

    To insert the path into the mapping, you can use <C-R>= (insert register contents, used with the expression register):

    :nnoremap <Leader>ec :edit <C-R>=get(split(globpath(&runtimepath, 'colors/' . g:colors_name . '.vim'), "\n"), 0, '')<CR><CR>