使用vim在man、tldr、cppman库间搜索帮助手册并打开

Wednesday, May 24, 2023
本文共875字
2分钟阅读时长
vim , man , shell , bash

⚠️本文是作者P3troL1er原创,首发于https://peterliuzhi.top/tricks/%E4%BD%BF%E7%94%A8vim%E5%9C%A8mantldrcppman%E5%BA%93%E9%97%B4%E6%90%9C%E7%B4%A2%E5%B8%AE%E5%8A%A9%E6%89%8B%E5%86%8C%E5%B9%B6%E6%89%93%E5%BC%80/。商业转载请联系作者获得授权,非商业转载请注明出处!

Technological progress has merely provided us with more efficient means for going backwards. — Aldous Huxley

这里我是写了一个bash脚本,只需要使用命令viman -e 函数/命令名就能自动在man/cppman间搜索。之所以写这个脚本最主要的动机是cppman里将cppman的数据导入man失败了并且不知道怎么修复…并且man自带的编辑器(文本浏览器?)不好用,并且还想集成tldr

项目目录见GitHub - PeterLiu-all/viman: 在命令行和vim中查看Linux manual,数据库包括man、cppman、tldr

注意,想要使用该脚本必须先下载cppman和tldr:

pip3 install cppman
sudo apt install -y tldr
tldr --update
# 如有需要,可以缓存cppman的离线数据库
# 笔者花了几个小时才下载完,而且可能是代码问题经常卡死,需要手动重启
cppman -c

接下来就是脚本了,使用方法(注意-e是必须的):

# 普通使用
viman -e 名称
# 显示帮助
viman -h
# 使用tldr
viman -t -e 名称

然后将下面的脚本保存为文件viman:

#!/bin/bash

echo "--Written By P3troL1er--"
echo "> Welcome to my blog: https://peterliuzhi.top"

cppman -v >> /dev/null 2>>/dev/null
if [ $? -ne 0 ];then
    echo "Can't find cppman!" 1>&2
    python3 -m pip install cppman
    if [ $? -ne 0 ];then
        echo "ERROR found when installing cppman!" 1>&2
        echo "used command to install: python3 -m pip install cppman" 1>&2
        echo "try to find out that if the command is not available on your system!(check if you have python3 and pip)" 1>&2
        exit 127
    fi
fi

tldr -v >> /dev/null 2>>/dev/null
if [ $? -ne 0 ];then
    echo "Can't find tldr!" 1>&2
    sudo apt install tldr -y
    if [ $? -ne 0 ];then
        echo "ERROR found when installing tldr!" 1>&2
        echo "used command to install: sudo apt install tldr -y" 1>&2
        echo "try to find out that if the command is not available on your system!(check if you are using debian-based system like Ubuntu)" 1>&2
        exit 127
    fi
fi


function help_guide(){
    echo "USAGE: $0 [OPTIONS] -e MAN_ENTRY"
    echo "HELP: $0 -h"
    echo "USE_TLDR: $0 -t -e MAN_ENTRY"
    echo "BY P3troL1er"
}

helper=0
NORMAL_MAN="man"
use_tldr=0
entry_name=""

while getopts "hte:" opt; do
  case $opt in
    h)
      helper=1
      ;;
    t)
      NORMAL_MAN="tldr"
      use_tldr=1
      ;;
    e)
      entry_name=$OPTARG
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      help_guide
      exit 1
      ;;
  esac
done

if [ $helper -eq 1 ]; then
    help_guide
    exit 0
fi

VIMAN_ARG=`mktemp /tmp/viman.temp.XXXXXX`
RETURN_MSG=`$NORMAL_MAN $entry_name >> $VIMAN_ARG`
VIMAN_EXITCODE=$?
if [ -n "$RETURN_MSG" ]; then
    VIMAN_EXITCODE=16
fi
if [ $VIMAN_EXITCODE -eq 0 ]; then
    if [ $use_tldr -eq 1 ]; then
        vim $VIMAN_ARG -c ":term ++curwin cat %"
    else
        vim $VIMAN_ARG -c "set nonumber"
    fi
else 
    echo "$NORMAL_MAN failed! using cppman..."
    RETURN_MSG=`cppman "$entry_name"`
    if [ -n "$RETURN_MSG" ];then
        echo "Not even in cppman!"
    fi
fi
rm $VIMAN_ARG
if [ $VIMAN_EXITCODE -ne 0 ];then
    echo "Coundn't find manual for $entry_name!"
fi
exit $VIMAN_EXITCODE

然后放到/usr/local/bin中并sudo chmod +x /usr/local/bin/viman后大功告成

如果想要在vim里面查看,可以使用命令:

tabnew | exec ":term ++curwin viman -e <entry_name>"

这个命令会新打开一个tab执行shell命令

然而这个命令实在是太长了,我们可以在~/.vimrc中写入:

function! VimanFunc(entry_name, ...)
    let args = join(a:000, ' ')
    tabnew | execute "term ++curwin viman -e " . a:entry_name . " " . args
endfunction

command! -nargs=+ Viman :call VimanFunc(<f-args>)
cnoreabbrev viman Viman

使用的时候只需要在vim中输入:viman <entry_name> ...即可

文内图片