我想将此map转换为command.我尝试过"明显的",将行的命令部分复制map到command一行:
command SortWords d:execute 'normal i' . join(sort(split(getreg('"'))), ' ')<CR>
Run Code Online (Sandbox Code Playgroud)
但是,当使用选定文本时,它只会以" E481: No range allowed" 失败.我的Google-fu不够强大,而且手册是...计算机可解析的,让我们说.
command! -nargs=0 -range SortWords exe 'norm! gvd'|call setreg('"', join(sort(split(@")), ' '), visualmode()[0])|norm! P
Run Code Online (Sandbox Code Playgroud)
这个命令很脏,因为它破坏了未命名的寄存器.
为避免这种情况,您必须保存寄存器并在完成后将其恢复.最好的方法是使用一个函数.
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
Run Code Online (Sandbox Code Playgroud)