欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Python編寫vim插件的簡單示例

 更新時(shí)間:2015年04月17日 17:28:49   投稿:goldensun  
這篇文章主要介紹了使用Python編寫vim插件的簡單教程,文中舉了一個(gè)獲取reddit首頁信息并顯示在緩沖區(qū)中的例子,需要的朋友可以參考下

 Vim 插件是一個(gè) .vim 的腳本文件,定義了函數(shù)、映射、語法規(guī)則和命令,可用于操作窗口、緩沖以及行。一般一個(gè)插件包含了命令定義和事件鉤子。當(dāng)使用 Python 編寫 vim 插件時(shí),函數(shù)外面是使用 VimL 編寫,盡管 VimL 學(xué)起來很快,但 Python 更加靈活,例如可以用 urllib/httplib/simplejson 來訪問某些 Web 服務(wù),這也是為什么很多需要訪問 Web 服務(wù)的插件都是使用 VimL + Python 編寫的原因。


在開始編寫插件之前,你需要確認(rèn) Vim 支持 Python,通過以下命令來判別:
 

復(fù)制代碼 代碼如下:
vim --version | grep +python


接下來我們通過一個(gè)簡單的例子來學(xué)習(xí)用 Python 編寫 Vim 插件,該插件用來獲取 Reddit 首頁信息并顯示在當(dāng)前緩沖區(qū)上。

首先在 Vim 新建 vimmit.vim 文件,我們首先需要判斷是否支持 Python,如果不支持給出提示信息:
 

if !has('python')
  echo "Error: Required vim compiled with +python"
  finish
endif

上面這段代碼就是用 VimL 編寫的,它將檢查 Vim 是否支持 Python。


下面是用 Python 編寫的 Reddit() 主函數(shù):

 

" Vim comments start with a double quote.
" Function definition is VimL. We can mix VimL and Python in
" function definition.
function! Reddit()
 
" We start the python code like the next line.
 
python << EOF
# the vim module contains everything we need to interface with vim from
# python. We need urllib2 for the web service consumer.
import vim, urllib2
# we need json for parsing the response
import json
 
# we define a timeout that we'll use in the API call. We don't want
# users to wait much.
TIMEOUT = 20
URL = "http://reddit.com/.json"
 
try:
  # Get the posts and parse the json response
  response = urllib2.urlopen(URL, None, TIMEOUT).read()
  json_response = json.loads(response)
 
  posts = json_response.get("data", "").get("children", "")
 
  # vim.current.buffer is the current buffer. It's list-like object.
  # each line is an item in the list. We can loop through them delete
  # them, alter them etc.
  # Here we delete all lines in the current buffer
  del vim.current.buffer[:]
 
  # Here we append some lines above. Aesthetics.
  vim.current.buffer[0] = 80*"-"
 
  for post in posts:
    # In the next few lines, we get the post details
    post_data = post.get("data", {})
    up = post_data.get("ups", 0)
    down = post_data.get("downs", 0)
    title = post_data.get("title", "NO TITLE").encode("utf-8")
    score = post_data.get("score", 0)
    permalink = post_data.get("permalink").encode("utf-8")
    url = post_data.get("url").encode("utf-8")
    comments = post_data.get("num_comments")
 
    # And here we append line by line to the buffer.
    # First the upvotes
    vim.current.buffer.append("↑ %s"%up)
    # Then the title and the url
    vim.current.buffer.append("  %s [%s]"%(title, url,))
    # Then the downvotes and number of comments
    vim.current.buffer.append("↓ %s  | comments: %s [%s]"%(down, comments, permalink,))
    # And last we append some "-" for visual appeal.
    vim.current.buffer.append(80*"-")
 
except Exception, e:
  print e
 
EOF
" Here the python code is closed. We can continue writing VimL or python again.
endfunction

使用如下命令保存文件
 

復(fù)制代碼 代碼如下:
:source vimmit.vim

然后調(diào)用該插件:
 

復(fù)制代碼 代碼如下:
:call Reddit()

這個(gè)命令用起來不那么方便,因此我們再定義一個(gè)命令:

復(fù)制代碼 代碼如下:
command! -nargs=0 Reddit call Reddit()

我們定義了命令:Reddit來調(diào)用這個(gè)函數(shù)。-nargs 參數(shù)聲明命令行中有多少個(gè)參數(shù)。


關(guān)于函數(shù)參數(shù)的問題:

問:如何訪問函數(shù)中的參數(shù)?
 

function! SomeName(arg1, arg2, arg3)
  " Get the first argument by name in VimL
  let firstarg=a:arg1
 
  " Get the second argument by position in Viml
  let secondarg=a:1
 
  " Get the arguments in python
 
  python << EOF
  import vim
 
  first_argument = vim.eval("a:arg1") #or vim.eval("a:0")
  second_argument = vim.eval("a:arg2") #or vim.eval("a:1")

你可以使用 ... 來處理可變個(gè)數(shù)參數(shù)來替換特定的參數(shù)名,可通過位置或者命名參數(shù)來訪問,如:(arg1, arg2, ...)

問:如何在 Python 中調(diào)用 Vim 命令?
 

復(fù)制代碼 代碼如下:
vim.command("[vim-command-here]")

問:如何定義全局變量,并在 VimL 和 Python 中訪問?

全局變量使用形如 g:. 的前綴,定義全局變量前應(yīng)該檢查該變量是否已定義:
 

if !exists("g:reddit_apicall_timeout")
  let g:reddit_apicall_timeout=40
endif

然后你通過下面代碼在 Python 中訪問這個(gè)變量:
 

TIMEOUT = vim.eval("g:reddit_apicall_timeout")

可通過下面的方法來對(duì)全局變量進(jìn)行重新賦值:
 

let g:reddit_apicall_timeout=60

更多關(guān)于使用 Python 編寫 Vim 插件的說明請(qǐng)看官方文檔。


備注:

一旦你用過VimL,就會(huì)發(fā)現(xiàn)它挺簡單的,你用python寫的代碼也可以用它來實(shí)現(xiàn)。詳細(xì)請(qǐng)參考vim python模塊文檔,這是一份重要的參考資料。

除了上述文檔,你也可以在IBM developerWorks網(wǎng)站找到一些有用的資料。

相關(guān)文章

  • 關(guān)于Python字典(Dictionary)操作詳解

    關(guān)于Python字典(Dictionary)操作詳解

    這篇文章主要介紹了關(guān)于Python字典(Dictionary)操作詳解,Python字典是另一種可變?nèi)萜髂P?,且可存?chǔ)任意類型對(duì)象,如字符串、數(shù)字、元組等其他容器模型,需要的朋友可以參考下
    2023-04-04
  • Python 使用 attrs 和 cattrs 實(shí)現(xiàn)面向?qū)ο缶幊痰膶?shí)踐

    Python 使用 attrs 和 cattrs 實(shí)現(xiàn)面向?qū)ο缶幊痰膶?shí)踐

    這篇文章主要介紹了Python 使用 attrs 和 cattrs 實(shí)現(xiàn)面向?qū)ο缶幊痰膶?shí)踐,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-06-06
  • 跟老齊學(xué)Python之重回函數(shù)

    跟老齊學(xué)Python之重回函數(shù)

    在本教程的開始部分,就已經(jīng)引入了函數(shù)的概念:《永遠(yuǎn)強(qiáng)大的函數(shù)》,之所以那時(shí)候就提到函數(shù),是因?yàn)槲矣X得函數(shù)之重要,遠(yuǎn)遠(yuǎn)超過一般。這里,重回函數(shù),一是復(fù)習(xí),二是要在已經(jīng)學(xué)習(xí)的基礎(chǔ)上,對(duì)函數(shù)有更深刻的理解。
    2014-10-10
  • Python隨機(jī)生成手機(jī)號(hào)、數(shù)字的方法詳解

    Python隨機(jī)生成手機(jī)號(hào)、數(shù)字的方法詳解

    這篇文章主要介紹了Python隨機(jī)生成手機(jī)號(hào)、數(shù)字的方法,結(jié)合完整實(shí)例形式分析了Python編程生成隨機(jī)手機(jī)號(hào)與數(shù)字的實(shí)現(xiàn)方法及相關(guān)函數(shù)用法,需要的朋友可以參考下
    2017-07-07
  • 跟老齊學(xué)Python之print詳解

    跟老齊學(xué)Python之print詳解

    print的一些基本用法,在前面的講述中也涉及一些,本講是在復(fù)習(xí)的基礎(chǔ)上,盡量再多點(diǎn)內(nèi)容。
    2014-09-09
  • Python+django實(shí)現(xiàn)簡單的文件上傳

    Python+django實(shí)現(xiàn)簡單的文件上傳

    這篇文章主要為大家詳細(xì)介紹了Python+django實(shí)現(xiàn)簡單的文件上傳的相關(guān)代碼,感興趣的小伙伴們可以參考一下
    2016-08-08
  • python opencv 找出圖像中的最大輪廓并填充(生成mask)

    python opencv 找出圖像中的最大輪廓并填充(生成mask)

    這篇文章主要介紹了python opencv 找出圖像中的最大輪廓并填充(生成mask),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • python pandas消除空值和空格以及 Nan數(shù)據(jù)替換方法

    python pandas消除空值和空格以及 Nan數(shù)據(jù)替換方法

    今天小編就為大家分享一篇python pandas消除空值和空格以及 Nan數(shù)據(jù)替換方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • pandas loc iloc ix用法詳細(xì)分析

    pandas loc iloc ix用法詳細(xì)分析

    pandas處理數(shù)據(jù)時(shí),我們會(huì)經(jīng)常看到dataframe結(jié)構(gòu)使用loc, iloc, ix等方法,那么這些方法到底有啥區(qū)別,下面我們來進(jìn)行詳細(xì)分析,感興趣的朋友跟隨小編一起看看吧
    2023-01-01
  • Python中的閉包實(shí)例詳解

    Python中的閉包實(shí)例詳解

    這篇文章主要介紹了Python中的閉包,針對(duì)閉包的定義、用法及注意事項(xiàng)進(jìn)行了實(shí)例講解,有助于讀者深入理解閉包的概念及用法,需要的朋友可以參考下
    2014-08-08

最新評(píng)論