如何轻松地切换单词(在VIM中)?


12

我对vim不够好,无法确定这是否可能(这就是为什么我成为超级用户而不是好用户)〜vim中是否有一种方法可以轻松切换两个单词?

例如,def function(param1, param2)有没有一种快速/简便的方法可以将其更改为def function(param2, param1)????

Answers:


13

我不记得我最初从哪里得到的,但是它已经存在于我的〜/ .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


4
我也有,它来自vim Wiki。
romainl 2011年

5

+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的略有不同。我不确定哪个更好。


4

这个长期的解决方案很难看。假设您的光标在第一个单词的第一个字母的左侧,即“ p”。这样做:dwlpldw%p。这适合您的特殊情况。如何处理日常编辑?尝试dwwPdWWP。:D

提示:如果您不需要经常这样做,则不要总是写长的正则表达式。否则,您的vimrc繁荣。所有vim用户都应熟悉其内置的光标移动。


1

我使用vim-exchangeRepeatable(取决于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将撤消交换(它们是原子更改)
  • 您可以使用.继续向左/向右移动参数。

我知道,对于一个简单的操作来说,似乎有很多插件,但是请考虑一下这些插件还能为您带来什么:

  • argtextobj为您提供iaaatextobj,用于删除(diadaa)和拉动(yia)。
  • vim-repeat和Repeatable用于使您的任何映射都可重复.
  • vim-exchange使您可以重复,原子地交换文本。

1

拉丁语言的交换映射

Vim Wiki上的交换映射将无法正确处理带有重音符号的单词。

这些映射适用于使用(欧洲)ISO / IEC_8859-1 Latin-1补码字符。这可以通过替换\wwith的[0-9A-Za-zÀ-ÖØ-öø-ÿ_\-]所有实例和\_Wwith的所有实例来完成\_[^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{

0

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()
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.