mirror of
https://github.com/junegunn/fzf.vim.git
synced 2025-12-06 12:44:24 +08:00
Use colors that match the current color scheme and add g:fzf_colors
Close #59 and #112
This commit is contained in:
28
README.md
28
README.md
@@ -87,6 +87,8 @@ pathogen#helptags()`. [↩](#a1))
|
|||||||
|
|
||||||
### Customization
|
### Customization
|
||||||
|
|
||||||
|
#### Global options
|
||||||
|
|
||||||
```vim
|
```vim
|
||||||
" This is the default extra key bindings
|
" This is the default extra key bindings
|
||||||
let g:fzf_action = {
|
let g:fzf_action = {
|
||||||
@@ -99,13 +101,37 @@ let g:fzf_action = {
|
|||||||
" - window (nvim only)
|
" - window (nvim only)
|
||||||
let g:fzf_layout = { 'down': '~40%' }
|
let g:fzf_layout = { 'down': '~40%' }
|
||||||
|
|
||||||
|
" Customize fzf colors to match your color scheme
|
||||||
|
let g:fzf_colors =
|
||||||
|
\ { 'fg': ['fg', 'Normal'],
|
||||||
|
\ 'bg': ['bg', 'Normal'],
|
||||||
|
\ 'hl': ['fg', 'Comment'],
|
||||||
|
\ 'fg+': ['fg', 'CursorLine', 'CursorColumn', 'Normal'],
|
||||||
|
\ 'bg+': ['bg', 'CursorLine', 'CursorColumn'],
|
||||||
|
\ 'hl+': ['fg', 'Statement'],
|
||||||
|
\ 'info': ['fg', 'PreProc'],
|
||||||
|
\ 'prompt': ['fg', 'Conditional'],
|
||||||
|
\ 'pointer': ['fg', 'Exception'],
|
||||||
|
\ 'marker': ['fg', 'Keyword'],
|
||||||
|
\ 'spinner': ['fg', 'Label'],
|
||||||
|
\ 'header': ['fg', 'Comment'] }
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Command-local options
|
||||||
|
|
||||||
|
```vim
|
||||||
" [Buffers] Jump to the existing window if possible
|
" [Buffers] Jump to the existing window if possible
|
||||||
let g:fzf_buffers_jump = 1
|
let g:fzf_buffers_jump = 1
|
||||||
|
|
||||||
" [[B]Commits] to customize the options used by 'git log':
|
" [[B]Commits] to customize the options used by 'git log':
|
||||||
let g:fzf_commits_log_options = '--graph --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr"'
|
let g:fzf_commits_log_options = '--graph --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr"'
|
||||||
|
```
|
||||||
|
|
||||||
" Advanced customization using autoload functions
|
#### Advanced customization using autoload functions
|
||||||
|
|
||||||
|
You can use autoload functions to define your own commands.
|
||||||
|
|
||||||
|
```vim
|
||||||
autocmd VimEnter * command! Colors
|
autocmd VimEnter * command! Colors
|
||||||
\ call fzf#vim#colors({'left': '15%', 'options': '--reverse --margin 30%,0'})
|
\ call fzf#vim#colors({'left': '15%', 'options': '--reverse --margin 30%,0'})
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -56,13 +56,29 @@ else
|
|||||||
endfunction
|
endfunction
|
||||||
endif
|
endif
|
||||||
|
|
||||||
function! s:ansi(str, col, bold)
|
function! s:get_color(attr, ...)
|
||||||
return printf("\x1b[%s%sm%s\x1b[m", a:col, a:bold ? ';1' : '', a:str)
|
for group in a:000
|
||||||
|
let code = synIDattr(synIDtrans(hlID(group)), a:attr)
|
||||||
|
if code =~ '^[0-9]\+$'
|
||||||
|
return code
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
|
return ''
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
for [s:c, s:a] in items({'black': 30, 'red': 31, 'green': 32, 'yellow': 33, 'blue': 34, 'magenta': 35, 'cyan': 36})
|
let s:ansi = {'black': 30, 'red': 31, 'green': 32, 'yellow': 33, 'blue': 34, 'magenta': 35, 'cyan': 36}
|
||||||
execute "function! s:".s:c."(str, ...)\n"
|
|
||||||
\ " return s:ansi(a:str, ".s:a.", get(a:, 1, 0))\n"
|
function! s:ansi(str, group, default, ...)
|
||||||
|
let fg = s:get_color('fg', a:group)
|
||||||
|
let bg = s:get_color('bg', a:group)
|
||||||
|
let color = (empty(fg) ? s:ansi[a:default] : ('38;5;'.fg)) .
|
||||||
|
\ (empty(bg) ? '' : (';48;5;'.bg))
|
||||||
|
return printf("\x1b[%s%sm%s\x1b[m", color, a:0 ? ';1' : '', a:str)
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
for s:color_name in keys(s:ansi)
|
||||||
|
execute "function! s:".s:color_name."(str, ...)\n"
|
||||||
|
\ " return s:ansi(a:str, get(a:, 1, ''), '".s:color_name."')\n"
|
||||||
\ "endfunction"
|
\ "endfunction"
|
||||||
endfor
|
endfor
|
||||||
|
|
||||||
@@ -70,11 +86,17 @@ function! s:buflisted()
|
|||||||
return filter(range(1, bufnr('$')), 'buflisted(v:val)')
|
return filter(range(1, bufnr('$')), 'buflisted(v:val)')
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function! s:defaults()
|
||||||
|
let rules = copy(get(g:, 'fzf_colors', {}))
|
||||||
|
let colors = join(map(items(filter(map(rules, 'call("s:get_color", v:val)'), '!empty(v:val)')), 'join(v:val, ":")'), ',')
|
||||||
|
return empty(colors) ? '' : ('--color='.colors)
|
||||||
|
endfunction
|
||||||
|
|
||||||
function! s:fzf(opts, extra)
|
function! s:fzf(opts, extra)
|
||||||
let extra = copy(get(a:extra, 0, {}))
|
let extra = copy(get(a:extra, 0, {}))
|
||||||
let eopts = has_key(extra, 'options') ? remove(extra, 'options') : ''
|
let eopts = has_key(extra, 'options') ? remove(extra, 'options') : ''
|
||||||
let merged = extend(copy(a:opts), extra)
|
let merged = extend(copy(a:opts), extra)
|
||||||
let merged.options = join(filter([get(merged, 'options', ''), eopts], '!empty(v:val)'))
|
let merged.options = join(filter([s:defaults(), get(merged, 'options', ''), eopts], '!empty(v:val)'))
|
||||||
call fzf#run(merged)
|
call fzf#run(merged)
|
||||||
return 1
|
return 1
|
||||||
endfunction
|
endfunction
|
||||||
@@ -202,7 +224,7 @@ function! fzf#vim#_lines(all)
|
|||||||
call extend(b == buf ? cur : rest,
|
call extend(b == buf ? cur : rest,
|
||||||
\ filter(
|
\ filter(
|
||||||
\ map(lines,
|
\ map(lines,
|
||||||
\ '(!a:all && empty(v:val)) ? "" : printf("[%s]\t%s:\t%s", s:blue(b), s:yellow(v:key + 1), v:val)'),
|
\ '(!a:all && empty(v:val)) ? "" : printf(s:blue("%2d\t", "TabLine").s:yellow(" %4d ", "LineNr")."\t%s", b, v:key + 1, v:val)'),
|
||||||
\ 'a:all || !empty(v:val)'))
|
\ 'a:all || !empty(v:val)'))
|
||||||
endfor
|
endfor
|
||||||
return extend(cur, rest)
|
return extend(cur, rest)
|
||||||
@@ -235,7 +257,7 @@ endfunction
|
|||||||
|
|
||||||
function! s:buffer_lines()
|
function! s:buffer_lines()
|
||||||
return map(getline(1, "$"),
|
return map(getline(1, "$"),
|
||||||
\ 'printf("%s:\t%s", s:yellow(v:key + 1), v:val)')
|
\ 'printf(s:yellow(" %4d ", "LineNr")."\t%s", v:key + 1, v:val)')
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! fzf#vim#buffer_lines(...)
|
function! fzf#vim#buffer_lines(...)
|
||||||
@@ -280,10 +302,10 @@ endfunction
|
|||||||
|
|
||||||
function! s:history_source(type)
|
function! s:history_source(type)
|
||||||
let max = histnr(a:type)
|
let max = histnr(a:type)
|
||||||
let fmt = '%'.len(string(max)).'d'
|
let fmt = ' %'.len(string(max)).'d '
|
||||||
let list = filter(map(range(1, max), 'histget(a:type, - v:val)'), '!empty(v:val)')
|
let list = filter(map(range(1, max), 'histget(a:type, - v:val)'), '!empty(v:val)')
|
||||||
return extend([' :: Press '.s:magenta('CTRL-E').' to edit'],
|
return extend([' :: Press '.s:magenta('CTRL-E', 'Special').' to edit'],
|
||||||
\ map(list, 's:yellow(printf(fmt, len(list) - v:key)).": ".v:val'))
|
\ map(list, 's:yellow(printf(fmt, len(list) - v:key), "Number")." ".v:val'))
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
nnoremap <plug>(-fzf-vim-do) :execute g:__fzf_command<cr>
|
nnoremap <plug>(-fzf-vim-do) :execute g:__fzf_command<cr>
|
||||||
@@ -394,12 +416,12 @@ endfunction
|
|||||||
function! s:format_buffer(b)
|
function! s:format_buffer(b)
|
||||||
let name = bufname(a:b)
|
let name = bufname(a:b)
|
||||||
let name = empty(name) ? '[No Name]' : name
|
let name = empty(name) ? '[No Name]' : name
|
||||||
let flag = a:b == bufnr('') ? s:blue('%') :
|
let flag = a:b == bufnr('') ? s:blue('%', 'Conditional') :
|
||||||
\ (a:b == bufnr('#') ? s:magenta('#') : ' ')
|
\ (a:b == bufnr('#') ? s:magenta('#', 'Special') : ' ')
|
||||||
let modified = getbufvar(a:b, '&modified') ? s:red(' [+]') : ''
|
let modified = getbufvar(a:b, '&modified') ? s:red(' [+]', 'Exception') : ''
|
||||||
let readonly = getbufvar(a:b, '&modifiable') ? '' : s:green(' [RO]')
|
let readonly = getbufvar(a:b, '&modifiable') ? '' : s:green(' [RO]', 'Constant')
|
||||||
let extra = join(filter([modified, readonly], '!empty(v:val)'), '')
|
let extra = join(filter([modified, readonly], '!empty(v:val)'), '')
|
||||||
return s:strip(printf("[%s] %s\t%s\t%s", s:yellow(a:b), flag, name, extra))
|
return s:strip(printf("[%s] %s\t%s\t%s", s:yellow(a:b, 'Number'), flag, name, extra))
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:sort_buffers(...)
|
function! s:sort_buffers(...)
|
||||||
@@ -571,7 +593,7 @@ function! fzf#vim#tags(query, ...)
|
|||||||
let proc = 'grep -v ''^\!'' '
|
let proc = 'grep -v ''^\!'' '
|
||||||
let copt = ''
|
let copt = ''
|
||||||
else
|
else
|
||||||
let proc = 'perl -ne ''unless (/^\!/) { s/^(.*?)\t(.*?)\t/\x1b[33m\1\x1b[m\t\x1b[34m\2\x1b[m\t/; print }'' '
|
let proc = 'perl -ne ''unless (/^\!/) { s/^(.*?)\t(.*?)\t/'.s:yellow('\1', 'Function').'\t'.s:blue('\2', 'String').'\t/; print }'' '
|
||||||
let copt = '--ansi '
|
let copt = '--ansi '
|
||||||
endif
|
endif
|
||||||
return s:fzf(fzf#vim#wrap({
|
return s:fzf(fzf#vim#wrap({
|
||||||
@@ -612,7 +634,7 @@ let s:nbs = nr2char(0x2007)
|
|||||||
|
|
||||||
function! s:format_cmd(line)
|
function! s:format_cmd(line)
|
||||||
return substitute(a:line, '\C \([A-Z]\S*\) ',
|
return substitute(a:line, '\C \([A-Z]\S*\) ',
|
||||||
\ '\=s:nbs.s:yellow(submatch(1)).s:nbs', '')
|
\ '\=s:nbs.s:yellow(submatch(1), "Function").s:nbs', '')
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:command_sink(cmd)
|
function! s:command_sink(cmd)
|
||||||
@@ -622,7 +644,7 @@ endfunction
|
|||||||
|
|
||||||
function! s:format_excmd(ex)
|
function! s:format_excmd(ex)
|
||||||
let match = matchlist(a:ex, '^|:\(\S\+\)|\s*\S*\(.*\)')
|
let match = matchlist(a:ex, '^|:\(\S\+\)|\s*\S*\(.*\)')
|
||||||
return printf(" \x1b[34m%-38s\x1b[m%s", s:nbs.match[1].s:nbs, s:strip(match[2]))
|
return printf(' '.s:blue('%-38s', 'Statement').'%s', s:nbs.match[1].s:nbs, s:strip(match[2]))
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:excmds()
|
function! s:excmds()
|
||||||
@@ -708,7 +730,7 @@ function! fzf#vim#helptags(...)
|
|||||||
|
|
||||||
return s:fzf({
|
return s:fzf({
|
||||||
\ 'source': "grep -H '.*' ".join(map(tags, 'shellescape(v:val)')).
|
\ 'source': "grep -H '.*' ".join(map(tags, 'shellescape(v:val)')).
|
||||||
\ "| perl -ne '/(.*?):(.*?)\t(.*?)\t/; printf(qq(\x1b[33m%-40s\x1b[m\t%s\t%s\n), $2, $3, $1)' | sort",
|
\ "| perl -ne '/(.*?):(.*?)\t(.*?)\t/; printf(qq(".s:green('%-40s', 'Label')."\t%s\t%s\n), $2, $3, $1)' | sort",
|
||||||
\ 'sink': s:function('s:helptag_sink'),
|
\ 'sink': s:function('s:helptag_sink'),
|
||||||
\ 'options': '--ansi +m --tiebreak=begin --with-nth ..-2'}, a:000)
|
\ 'options': '--ansi +m --tiebreak=begin --with-nth ..-2'}, a:000)
|
||||||
endfunction
|
endfunction
|
||||||
@@ -733,7 +755,7 @@ function! s:format_win(tab, win, buf)
|
|||||||
let name = bufname(a:buf)
|
let name = bufname(a:buf)
|
||||||
let name = empty(name) ? '[No Name]' : name
|
let name = empty(name) ? '[No Name]' : name
|
||||||
let active = tabpagewinnr(a:tab) == a:win
|
let active = tabpagewinnr(a:tab) == a:win
|
||||||
return (active? s:blue('> ') : ' ') . name . (modified? s:red(' [+]') : '')
|
return (active? s:blue('> ', 'Operator') : ' ') . name . (modified? s:red(' [+]', 'Exception') : '')
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:windows_sink(line)
|
function! s:windows_sink(line)
|
||||||
@@ -747,9 +769,9 @@ function! fzf#vim#windows(...)
|
|||||||
let buffers = tabpagebuflist(t)
|
let buffers = tabpagebuflist(t)
|
||||||
for w in range(1, len(buffers))
|
for w in range(1, len(buffers))
|
||||||
call add(lines,
|
call add(lines,
|
||||||
\ printf('%s:%s: %s',
|
\ printf('%s %s %s',
|
||||||
\ s:yellow(printf('%3d', t)),
|
\ s:yellow(printf('%3d', t), 'Number'),
|
||||||
\ s:cyan(printf('%3d', w)),
|
\ s:cyan(printf('%3d', w), 'String'),
|
||||||
\ s:format_win(t, w, buffers[w-1])))
|
\ s:format_win(t, w, buffers[w-1])))
|
||||||
endfor
|
endfor
|
||||||
endfor
|
endfor
|
||||||
@@ -816,9 +838,9 @@ function! s:commits(buffer_local, args)
|
|||||||
\ })
|
\ })
|
||||||
|
|
||||||
if a:buffer_local
|
if a:buffer_local
|
||||||
let options.options .= ',ctrl-d --header ":: Press '.s:magenta('CTRL-S').' to toggle sort, '.s:magenta('CTRL-D').' to diff"'
|
let options.options .= ',ctrl-d --header ":: Press '.s:magenta('CTRL-S', 'Special').' to toggle sort, '.s:magenta('CTRL-D', 'Special').' to diff"'
|
||||||
else
|
else
|
||||||
let options.options .= ' --header ":: Press '.s:magenta('CTRL-S').' to toggle sort"'
|
let options.options .= ' --header ":: Press '.s:magenta('CTRL-S', 'Special').' to toggle sort"'
|
||||||
endif
|
endif
|
||||||
|
|
||||||
return s:fzf(options, a:args)
|
return s:fzf(options, a:args)
|
||||||
@@ -850,8 +872,8 @@ endfunction
|
|||||||
|
|
||||||
function! s:highlight_keys(str)
|
function! s:highlight_keys(str)
|
||||||
return substitute(
|
return substitute(
|
||||||
\ substitute(a:str, '<[^ >]\+>', "\x1b[33m\\0\x1b[m", 'g'),
|
\ substitute(a:str, '<[^ >]\+>', s:yellow('\0', 'Special'), 'g'),
|
||||||
\ "\x1b[33m<Plug>\x1b[m", "\x1b[34m<Plug>\x1b[m", 'g')
|
\ '<Plug>', s:blue('<Plug>', 'SpecialKey'), 'g')
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:key_sink(line)
|
function! s:key_sink(line)
|
||||||
@@ -881,7 +903,7 @@ function! fzf#vim#maps(mode, ...)
|
|||||||
let curr = line[3:]
|
let curr = line[3:]
|
||||||
else
|
else
|
||||||
let src = ' '.join(reverse(reverse(split(src, '/'))[0:2]), '/')
|
let src = ' '.join(reverse(reverse(split(src, '/'))[0:2]), '/')
|
||||||
call add(list, printf('%s %s', curr, s:black(src)))
|
call add(list, printf('%s %s', curr, s:black(src, 'Comment')))
|
||||||
let curr = ''
|
let curr = ''
|
||||||
endif
|
endif
|
||||||
endfor
|
endfor
|
||||||
|
|||||||
@@ -116,7 +116,9 @@ But its functionality is still available via `call pathogen#helptags()`.)
|
|||||||
< Customization >_____________________________________________________________~
|
< Customization >_____________________________________________________________~
|
||||||
*fzf-vim-customization*
|
*fzf-vim-customization*
|
||||||
|
|
||||||
*g:fzf_action* *g:fzf_layout* *g:fzf_commits_log_options*
|
*g:fzf_action* *g:fzf_layout* *g:fzf_colors* *g:fzf_commits_log_options*
|
||||||
|
|
||||||
|
Global options~
|
||||||
>
|
>
|
||||||
" This is the default extra key bindings
|
" This is the default extra key bindings
|
||||||
let g:fzf_action = {
|
let g:fzf_action = {
|
||||||
@@ -129,18 +131,43 @@ But its functionality is still available via `call pathogen#helptags()`.)
|
|||||||
" - window (nvim only)
|
" - window (nvim only)
|
||||||
let g:fzf_layout = { 'down': '~40%' }
|
let g:fzf_layout = { 'down': '~40%' }
|
||||||
|
|
||||||
|
" Customize fzf colors to match your color scheme
|
||||||
|
let g:fzf_colors =
|
||||||
|
\ { 'fg': ['fg', 'Normal'],
|
||||||
|
\ 'bg': ['bg', 'Normal'],
|
||||||
|
\ 'hl': ['fg', 'Comment'],
|
||||||
|
\ 'fg+': ['fg', 'CursorLine', 'CursorColumn', 'Normal'],
|
||||||
|
\ 'bg+': ['bg', 'CursorLine', 'CursorColumn'],
|
||||||
|
\ 'hl+': ['fg', 'Statement'],
|
||||||
|
\ 'info': ['fg', 'PreProc'],
|
||||||
|
\ 'prompt': ['fg', 'Conditional'],
|
||||||
|
\ 'pointer': ['fg', 'Exception'],
|
||||||
|
\ 'marker': ['fg', 'Keyword'],
|
||||||
|
\ 'spinner': ['fg', 'Label'],
|
||||||
|
\ 'header': ['fg', 'Comment'] }
|
||||||
|
<
|
||||||
|
|
||||||
|
Command-local options~
|
||||||
|
|
||||||
|
>
|
||||||
" [Buffers] Jump to the existing window if possible
|
" [Buffers] Jump to the existing window if possible
|
||||||
let g:fzf_buffers_jump = 1
|
let g:fzf_buffers_jump = 1
|
||||||
|
|
||||||
" [[B]Commits] to customize the options used by 'git log':
|
" [[B]Commits] to customize the options used by 'git log':
|
||||||
let g:fzf_commits_log_options = \
|
let g:fzf_commits_log_options = \
|
||||||
'--graph --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr"'
|
'--graph --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr"'
|
||||||
|
<
|
||||||
|
|
||||||
|
Advanced customization using autoload functions~
|
||||||
|
|
||||||
|
You can use autoload functions to define your own commands.
|
||||||
|
>
|
||||||
" Advanced customization using autoload functions
|
" Advanced customization using autoload functions
|
||||||
autocmd VimEnter * command! Colors
|
autocmd VimEnter * command! Colors
|
||||||
\ call fzf#vim#colors({'left': '15%', 'options': '--reverse --margin 30%,0'})
|
\ call fzf#vim#colors({'left': '15%', 'options': '--reverse --margin 30%,0'})
|
||||||
<
|
<
|
||||||
|
|
||||||
|
|
||||||
MAPPINGS *fzf-vim-mappings*
|
MAPPINGS *fzf-vim-mappings*
|
||||||
==============================================================================
|
==============================================================================
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user