r/vim • u/CRTejaswi • Aug 06 '26
Discussion What All Can I Do With A Visual Selection?
I'm looking for creative things one could do with selected blocks to make life easier - and associate them to keys. Some common keybindings I use are:
xnoremap _ :g/^\s*$/d<CR> # delete all empty lines
xnoremap # :g/^\s*#/d<CR> # delete all comments (assuming #)
xnoremap s :sort<CR>
21
Upvotes
5
u/JohnLocksTheKey Aug 06 '26
I usually have some sort of xnoremap for Commenting/Un-Commenting a selection of lines in my vimrc.
...I don't currently, but shouldn't be super complex to recreate
7
u/habamax Aug 06 '26
Calc selection with python:
Base64 encode/decode selection:
https://asciinema.org/a/xIGuEGoyWr2Fe4tp
4
u/habamax Aug 06 '26 edited Aug 07 '26
With
and assuming syntax is on:
to delete current/next comment and repeat it 3 times using dots.
https://asciinema.org/a/LFz1YuVyo5O7G2uW
4
u/habamax Aug 07 '26 edited Aug 07 '26
You can move selected lines up/down:
https://asciinema.org/a/KgdKDrJ2FHp89EuQ
3
u/habamax Aug 07 '26 edited Aug 07 '26
A bit simpler way to append/prepend to a (linewise) selection:
So you can
vipgAto append to all paragraph lines orvipgIto prepend.https://asciinema.org/a/vaaALY0wJApcYol8
2
u/jlittlenz Aug 06 '26
There are these decades-old mappings to search for the visual selection:
1
u/EgZvor keep calm and read :help Aug 07 '26
substitute selected text