一行代码为vim增加自动翻译单词功能 使用有道词典

1. 在 ~/.bash_profile最后增加如下:

translate()
{
  word=$1
  [ "x$word" = "x" ] && return 1
  start=0;
  wget -q --dns-timeout 6 -O - "http://fanyi.youdao.com/openapi.do?keyfrom=youdao111&key=60638690&type=data&doctype=xml&version=1.1&q=$word" |\
  while read line;do if echo "$line" | grep "<explains>" >/dev/null; then start=1;fi; if test $start -eq 1 && echo "$line" | grep '<!\[CDATA\[' >/dev/null; then echo $line| sed -n 's/^.*CDATA\[\(.*\)\]].*$/\1/gp';fi; if echo $line | grep "</explains" >/dev/null; then break; fi;done
}


export -f  translate

2. 在~/.vimrc(没有就创建一下)增加:

nnoremap tr :let a=expand("<cword>")<Bar>exec '!translate ' .a<CR>


这样在普通模式下, 输入‘tr’两个字符 就会到有道在线词典查询当前光标处的单词 显示出来

猜你喜欢

转载自blog.csdn.net/wangxugangzy05/article/details/53338419