Is there a text object for current line?
Is there a text object for
2There exists a plugin– Christian BrabandtJan 12, 2016 at 19:28
Here is a crude "line" text-objects:
xnoremap il g_o0 onoremap il :normal vil<CR> xnoremap al $o0 onoremap al :normal val<CR>
And a crude "buffer" text-object:
xnoremap i% GoggV onoremap i% :normal vi%<CR>
An "operator" is a command that doesn't do anything by itself:
Pressing those keys put you in "operator-pending mode", where Vim waits for you to tell it what you want it to operate on… with an "operator-pending motion".
You can use
:onoremap to define custom operator-pending motions. The technique used here is inspired by
- you define a visual mode mapping that covers the desired area,
- you use that mapping in an operator-pending mapping.
Note that I used
i% don't already exist. If you are overriding an existing motion or text-object — say
ip — you must use a recursive mapping:
:help :map-operator describes a slightly more advanced method.
:help v_o for
o in visual mode.
1Cool. Would you mind explaining a few bits of it? What is
o(operator pending mode) in onoremap? What does
oin visual mode do? I would guess that it is "other side", because it seems to jump to the other side of a selection... How do i find
:hon it? I can't find it with
:h o.– lindheJan 14, 2016 at 18:14
I would suggest an additional solution, to use ^ instead of 0 (so the line starts at the beginning of the first character:
xnoremap il g_o^ onoremap il :normal vil<CR> xnoremap al $o^ onoremap al :normal val<CR>
This way if we do "change in line" it will respect the indentation level.
There is also a plugin providing this functionality by vim master kana: github.com/kana/vim-textobj-line/blob/master/doc/…– BlascoSep 25, 2019 at 17:24