2014-08-28 59 views
0

我寫一個函數用於刪除選定的文本(以一種特殊的方式)在Vim在SSH會話中運行:函數適用於選擇在vim

python << EOF 
def delSelection(): 
    buf = vim.current.buffer 
    (lnum1, col1) = buf.mark('<') 
    (lnum2, col2) = buf.mark('>') 

    # get selected text 
    # lines = vim.eval('getline({}, {})'.format(lnum1, lnum2)) 
    # lines[0] = lines[0][col1:] 
    # lines[-1] = lines[-1][:col2+1] 
    # selected = "\n".join(lines) + "\n" 
    # passStrNc(selected) 

    # delete selected text 
    lnum1 -= 1 
    lnum2 -= 1 
    firstSeletedLine = buf[lnum1] 
    firstSeletedLineNew = buf[lnum1][:col1] 
    lastSelectedLine = buf[lnum2] 
    lastSelectedLineNew = buf[lnum2][(col2 + 1):] 
    newBuf = ["=" for i in range(lnum2 - lnum1 + 1)] 
    newBuf[0] = firstSeletedLineNew 
    newBuf[-1] = lastSelectedLineNew 
    print(len(newBuf)) 
    print(len(buf[lnum1:(lnum2 + 1)])) 
    buf[lnum1:(lnum2 + 1)] = newBuf 

EOF 


function! DelSelection() 
python << EOF 
delSelection() 
EOF 
endfunction 

python << EOF 
import os 
sshTty = os.getenv("SSH_TTY") 
if sshTty: 
    cmd6 = "vnoremap d :call DelSelection()<cr>" 
    vim.command(cmd6) 
EOF 

顯然VIM呼籲每一個功能所選擇的行,這打破了功能的整個目的。我應該如何正確地做到這一點?

回答

1

這是因爲:視覺模式發行時自動插入'<,'>範圍。要明確指出,規範的方法是通過預先<C-u>的映射:

cmd6 = "vnoremap d :<C-u>call DelSelection()<cr>" 

另外,您還可以追加range關鍵字到:function定義,CP。 :help a:firstline

0

好的,我明白了。我只需要調用該函數前添加Esc鍵:

python << EOF 
import os 
sshTty = os.getenv("SSH_TTY") 
if sshTty: 
    cmd6 = "vnoremap d <esc>:call DelSelection()<cr>" 
    vim.command(cmd6) 
EOF 
+1

這樣做的標準方法是:: ...。 – FDinoff 2014-08-28 21:33:51