Is there a text object for current line?
Is there a text object for
^$
?
2 Answers
Here are 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>
---EDIT---
An "operator" is a command that doesn't do anything by itself: d
, y
, etc.
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 :omap
or :onoremap
to define custom operator-pending motions. The technique used here is inspired by :help omap-info
:
- you define a visual mode mapping that covers the desired area,
- you use that mapping in an operator-pending mapping.
Note that I used :onoremap
because il
, al
and i%
don't already exist. If you are overriding an existing motion or text-object — say ip
— you must use a recursive mapping: :omap
.
:help :map-operator
describes a slightly more advanced method.
See :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 doeso
in 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:h
on it? I can't find it with:h x_o
or:h o
.– lindheCommented Jan 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/…– BlascoCommented Sep 25, 2019 at 17:24