Choice of Mapping
I personnally like to use v_*
for searching the visually selected text, similar to what n_*
does in normal mode for the word under the cursor. Using v_*
for shifting a visual selection sideways is in conflict with vim-visualstar which I think could actually be the default behavior of vanilla vim.
Using viwdp
or with an additional movement between d
and p
here is good enough for me.
A typical related feature request is shifting lines up and down. Greg Hurrell has produced a screencast how to do this (youtube). However, he
does not recommend it because:
it encourages bad habits ... do not mash keys ... not the vim way
I agree and would say that repeat is an anti-pattern here.
If you want such behaviour and a direct key to press, consider also following plugins and their suggestions for mappings:
Plugins and their Suggested Mappings
vim-move
The default mappings use the Alt-key as prefix which does not work in all circumstances:
<A-k> Move current line/selection up
<A-j> Move current line/selection down
<A-h> Move current character/selection left
<A-l> Move current character/selection right
vim-textmanip
xmap <C-j> <Plug>(textmanip-move-down)
xmap <C-k> <Plug>(textmanip-move-up)
xmap <C-h> <Plug>(textmanip-move-left)
xmap <C-l> <Plug>(textmanip-move-right)
The plugin vim-tmux-navigator uses the same set in normal mode to switch windows. This makes them a little bit inconsistent between modes.
vim-schlepp
vmap <unique> <up> <Plug>SchleppUp
vmap <unique> <down> <Plug>SchleppDown
vmap <unique> <left> <Plug>SchleppLeft
vmap <unique> <right> <Plug>SchleppRight
Some may argue arrow keys are too far away from the homerow.
Related Plugins
If you want to shift list items, function arguments and similar, consider the plugins sideways.vim and vim-swap. Suggested mappings by the plugin author Andrew Radev are again:
nnoremap <c-h> :SidewaysLeft<cr>
nnoremap <c-l> :SidewaysRight<cr>
vim-swap defines following two which I think are a quite good choice:
nmap g< <Plug>(swap-prev)
nmap g> <Plug>(swap-next)
vim-exchange works differently by not shifting things around. I think this plugin fits better to the vim way. The mappings are
call s:create_map('n', 'cx', '<Plug>(Exchange)')
call s:create_map('x', 'X', '<Plug>(Exchange)')
call s:create_map('n', 'cxx', '<Plug>(ExchangeLine)')
and allows to use .
if the target is addressed by the same mapping. E.g. swapping distant lines cxx
, <no>G
and then press .
or cxiw
, move and then .
.
I point the reader to the screencast by Drew Neil to see it in action.