Here is some text
here is line two of text我在Vim中直观地从is选择到is:(括号表示视觉选择[ ])
Here [is some text
here is] line two of text使用Python,我可以获得所选内容的范围元组:
function! GetRange()
python << EOF
import vim
buf = vim.current.buffer # the buffer
start = buf.mark('<') # start selection tuple: (1,5)
end = buf.mark('>') # end selection tuple: (2,7)
EOF
endfunction我提供了这个文件::so %,可视化地选择文本,运行:<,'>call GetRange()和
现在我有了(1,5)和(2,7)。在Python中,如何编译以下字符串:
is some text\nhere is
很高兴:
发布于 2013-08-11 02:09:32
试试这个:
fun! GetRange()
python << EOF
import vim
buf = vim.current.buffer
(lnum1, col1) = buf.mark('<')
(lnum2, col2) = buf.mark('>')
lines = vim.eval('getline({}, {})'.format(lnum1, lnum2))
lines[0] = lines[0][col1:]
lines[-1] = lines[-1][:col2]
print "\n".join(lines)
EOF
endfun您可以使用vim.eval获取vim函数和变量的python值。
发布于 2013-08-10 20:46:17
如果您使用纯vimscript,这可能会奏效。
function! GetRange()
let @" = substitute(@", '\n', '\\n', 'g')
endfunction
vnoremap ,r y:call GetRange()<CR>gvp这将将可视选择中的所有换行符转换为\n,并将选择替换为该字符串。
此映射将所选内容插入"寄存器。调用该函数(因为它只有一个命令,所以并不是真正必要的)。然后使用gv重新选择视觉选择,然后将引号寄存器粘贴回选定区域。
注意:在vimscript中,所有用户定义的函数都必须以大写字母开头。
发布于 2015-01-14 06:08:39
这是康纳回答的另一个版本。我采纳了qed的建议,并在选择完全在一行的情况下添加了一个补丁。
import vim
def GetRange():
buf = vim.current.buffer
(lnum1, col1) = buf.mark('<')
(lnum2, col2) = buf.mark('>')
lines = vim.eval('getline({}, {})'.format(lnum1, lnum2))
if len(lines) == 1:
lines[0] = lines[0][col1:col2 + 1]
else:
lines[0] = lines[0][col1:]
lines[-1] = lines[-1][:col2 + 1]
return "\n".join(lines)https://stackoverflow.com/questions/18165973
复制相似问题