2

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
  • 1
    When you have the cursor at the top of the file, type in the key sequence to delete everything in the file: dG d meaning delete, and G meaning till end of file.
    – Terrance
    Apr 27, 2015 at 20:32
  • Uh, no it doesn't. It doesn't work in any version of Vim.
    – muru
    Apr 27, 2015 at 20:45
  • @muru Try telling that to my currently open vi sessions. Apr 27, 2015 at 20:46
  • @javadba I don't need to.
    – muru
    Apr 27, 2015 at 20:46
  • @muru " I don't need to". I can not say it any more clearly than I just did it - and did it dozens of times on LTS 14.04 on a different ubuntu machine. So please keep your misinformed OPINION (not fact). Apr 27, 2015 at 20:47

2 Answers 2

3

@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.

2

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
  • This works. It would however still be nice to know : why is my approach - which does work on dozens of other systems - not work on this particular one. Apr 27, 2015 at 20:53
  • @javadba Check on all of them, with vim -u NONE - disabling your usualy vimrc and plugins.
    – muru
    Apr 27, 2015 at 20:54
  • @javadba How do I know you even have a shirt on?
    – muru
    Apr 27, 2015 at 20:57
  • @javadba I'm not saying you didn't do something. I'm asking you to do something (check with vim -u NONE). It's up to you to do that (or not do that and say "tell that to my vim", which is just as belittling). Notice I haven't downvoted your question for being obviously fantasy.
    – muru
    Apr 27, 2015 at 21:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.