Shaun
Shaun

Reputation: 490

why does the “$d” command not work in the ex mode of vim?

I found the $d command that can't work in the ex mode of vim .But it can work in the command line mode of vim . As follow:

I have a file called foo whose content are as follows :

$ cat foo
line1
line2
line3
line4
line5

The follow that i tried to delete the last line of foo file in ex mode of vim. The last line is the line where line5 is located.

$ vim -E -s foo <<-EOF
> $d
> w
> EOF

But,the content of foo file is not change , the last line still exists:

$ cat foo
line1
line2
line3
line4
line5

The follow that i use the command line mode of vim to execute $d command:

  1 line1
  2 line2
  3 line3
  4 line4
  5 line5
~
~
1          foo                                      0x6C      1,1           all
:$d

The result is:

$ cat  foo
line1
line2
line3
line4

The last line was successfully deleted.

In addition to using the $d command , i have used the 1d command and the 1,2d command , both commands work properly.

UPDATE: add an example

The contents of foo file are as follows:

$ cat foo
line1
line2
line3
line4
line5

I processed this file with the ex mode of vim in bash:

$ vim -E -s foo <<-EOF
> 1d
> 2,3d
> $d
> w
> EOF

The result is:

$ cat foo
line2
line5

The result I expect is that only the line where line2 is located will remain. But the line where line5 is located was remained , it should be deleted by $d command.

why ? who can help me?

Upvotes: 1

Views: 195

Answers (1)

Kent
Kent

Reputation: 195209

You can just use -c:

vim -c '$d' -c 'w' file

You used -s {scriptin} read man page what does it mean. You should pass a file to -s, and it contains: :$d^M:w^M (which ^M is ctrl-v ctrl-m)

or:

vim file -s <(echo ':$d^M:w^M')

^M linebreak should be typed in the same way as above.

Upvotes: 1

Related Questions