类似 less 的寻呼程序,能够重复前 N 行

类似 less 的寻呼程序,能够重复前 N 行

有没有办法让less程序在每个显示的页面上重复第一行(或前两行)?

还有其他寻呼程序可以做到这一点吗?

这将是数据库表浏览的杀手级应用程序,想想mysqlpsqlgqlplus......

请参阅底部的屏幕截图这一页。我想重复标题行+水平ascii栏。

答案1

有一个使用 Vim 的解决方案。

首先,您需要一个 Vim 宏,它将完成大部分工作。将其保存在~/.vim/plugin/less.vim

" :Less
" turn vim into a pager for psql aligned results 
fun! Less()
  set nocompatible
  set nowrap
  set scrollopt=hor
  set scrollbind
  set number
  execute 'above split'
  " resize upper window to one line; two lines are not needed because vim adds separating line
  execute 'resize 1'
  " switch to lower window and scroll 2 lines down 
  wincmd j
  execute 'norm! 2^E'
  " hide statusline in lower window
  set laststatus=0
  " hide contents of upper statusline. editor note: do not remove trailing spaces in next line!
  set statusline=\  
  " arrows do scrolling instead of moving
  nmap ^[OC zL
  nmap ^[OB ^E
  nmap ^[OD zH
  nmap ^[OA ^Y
  nmap <Space> <PageDown>
  " faster quit (I tend to forget about the upper panel)
  nmap q :qa^M
  nmap Q :qa^M
endfun
command! -nargs=0 Less call Less()

其次,要模拟寻呼机,您需要调用 vim,以便它:

  • 读取标准输入
  • 但如果在命令行上给出参数,请阅读那里的任何内容
  • 以只读模式工作
  • 跳过所有初始化脚本,而是执行上面定义的 Less 宏

我将其作为辅助脚本放在一起~/bin/vimpager

#!/bin/bash
what=-
test "$@" && what="$@"
exec vim -u NONE -R -S ~/.vim/plugin/less.vim -c Less $what

使用 使脚本可执行chmod +x ~/bin/vimpager

第三,您需要重写 psql 的分页程序。不要PAGER全局设置变量,因为它会影响其他程序,而不仅仅是 psql。相反,将其添加到您的~/.psqlrc文件中:

\setenv PAGER ~/bin/vimpager

!重新加载您的个人资料后,您可以享受结果,其行为应符合预期(箭头键垂直和水平浏览),如下所示:维姆佩尔在行动。另外,如果您需要的话,Vim 的所有功能都在身边。

答案2

你有没有尝试过SQL模式在 Emacs/XEmacs 中?

它的使用当然不如moreor那么简单less,但它满足您的要求,在垂直和水平滚动结果时留下标题行。

答案3

这很大程度上借用了接受的答案,但补充说...

  • 滚动速度更快
  • 不会意外滚动到标题中
  • 语法突出显示(一些信用属于这里
    • 正/负数、日期、时间、NULL真/假(以及 T/F、Y/N、是/否)
    • 行号(如果管道字符之前有行号)。
  • 帮助文本
  • 支持附带的 Vim适用于 Windows 的 Git
  • 如果标准输入缓冲区发生变化,不要威胁要更新视图

某些部分可能需要根据您的特定输出进行调整,因为我不使用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 https://unix.stackexchange.com/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()

答案4

您可以在“前进”之前添加一个数字,它将滚动 N 行,而不是完整长度。因此,如果您的终端窗口有 40 行,请键入38f开始仅滚动 38 行,留下最后一个“页面”的最后 2 行。从联机帮助页:

   SPACE or ^V or f or ^F
          Scroll forward N  lines,  default  one  window  (see  option  -z
          below).   If  N  is  more  than  the screen size, only the final
          screenful is displayed.  Warning: some systems use ^V as a  spe‐
          cial literalization character.

   z      Like  SPACE,  but  if  N is specified, it becomes the new window
          size.

   b or ^B or ESC-v
          Scroll backward N lines,  default  one  window  (see  option  -z
          below).   If  N  is  more  than  the screen size, only the final
          screenful is displayed.

相关内容