程序员人生 网站导航

在VIM中使用GOOGLE进行搜索或者翻译

栏目:综合技术时间:2014-06-03 21:48:53

一个在VIM中直接调用浏览器搜索或翻译当前光标下单词的一段代码。VIM必须内建PYTHON.
放入.vimrc 文件中即可

代码片段:

python << EOM
#coding = utf-8
def google_it(word):
import re
import webbrowser
if not word or word.isspace():
print 'there is no word under the cursor'
else:
try:
url = 'http://www.google.com/search?q='+word
webbrowser.open(url)
except:
print 'cannot access google!'
def google_translate_it(word):
import re
import webbrowser
if not word or word.isspace():
print 'there is no word under the cursor!'
else:
try:
url = 'http://translate.google.cn/#en|zh-CN|'+word+'%0A'
webbrowser.open(url)
except:
print 'cannot access google!'
EOM

function! Google()
python << EOM
#coding = utf-8
import vim
py_word = vim.eval("expand("<cword>")")
print py_word
google_it(py_word)
EOM
endfunction

function! GoogleTranslate()
python << EOM
#coding = utf-8
import vim
py_word = vim.eval("expand("<cword>")")
print py_word
google_translate_it(py_word)
EOM
endfunction

command GOOGLE :call Google()
command GOOGLETRANSLATE :call GoogleTranslate()

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐