这是从公认的答案中大量借用的,但是增加了...
- 快速滚动
- 不能意外滚动到标题
- 语法突出显示(一些功劳归于此)
- 正数/负数,日期,时间,
NULL
真/假(以及T / F,Y / N,是/否)
- 行号(如果在管道字符前有行号)。
- 帮助文字
- 支持Windows版Git随附的Vim
- 如果stdin缓冲区发生更改,请不要威胁更新视图
由于我不使用,因此可能需要针对您的特定输出调整某些部分psql
。我的辅助功能也略有不同,但与接受的答案中的功能相似。
样品输入
| ID | First | Last | Member | Balance |
--+----+-----------+--------------+--------+---------+
1| 4 | Tom | Hanks | False | 0.00 |
2| 12 | Susan | Patterson | True | 10.00 |
3| 23 | Harriet | Langford-Wat | False | 0.00 |
4| 8 | Jerry | NULL | True | -382.94 |
[… More rows …]
10| 87 | Horace | Weaver | False | 47.52 |
码
" :HeadPager
" Turn vim into a pager with a header row
" Adapted from /unix//a/27840/143088
fun! HeadPager()
" If you didn't get three lines, shortcut out
if line('$') < 3
set nocompatible
nmap <silent> q :qa!<c-M>
nmap <silent> Q :qa!<c-M>
return
endif
set noswapfile
set nocompatible
set nowrap
set scrollopt=hor
set scrollbind
" Hide statusline in lower window
set laststatus=0
" Explain mapped chars in status line.
set statusline=\ \ \ Q\ to\ quit\.\ Arrows\ or\ mousewheel\ to\ scroll\.\ \(Vim\ commands\ work\,\ too\.\)
" Delete/copy header lines
silent execute '1,2d'
" Split screen with new buffer (opens at top)
execute 'new'
" Switch to upper split
wincmd k
" Paste the header over the blank line
execute 'norm! Vp'
" Header highlighting
syn match Pipe "|"
hi def Pipe ctermfg=blue
syn match Any /[^|]\+/
hi def Any ctermfg=yellow
" Switch back to lower split for scrolling
wincmd j
" Set lower split height to maximum
execute "norm! \<c-W>_"
" Syntax highlighting
syn cluster CellContents contains=None
syn match Pipe "|" contained nextgroup=@CellContents skipwhite
hi def Pipe ctermfg=blue
" Start with newline or |. End right before next | or EOL
syn region Cell start=/\v(^|\|)\s*/ end=/\v(\||$)\@=/ contains=LineNumber,Pipe
syn match NumPos /\v\+?\d+(,?\d{3})*\.?\d*\ze *(\||$)\@=/ contained
syn match NumNeg /\v-\d+(,?\d{3})*\.?\d*\ze *(\||$)\@=/ contained
syn match NumZero /\v[+-]?0+\.?0*\ze *(\||$)\@=/ contained
hi def NumPos ctermfg=cyan
hi def NumNeg ctermfg=red
hi def NumZero ctermfg=NONE
syn cluster CellContents add=NumPos,NumNeg,NumZero
syn match DateVal /\v\d{4}-\d{2}-\d{2}/ contained nextgroup=TimeVal skipwhite
syn match TimeVal /\v\d{1,2}:\d{2}(:\d{2})?(\.\d+)?(Z| ?\c[AP]M)?\ze *(\||$)\@=/ contained
hi def DateVal ctermfg=magenta
hi def TimeVal ctermfg=magenta
syn cluster CellContents add=DateVal,TimeVal
syn match TrueVal /\v\c(t(rue)?|y(es)?)\ze *(\||$)\@=/ contained
syn match FalseVal /\v\c(f(alse)?|no?)\ze *(\||$)\@=/ contained
hi def TrueVal ctermfg=green
hi def FalseVal ctermfg=red
syn match NullVal /\v\cnull?\ze *(\||$)\@=/ contained
hi def NullVal ctermbg=gray ctermfg=black
syn cluster CellContents add=TrueVal,FalseVal,NullVal
syn match LineNumber /^ *\d\+/ contained
hi def LineNumber ctermfg=yellow
" Arrows do scrolling instead of moving
nmap <silent> <Up> 3<c-Y>
nmap <silent> <Down> 3<c-E>
nmap <silent> <Left> zH
nmap <silent> <Right> zL
nmap <Space> <PageDown>
" Faster quit (I tend to forget about the upper panel)
nmap <silent> q :qa!<c-M>
nmap <silent> Q :qa!<c-M>
" Ignore external updates to the buffer
autocmd! FileChangedShell */fd/*
autocmd! FileChangedRO */fd/*
endfun
command! -nargs=0 HeadPager call HeadPager()