Using "dd" in Vi (delete multi lines) not accepted if number of lines > file length

I often edit a file locally then cut and paste it into a remote ssh session inside a vi session.

The first step is to delete the entire contents by doing something like

 9999dd

Which will delete up to 9999 lines of the file.

Now somehow the version of vi on a certain ubuntu machine running 14.04 LTS is behaving differently: it is not allowing the above command if the file does not actually contain at least 9999 rows.

How do I re-enable the former /familiar behavior - in which all lines in the entire file would be erased if N (9999 in this case) exceeds the file line count?

UPDATE I appreciate answers coming in for alternative methods to achieve this. However that does not explain why/how/when the behavior changed (I have used Ndd successfully for years. Maybe not the optimal way to do it. But it does work - including on ubuntu - various versions, centos, os/x).

Another update The ":" in front was removed : we are in normal mode. It STILL DOES NOT WORK in this particular editor/vi instance - whereas it works with say 99dd (since the file has > 99 lines) and also doing the same action 9999dd works in other instances.

10

2 Answers

@javadba, try :set nocompatible

The difference between these two behaviors seems to depend on whether I have :set nocompatible in my ~/.vimrc or in /etc/vim/vimrc or wherever the global one is.

If I :set nocompatible then I can use :9999 rather than :$ to jump to the bottom of a small file, and I can get away with things like 9999dd. If I have not :set nocompatible then nothing happens when I try 9999dd, and I get an error E16: Invalid range when I try :9999 to jump to the end of the file.

What you are trying (to use an invalid range in command mode), can never have worked. It doesn't work in 7.2.445 (Debian 6), 7.2.411 (CentOS 6.6), 7.3.429 (Ubuntu 12.04), 7.4.683 (Ubuntu 14.04, PPA).

I think you have confused it with 99999dd, which is valid in normal mode because N here is not a line number, but the number of times the action is repeated - note the absence of :.

Indeed, the documentation says as much:

Line numbers may be specified with: :range E14 {address} {number} an absolute line number
....
The {number} must be between 0 and the number of lines in the file. When
using a 0 (zero) this is interpreted as a 1 by most commands. Commands that
use it as a count do use it as a zero (:tag, :pop, etc). Some commands
interpret the zero as "before the first line" (:read, search pattern, etc).

If you want to delete all lines, and want use the command mode to do so, then:

:%d

Or, using ranges:

:0,$d

If you don't want to use the command mode:

ggdG

In neither case should you use a weird number for a range.

4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like