如何在Vim中交换包含特殊标点符号的单词?

8ehkhllq  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(140)

我有很多类似"Edit the filemy-file.conf"的字符串,我想把它们改成"Edit themy-file.conffile"。注意文件名是用反引号而不是单引号(我不知道如何在这个编辑器中说明)。我试过很多替代方法,但似乎总是被反引号卡住。有人能帮我吗?谢谢。

sqougxex

sqougxex1#

我一直想创建一些干净的函数来实现这一点(也许其他人会提供它们),但现在我在vimrc中有以下(hacky)重Map:

" use [w and ]w and [W and ]W to exchange a word/WORD under the cursor with
" the prev/next one
nnoremap ]w mx$ox<ESC>kJ`xdawhelphmx$"_daw`xh
nnoremap ]W mx$ox<ESC>kJ`xdaWElphmx$"_daw`xh
nnoremap [w mx$ox<ESC>kJ`xdawbPhmx$"_daw`xh
nnoremap [W mx$ox<ESC>kJ`xdaWBPhmx$"_daw`xh

使用tpope的vim-repeat使它们可重复

" apply the repeat plugin to any mapping
function! Repeat(mapname, map, command)
    execute 'nnoremap <silent> <Plug>'.a:mapname.' '.a:command.
                \' :call repeat#set("\<Plug>'.a:mapname.'")<CR>'
    execute 'nmap '.a:map.' <Plug>'.a:mapname
endfunction

" use [w and ]w and [W and ]W to exchange a word/WORD under the cursor with
" the prev/next one
call Repeat('WordForward', ']w', 'mx$ox<ESC>kJ`xdawhelphmx$"_daw`xh')
call Repeat('WORDForward', ']W', 'mx$ox<ESC>kJ`xdaWElphmx$"_daw`xh')
call Repeat('WordBackward', '[w', 'mx$ox<ESC>kJ`xdawbPhmx$"_daw`xh')
call Repeat('WORDBackward', '[W', 'mx$ox<ESC>kJ`xdaWBPhmx$"_daw`xh')

相关问题