我想转换this map
到命令
。我尝试过“显而易见”,将 map
行的命令部分复制到 command
行:
command SortWords d:execute 'normal i' . join(sort(split(getreg('"'))), ' ')<CR>
但是,当将其与选定的文本一起使用时,它会失败并显示“E481:不允许范围
”。我的 Google-fu 不够强大,而且手册是......计算机可以解析的,比如说。
最佳答案
command! -nargs=0 -range SortWords exe 'norm! gvd'|call setreg('"', join(sort(split(@")), ' '), visualmode()[0])|norm! P
该命令是脏命令,因为它破坏了未命名的寄存器。
为了避免这种情况,您必须保存寄存器并在完成后恢复它。最好的方法是使用函数。
command! -nargs=0 -range SortWords call VisualSortWords()
function! VisualSortWords()
let rv = @"
let rt = getregtype('"')
try
norm! gvy
call setreg('"', join(sort(split(@")), ' '), visualmode()[0])
norm! `>pgvd
finally
call setreg('"', rv, rt)
endtry
endfunction
关于Vim:将 map 转换为命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7415644/