Answers:
我不记得我最初从哪里得到的,但是它已经存在于我的〜/ .vimrc中至少有几年了:
" Swap the word the cursor is on with the next word (which can be on a
" newline, and punctuation is "skipped"):
nmap <silent> gw "_yiw:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><C-o>:noh<CR>
定义好之后,您所需要做的就是在正常模式下将光标放在“ param1”上,然后键入: gw
+1表示@Heptite的答案。
为了更完整,这是我的.vimrc文件中的内容:
" push current line up or down
nnoremap <leader><Up> ddkP
nnoremap <leader><Down> ddp
" exchange character under cursor with the next character without moving the cursor
nnoremap gc xph
" exchange word under cursor with the next word without moving the cursor
nnoremap gw "_yiw:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><C-o><C-l>
" push word under cursor to the left
nnoremap <leader><Left> "_yiw?\w\+\_W\+\%#<CR>:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><C-o><C-l>
" push word under cursor to the right
nnoremap <leader><Right> "_yiw:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><C-o>/\w\+\_W\+<CR><C-l>
资料来源:vim Wiki。
我看到我的(和Wiki的)gw
与Heptite的略有不同。我不确定哪个更好。
我使用vim-exchange,Repeatable(取决于repeat.vim)和argtextobj的组合编写了可重复的映射。
" Swap function arguments, move the argument under the cursor to the left or to
" the right.
Repeatable map <leader>al cxiaf,cxia
Repeatable map <leader>ah cxiaF,hcxia
在这些映射中使用交换插件和可重复插件的优点是:
u
将撤消交换(它们是原子更改).
继续向左/向右移动参数。我知道,对于一个简单的操作来说,似乎有很多插件,但是请考虑一下这些插件还能为您带来什么:
ia
和aa
textobj,用于删除(dia
和daa
)和拉动(yia
)。.
。Vim Wiki上的交换映射将无法正确处理带有重音符号的单词。
这些映射适用于使用(欧洲)ISO / IEC_8859-1 Latin-1补码字符。这可以通过替换\w
with的[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-]
所有实例和\_W
with的所有实例来完成\_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-]
。
此外,在需要的地方清除了搜索突出显示的内容。这是通过:nohlsearch<return>
在需要的每个映射的末尾添加来实现的。
这是最终结果:
" Use gc to swap the current CHARACTER with the next, WITHOUT changing the cursor position.
nnoremap <silent> gc xph
" Use gw to swap the current WORD with the next, WITHOUT changing the cursor position.
nnoremap <silent> gw "_yiw:s/\(\%#[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\(\_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\([0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)/\3\2\1/<CR><c-o><c-l>:nohlsearch<return>
" Disable Alt+[menukey] menu keys (i.e. Alt+h for help)
set winaltkeys=no
" Use Alt + ← or Alt + h to swap the current WORD with the previous, keeping the cursor on the current word. This feels like "PUSHING" the word to the left.
nnoremap <silent> <A-Left> "_yiw?[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-]\+\%#<CR>:s/\(\%#[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\(\_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\([0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)/\3\2\1/<CR><c-o><c-l>:nohlsearch<return>
nnoremap <silent> <A-h> "_yiw?[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-]\+\%#<CR>:s/\(\%#[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\(\_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\([0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)/\3\2\1/<CR><c-o><c-l>:nohlsearch<return>
" <A-h> corresponds to è
" Use Alt + → or Alt + l to swap the current WORD with the next, keeping the cursor on the current word. This feels like "PUSHING" the word to the right.
nnoremap <silent> <A-Right> "_yiw:s/\(\%#[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\(\_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\([0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)/\3\2\1/<CR><c-o>/[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+<CR><c-l>:nohlsearch<return>
nnoremap <silent> <A-l> "_yiw:s/\(\%#[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\(\_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)\([0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\)/\3\2\1/<CR><c-o>/[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+\_[^0-9A-Za-zÀ-ÖØ-öø-ÿ_\-\`]\+<CR><c-l>:nohlsearch<return>
" <A-l> corresponds to ì
" Use g{ to swap the current PARAGRAPH with the next.
nnoremap g{ {dap}p{
Eclim插件提供了一个很好的插件。所有学分:)
:SwapWords
..如果您不想安装整个插件,请提取以下功能:
" Swap words:
" taken from Eclim
" https://github.com/ervandew/eclim
function! SwapWords() " {{{
" Initially based on http://www.vim.org/tips/tip.php?tip_id=329
" save the last search pattern
let save_search = @/
normal! "_yiw
let pos = getpos('.')
keepjumps s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/
call setpos('.', pos)
" restore the last search pattern
let @/ = save_search
silent! call repeat#set(":call SwapWords()\<cr>", v:count)
endfunction " }}}
command! SwapWords :call SwapWords()