Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use 0d_ in DiffOrig in Vim?

Tags:

vim

In the online manual of Vim, :help DiffOrig will show the recommended command sequence to get changes of current editing file.

Here it is:

command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
                   \ | wincmd p | diffthis

I wonder what the effect of 0d_ is. I tried 0d_ in normal mode, it works like dd, but I can't understand why it used here.

like image 297
longbowk Avatar asked Mar 22 '12 10:03

longbowk


Video Answer


1 Answers

Let's explain it a bit: Suppose you have original foo.txt containing (with line numbers):

1 a
2 c
3 d
~

You have added a line containing “b” between lines 1 and 2:

  • :vert new creates a new, empty, buffer in a vertical split (:help :new)
  • :set bt=nofile makes it a scratch buffer (:help 'bt'). Note:

    1 a  | 1 ·<cursor here
    2 b  | ~
    3 c  | ~
    4 d  | ~
    ~    | ~
    
  • :r # inserts after current line, contents of alternate file (#), as stored on the file system. You haven't saved the other buffer, so you get original content. (:help alternate-file).
    :help :r tells you that it always inserts after. Therefore:

    1 a  | 1
    2 b  | 2 a
    3 c  | 3 c
    4 d  | 4 d
    ~    | ~
    
  • :0d_ removes the first line. Why 0, I don't know it really, I would rather write it :1d_. :help range tells:

    When using a 0 (zero) this is interpreted as a 1 by most commands

    The _ specifies that it goes to the black-hole register. See :help :d about the :d ex command, it works linewise.

The rest is obvious.

like image 84
Benoit Avatar answered Sep 28 '22 09:09

Benoit