17
  • Is there a text object for current line?

  • Is there a text object for ^$?

1

2 Answers 2

19

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:

  1. you define a visual mode mapping that covers the desired area,
  2. 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.

1
  • 1
    Cool. Would you mind explaining a few bits of it? What is o (operator pending mode) in onoremap? What does o 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 :hon it? I can't find it with :h x_o or :h o.
    – lindhe
    Commented Jan 14, 2016 at 18:14
6

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.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.