├── README.md └── plugin └── pedant.vim /README.md: -------------------------------------------------------------------------------- 1 | # vim-pedant 2 | Change iterm2 palette when changing colorschemes 3 | 4 | ## The problem 5 | Changing colorscheme won't change the background color of the bordering window. For pedants like me, this isn't acceptable. 6 | 7 | ## The solution 8 | This plugin allows you to map a corresponding `.itermcolors` file to vim's colorscheme, and vim will update the current iterm2 profile to match. Works well with [FZF](https://github.com/junegunn/fzf). 9 | 10 | ## Installation 11 | Install with a vim plugin manager (or don't). 12 | 13 | ## Configuration 14 | Pedant looks for its configuration inside a vim dictionary `g:pedant_options`. Each colorscheme should have its name as the key, and an array as its value, with the first item being the full path the the corresponding `.itermcolors` file, and the second item is passed to `set background=`. 15 | 16 | E.g. In your `.vimrc` add: 17 | ```vim 18 | let g:pedant_options = { 19 | \ 'nova': ['/Users//itermcolors/nova.itermcolors', 'dark'], 20 | \ 'gruvbox': ['/Users//itermcolors/gruvbox.itermcolors', 'light'], 21 | \ 'deep-space': ['/Users//itermcolors/deep-space.itermcolors', 'dark'], 22 | \ } 23 | ``` 24 | 25 | ## Usage 26 | Change colorscheme with `Pcolorscheme` instead of `colorscheme`. 27 | 28 | E.g 29 | ``` 30 | :Pcolorscheme nova 31 | ``` 32 | 33 | ## Notes 34 | This is a nasty hack. 35 | 36 | Initially I just changed the iterm2 profile when changing colorschemes. This worked will with one caveat, font size. I often use += and +- to zoom, but whenever the profile is changed, iterm will revert to the profile's font size. Honestly, font size shouldn't be part of the profile, and there is an open issue regarding this: https://gitlab.com/gnachman/iterm2/issues/5645 37 | 38 | So instead this plugin will update the colors of the current session. This works very well and doesn't change your font size. The downside of this is when a new split is launched it will default to whichever profile is currently selected. Pull requests warmly welcomed! 39 | 40 | Works well with [colorscheme-switcher.vim](http://peterodding.com/code/vim/colorscheme-switcher/). 41 | -------------------------------------------------------------------------------- /plugin/pedant.vim: -------------------------------------------------------------------------------- 1 | " Vim global plugin for changing iterm2 color palette when changing vim colorscheme 2 | " Last Change: 2017-03-29 3 | " Maintainer: Sheldon Johnson 4 | " License: MIT 5 | 6 | function! PedantColorScheme(colorscheme) 7 | if !exists("g:pedant_options") 8 | echo "g:pedant_options variable not defined" 9 | else 10 | let &background = g:pedant_options[a:colorscheme][1] 11 | let g:itermcolors_file = g:pedant_options[a:colorscheme][0] 12 | call UpdateIterm() 13 | execute "colorscheme " . a:colorscheme 14 | endif 15 | endfunction 16 | 17 | function! UpdateIterm() 18 | ruby << EOF 19 | require 'plist' 20 | 21 | class ITermColorFile 22 | LOOKUP = { "0" => "Ansi 0 Color", 23 | "1" => "Ansi 1 Color", 24 | "2" => "Ansi 2 Color", 25 | "3" => "Ansi 3 Color", 26 | "4" => "Ansi 4 Color", 27 | "5" => "Ansi 5 Color", 28 | "6" => "Ansi 6 Color", 29 | "7" => "Ansi 7 Color", 30 | "8" => "Ansi 8 Color", 31 | "9" => "Ansi 9 Color", 32 | "a" => "Ansi 10 Color", 33 | "b" => "Ansi 11 Color", 34 | "c" => "Ansi 12 Color", 35 | "d" => "Ansi 13 Color", 36 | "e" => "Ansi 14 Color", 37 | "f" => "Ansi 15 Color", 38 | "g" => "Foreground Color", 39 | "h" => "Background Color", 40 | "l" => "Cursor Color" } 41 | 42 | def initialize(file) 43 | @xml_obj = Plist::parse_xml(file) 44 | end 45 | 46 | def update_iterm 47 | VIM::command("silent !echo -e \"#{vim_escape_codes}\"") 48 | end 49 | 50 | private 51 | 52 | def vim_escape_codes 53 | to_hash.map { |identifier, color| vim_escape_code(identifier, color) }.join 54 | end 55 | 56 | def vim_escape_code(identifier, color) 57 | "\\\\033]P#{identifier}#{color}\\\\033\\\\\\\\" 58 | end 59 | 60 | def to_hash 61 | @to_hash ||= LOOKUP.transform_values do |identifier| 62 | xml_color_obj = @xml_obj[identifier] 63 | normal_color = NormalColor.new(r: xml_color_obj['Red Component'], 64 | g: xml_color_obj['Green Component'], 65 | b: xml_color_obj['Blue Component']) 66 | normal_color.to_hex_color.to_s 67 | end 68 | end 69 | end 70 | 71 | class Color 72 | def initialize(r:, g:, b:) 73 | @red = r 74 | @green = g 75 | @blue = b 76 | end 77 | 78 | def to_hash 79 | { r: @red, g: @green, b: @blue } 80 | end 81 | end 82 | 83 | class RGBColor < Color 84 | def to_hex_color 85 | hsh = to_hash.transform_values { |value| "%02x" % value } 86 | HexColor.new hsh 87 | end 88 | end 89 | 90 | class NormalColor < Color 91 | def to_rgb_color 92 | hsh = to_hash.transform_values { |value| 255 * value } 93 | RGBColor.new hsh 94 | end 95 | 96 | def to_hex_color 97 | to_rgb_color.to_hex_color 98 | end 99 | end 100 | 101 | class HexColor < Color 102 | def to_s 103 | "#{@red}#{@green}#{@blue}" 104 | end 105 | end 106 | 107 | file = VIM::evaluate("g:itermcolors_file") 108 | colors = ITermColorFile.new(file) 109 | colors.update_iterm 110 | EOF 111 | endfunction 112 | 113 | command! -nargs=1 Pcolorscheme call PedantColorScheme() 114 | --------------------------------------------------------------------------------