我有我想要的选择的开始和结束(行/列)对。
如何将其设置为实际的视觉选择?
前任:
我有缓冲区:
1 2
3 4
5 6
Run Code Online (Sandbox Code Playgroud)
开始:2,1
结束:3,2
:cal SetSelection()
Run Code Online (Sandbox Code Playgroud)
通话后所需的视觉选择:
3 4
5 6
Run Code Online (Sandbox Code Playgroud)
“SetSelection()”是什么样的?
编辑
迄今为止最好的解决方案,根据答案进行错误检查
"put user in visual mode and set the visual selection
"if arguments are not valid, nothing is changed, and raises an exception
":param 1:
"Visual mode to leave user in.
"must be either one of:
"- 'v' for visual (default)
"- "\<s-v>" for line visual
"- "\<c-v>" for block visual
":type 1: string
":returns: 0
":raises: bad mode argument, bad position argument
fu! SetSelection( x, y, x2, y2, ... )
let valid_mode_strings = ["v","\<s-v>","\<c-v>"]
if a:0 > 0
if index( valid_mode_strings, a:1 ) >= 0
let mode = a:1
el
th 'bad mode argument: ' . a:1 . ' valid options: ' . join( valid_mode_strings, ', ' )
en
el
let mode = 'v'
en
let oldpos = getpos('.')
if setpos( '.', [0,a:x,a:y] ) != 0
exe "norm! \<esc>"
th 'bad position argument: ' . a:x . ' ' . a:y . ' ' . a:x2 . ' ' . a:y2
en
exe 'norm! ' . mode
if setpos( '.', [0,a:x2,a:y2] ) != 0
exe "norm! \<esc>"
cal setpos( '.', oldpos )
th 'bad position argument: ' . a:x . ' ' . a:y . ' ' . a:x2 . ' ' . a:y2
en
endf
Run Code Online (Sandbox Code Playgroud)
你可以使用函数setpos()来完成这项工作:
fun! SetSelection()
call setpos('.',[0,2,1])
normal! v
call setpos('.',[0,3,2])
endf
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,[2,1]和[3,2]应该作为传入的参数。我只是硬编码以使其简单。
还有一些错误处理留给了你。例如参数验证等。