我对vim不够好,无法确定这是否可行(这就是为什么我来到超级用户而不是好)~vim有没有办法轻松切换两个词?
例如,def function(param1, param2)是否有一种快速/简单的方法可以将其更改为def function(param2, param1)???
Hep*_*ite 14
我不记得我最初从哪里得到这个,但它已经在我的 ~/.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>
Run Code Online (Sandbox Code Playgroud)
定义好这个之后,你需要做的就是将光标放在正常模式下的“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>
Run Code Online (Sandbox Code Playgroud)
来源:vim 维基。
我看到我的(和维基的)gw与 Heptite 的略有不同。我不确定哪个更好。
小智 5
那个冗长的解决方案是丑陋的。假设您的光标位于第一个单词的第一个字母的左侧,即“p”。做这个:dwlpldw%p。这适合您的特殊情况。如何处理日常编辑?尝试dwwP,或dWWP。:D
提示:不要总是写长的正则表达式,如果你不需要经常这样做。否则你的 vimrc 会爆炸。所有 vim 用户都应该熟悉其内置的光标移动。
| 归档时间: |
|
| 查看次数: |
5922 次 |
| 最近记录: |