[sf-lug] vi(1)/vim(1)/nvi(1) d<motion> command; etc. (was: beginner's vim question)

Michael Paoli Michael.Paoli at cal.berkeley.edu
Sun Feb 10 10:56:53 PST 2008

Or, to perhaps put it much more concisely:
in vi(1) (and generally likewise in nvi(1) and vim(1))
in command mode, d starts a delete command.
In the most general form d, followed by a cursor motion command,
deletes the range from the cursor position through the position to
which the cursor motion command would move the cursor to.

When the cursor motion command is within a line,
if deleting to the left, characters are deleted starting to the left of
the cursor starting position, if deleting to the right, deletion starts
on the character where the cursor is located and proceeds to the right.

For inter-line motions, the current line through cursor motion target line
are deleted.

There are also some other delete commands from command mode that don't
precisely fit the above pattern - but they have slightly different
syntax/mnemonics.  E.g.
dd - delete current line (similar to other doubled character commands, that
     operate on whole lines, e.g. yy)
D - delete through end of line (equivalent to d$)
x - delete character where the cursor is (think "x out")
X - delete character to the left of the cursor

So ... d- deletes the current and preceding line, and d+ the current
and next line, and would be equivalent respectively to dk and dj.

Of course these command are also quite flexible and rather extensible in
the way vi(1) can combine things - e.g. by including a count or using
(most?) any cursor motion command, e.g.:
3dd - delete 3 lines
dG - delete current through last line
d1G - delete current through first line
dL - delete current line through last line in current window
dH - delete current line through first line in current window
de - delete through end of "word*"
dB - delete backwards to start of "bigword*"
2dw or d2w - delete 2 "words*"
40x - delete 40 characters (cursor position and to right)
d20| - delete to(right)/through(left) character column 20

*"word"s - letters, digits and underscore, other non-whitespace characters
are treated as separate "word"s; "bigword"s - non-whitespace

random vim(1)/nvi(1)/vi(1) comments:
I *really* like nvi.  vim tends to annoy me - it's not highly vi compatible
(and given the years of experience I have with vi, vim's non-compatibilities
tend to trip me up and slow me down significantly).  Some/many operating
systems / distributions well offer one a choice (e.g. Debian), others don't
particularly offer such a choice (e.g. Red Hat includes vim, but not nvi -
at least for many or all of their versions).
see also:

> Date: Sun, 3 Feb 2008 18:26:36 -0800 (PST)
> From: Alex Kleider <a_kleider at yahoo.com>
> Subject: [sf-lug] vim logic
> To: Linux userGroup <sf-lug at linuxmafia.com>
> Message-ID: <69923.88453.qm at web36604.mail.mud.yahoo.com>
> Content-Type: text/plain; charset=iso-8859-1
> --- RBV <GoodWriter2548 at earthlink.net> wrote:
> > > From: Alex Kleider <a_kleider at yahoo.com>
> > > Subject: [sf-lug] beginner's vim question
> > > 
> > > I'm puzzled by the behavior of the - and + movement commands within
> > > vim:
> > > They are defined as "to the first non blank character" of the line
> > > above/below the cursor.
> > > When used alone, that indeed seems to be what they do.
> > > BUT..
> > > if used as a motion parameter to the d(elete command:
> > > the deletion includes the line below or above AS WELL as the
> > > appropriate part of the line on which the cursor was sitting.
> > > 
> > > This seems counter intuitive to me. Can anyone shed some light as
> > to
> > > why it's set up this way?
> > > 
> > 
> > Interesting question.  In trying to puzzle out the solution, I came
> > upon a couple of paragraphs that seemed to be explaining the behavior
> > on this page: http://vimdoc.sourceforge.net/htmldoc/motion.html 
> > 
> > "The operator either affects whole lines, or the characters between
> > the start
> > and end position.  Generally, motions that move between lines affect
> > lines
> > (are linewise), and motions that move within a line affect characters
> > (are
> > characterwise).  However, there are some exceptions.
> > 
> > ...and...
> > 
> > "A character motion is either inclusive or exclusive. When inclusive,
> > the start
> > and end position of the motion are included in the operation.  When
> > exclusive,
> > the last character towards the end of the buffer is not included. 
> > Linewise
> > motions always include the start and end position.
> > 
> > So, if I position the insertion point on a given line in the
> > following text and in vim command mode
> > press dj (delete and move down), vim deletes two lines.
> > 
> > Line 1
> > Line 2
> > Line 3
> > Line 4
> > Line 5
> > 
> > A bit confusing, yes, but it fits nicely into "vim-logic"...
> > 
> It'll take me a while to puzzle through that one but I will!
> Thanks, Riley, for taking an interest.
> And thanks for the ref.

More information about the sf-lug mailing list