Answers:
以下宏适用于这两种情况:
qq " start recording in register q
$ " jump to the last character on the line, a ) or a }
v% " select from here to the opening ( or {, inclusive
loh " shrink the selection
c " remove selection and enter insert mode
<CR><CR><Up> " open the (){} and put the cursor in between
<C-r>" " insert the content of default register
<Esc> " go back to normal mode
:s/,/,\r/g<CR> " replace every , with itself followed by a newline
:'[,']norm == " format the whole thing
q " stop recording
打@q
了foobar(foo: "FOO", bar: "BAR")
获得:
foobar(
foo: "FOO",
bar: "BAR"
)
并在foobar = { foo: "FOO", bar: "BAR" }
获得:
foobar = {
foo: "FOO",
bar: "BAR"
}
编辑
虽然这个宏最有可能跨会话保存,但很容易覆盖它。幸运的是,很容易将其转换为映射并将其保存在您的~/.vimrc
:
nnoremap <F6> $v%lohc<CR><CR><Up><C-r>"<Esc>:s/,/,\r/g<CR>:'[,']norm ==<CR>
<Up>
在映射版本中使用了正确的,所以我不知道如何让这一个通过。好吧......
<CR><CR>k
应该是<CR><CR><Up>
,因为那时我们仍处于插入模式。最后一个代码块中的映射已纠正错误。