Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the x command in Vim work on text selected in visual mode even if it is not an operator?

Tags:

vim

Quoting the list of operators mentioned in :h operator:

The motion commands can be used after an operator command, to have the command
operate on the text that was moved over.  That is the text between the cursor
position before and after the motion.  Operators are generally used to delete
or change text.  The following operators are available:

    |c| c   change
    |d| d   delete
    |y| y   yank into register (does not change the text)
    |~| ~   swap case (only if 'tildeop' is set)
    |g~|    g~  swap case
    |gu|    gu  make lowercase
    |gU|    gU  make uppercase
    |!| !   filter through an external program
    |=| =   filter through 'equalprg' or C-indenting if empty
    |gq|    gq  text formatting
    |g?|    g?  ROT13 encoding
    |>| >   shift right
    |<| <   shift left
    |zf|    zf  define a fold
    |g@|    g@      call function set with the 'operatorfunc' option

Quoting how visual mode can be used with operators from :h operator:

Instead of first giving the operator and then a motion you can use Visual
mode: mark the start of the text with "v", move the cursor to the end of the
text that is to be affected and then hit the operator.  The text between the
start and the cursor position is highlighted, so you can see what text will
be operated upon.  This allows much more freedom, but requires more key
strokes and has limited redo functionality.

So I understand that if I select a few words in visual mode and press d, the selected words would be deleted, since d operator can be specified its operand either by motion key or by selecting text in visual mode.

But I see that if I select a few words in visual mode and press x, then also the selected words are deleted. However x is not an operator. I am unable to understand from Vim's help while this should work.

Could you please help me understand which commands work on text selected in visual mode and which ones don't and how I can figure this from Vim's help?

like image 889
Lone Learner Avatar asked Oct 28 '25 06:10

Lone Learner


2 Answers

First, d and :d[elete] don't delete, they cut. Using the word delete for d is a gross mistake that's repeated all over Vim's documentation and thus almost everywhere online. The only way to make d and friends actually delete something is to use the "blackhole" register, :help "_.

Second, you could have found the answer to your question by scrolling down a little from the :help you quoted:

Additionally the following commands can be used:
    :   start Ex command for highlighted lines (1)  |v_:|
    r   change (4)                                  |v_r|
    s   change                                      |v_s|
    C   change (2)(4)                               |v_C|
    S   change (2)                                  |v_S|
    R   change (2)                                  |v_R|
    x   delete                                      |v_x|
    […]

and by reading :h x and, again, scrolling down a bit or, more simply, following the v_x tag at the end of the line.

Your questions are well written and laid out but you should spend your time and effort a little more wisely. Reading past the first screen, for example.

like image 50
romainl Avatar answered Oct 31 '25 03:10

romainl


You are right that operator commands that normally take a motion change for visual mode in such a way that you do not specify the motion, but instead have the command operate on the selected text. But that's only one group of commands that work in visual mode.

Another group contains normal mode commands that work (without a motion) on the current / [count] character(s) or line(s), like x, r, or J. In visual mode, these also apply to the selected text.

This creates some overlap in visual mode, e.g. both d and x do the same, whereas in normal mode, d5l and 5x would be different.

like image 36
Ingo Karkat Avatar answered Oct 31 '25 03:10

Ingo Karkat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!