0

目的の選択の開始と終了 (行/列) のペアがあります。

それを実際の視覚的な選択として設定する方法は?

元:

私はバッファを持っています:

 1 2
 3 4
 5 6

開始: 2,1

終了: 3,2

:cal SetSelection()

通話後の希望のビジュアル選択:

3 4
5 6

「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
4

2 に答える 2

4

関数setpos()を使用して仕事をすることができます:

fun! SetSelection()
    call setpos('.',[0,2,1])
    normal! v
    call setpos('.',[0,3,2])
endf

上記のコードでは、[2,1]and[3,2]は渡された引数と同じである必要があります。単純にするためにハードコーディングしただけです。

また、いくつかのエラー処理はあなたに任されていました。たとえば、引数の検証など。

于 2013-04-23T22:40:27.917 に答える
3

ほとんどの vim には、まさにこの目的のためにm<とマーカーがあります。m>:help m<

call setpos("'<", [0, 2, 1])
call setpos("'>", [0, 3, 2])
normal! gv

一部のvimインストールはそうではありません。そのような場合、ユーザー マークを保存して復元するか、コンテキスト マークを使用できます。`

call cursor([2, 1])
normal! m`
call cursor([3, 2])
normal! v``
于 2013-04-24T00:45:46.317 に答える