Vim 如何针对逐块视觉模式选择运行命令?
我有一个包含以下内容的测试文件:Vim 如何针对逐块视觉模式选择运行命令?,vim,Vim,我有一个包含以下内容的测试文件: var a = f ff fff 然后我将光标移动到第1行的f字符上,ctrl+v选择了它下面的两个f(■ 表示选择) 我想将文本更改为: var a = "f" "ff" "fff" 所以我执行了这个命令: :正常i“ctrl+vEscA” 但是引号被添加到了整行中。是否可以仅对块式视觉选择(而不是整行)执行操作?请注意,此示例仅用于讨论Vim技能。我不是想解决任何问题,只是想学习Vim技能
var a = f
ff
fff
然后我将光标移动到第1行的f
字符上,ctrl+v选择了它下面的两个f
(■ 表示选择)
我想将文本更改为:
var a = "f"
"ff"
"fff"
所以我执行了这个命令:
:正常i“
ctrl+vEscA”
但是引号被添加到了整行中。是否可以仅对块式视觉选择(而不是整行)执行操作?请注意,此示例仅用于讨论Vim技能。我不是想解决任何问题,只是想学习Vim技能 关于可能的解决方案:
:'<,'>norm $ciw"ctrl-v ctrl-r""
:'<,'>s/\w\+$/"&"
:”问题是所有的:
-命令只能采用行范围。回想一下,在视觉模式下按:
后,您得到了:'B!典型的我“^CA”
注意:^C
代表Ctrl-VCtrl-C谢谢Matt!它工作得很好。顺便问一句,function和。。。endf
和函数。。。endfunction
?再次感谢您的澄清!
:'<,'>s/\w\+$/"&"
" apply command to a visual selection
" a:cmd - any "range" command
" a:mods - :h command-modifiers
" a:trim - true if we need to trim trailing spaces from selection text
function s:block(cmd, mods, trim) abort
" save last line no
let l:last = line('$')
" yank selected text into the register "9"
silent normal! gv"9y
" put it at buffer's end
call append(l:last, getreg(9, 1, 1))
" trim trailing spaces
if a:trim
execute 'keepj keepp' l:last..'+,$s/\s\+$//e'
endif
" apply our command
" note: we assume that the command never enters Visual mode
execute a:mods l:last..'+,$' a:cmd
" get the changed text back into the register "9"
call setreg(9, getline(l:last + 1, '$'), visualmode())
" clean up
call deletebufline('%', l:last + 1, '$')
" replace the selection
" note: the old text goes into the register "1", "1" into "2", and so on.
" old register "9" is lost anyway, see :h v_p
silent normal! gv"9p
endfunction
" our interface is a user command
command! -bang -range -nargs=1 -complete=command B call s:block(<q-args>, <q-mods>, <bang>0)
:'<,'>B! normal! i"^CA"