2

If my cursor is in the middle of a line, how can I select the whole active line (i.e. without the possible indentations at the beginning of the line, so between ^ and $) without moving the cursor to the beginning or end of the line? I was thinking if there something similar to inner motions (e.g. diw or yip where the command is executed to the word or paragraph from where ever my cursor is inside the word or paragraph). I though maybe there is some imaginary vil?

9
  • 2
    The obvious answer would be V or is that not working for some reason?
    – Friedrich
    Mar 14, 2023 at 11:20
  • V selects the whole line, including the indentations.
    – Student
    Mar 14, 2023 at 11:24
  • 1
    In normal visual mode, as opposed to visual line mode, the cursor has to be at one or the other end of the visually selected text. Think of how you select text on browser with mouse. Can you put the cursor anywhere else other than the end of the selected text?
    – 3N4N
    Mar 14, 2023 at 11:44
  • 3
    Does this answer your question? Is there a text object for current line? Mar 14, 2023 at 12:24
  • 1
    Maybe this question could help you answering yours. Mar 14, 2023 at 12:25

1 Answer 1

3

The cursor is always at one end of the visual selection so what you are asking for is not possible.

But… you don't really select thing just for the fun, do you? You select them to do something with them, right?

Maybe what you actually want is the cursor to come back to where it was before you entered visual mode after you are done? In that case, you might want to provide more context, i.e. what you want to do with the selection, etc.

Maybe what you want to do could be better done without visual selection to begin with? In that case, a custom pseudo-text object might help.

It is difficult to get from your question.

For what it's worth, these are the pseudo-text objects I settled on after posting the answer linked in the comments:

xnoremap il g_o^
onoremap il :<C-u>normal vil<CR>
xnoremap al $o0
onoremap al :<C-u>normal val<CR>

The first one, il, covers the line without leading and trailing whitespace while the second one, al, covers the whole line, including EOL.

The problem I tried to work around with these pseudo-text object is that Vim only gives us a "line" primitive that comes with a lot of bagage:

  • it is line-wise,
  • it includes EOL,
  • it includes leading and trailing space.

Moreover, we have things like yy or dd but we don't really have a proper "line" motion/text object (and no, :help _ is not exactly that), let alone a "content of the line" one.

My il pseudo-text object gives me that missing "content of the line" primitive ("quote this line", "cut this line character-wise", etc.), while its al friend acts as an idiomatic mirror.

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.