Most destructive "forgot-to-enter-insert-mode" sequence in Vim

10

1

Pretend you have an arbitrary text file (which you may choose).

Give the sequence of keystrokes that is most destructive (*) if it was intended to be inserted to the text, but instead was entered in normal mode (i.e. i/a was not pressed). You may not enter visual mode and not quit Vim.

Destructive is defined as follows: The ratio of changed/deleted characters by the number of necessary keystrokes.
You get bonus points if your changes cannot be undone by a fixed number of undos.


Example: Input file without line breaks. Input sequence: dfs.

bitmask

Posted 2011-11-29T23:21:40.057

Reputation: 335

The [code-golf] tag means fewest characters wins. You challenge is more complicated than that, so I've changed the tagging. – dmckee --- ex-moderator kitten – 2011-11-30T13:41:19.870

Answers

3

: 0,0 w 
:r

ruins everything no undo

ratchet freak

Posted 2011-11-29T23:21:40.057

Reputation: 1 334

1I don't understand how it ruins everything! when I run this script it duplicates content of my file and undo works fine! could you explain? Thanks :) – saeedn – 2011-11-30T21:38:08.097

@saeedn :0,0 w should write lines 0 to 0 and :r would then read it (now empty) back in, I don't really have a vim to test it though – ratchet freak – 2011-11-30T21:42:54.670

1Well, first of all, : 0,0 w writes a partial buffer, so (my) Vim refuses to save it, I have to say w! instead of w. So far so good, now :r does not reopen the file. Instead, you have to say :e which will read in the file from the filesystem. However, although with this modifications it does change the buffer, the buffer is far from empty. It contains the first line (because you saved "0,0"). At least undoing does not work. – bitmask – 2011-11-30T23:02:19.177

3

:set ul=-1
ggdG
:w

This clears contents of file and saves it. No undo is possible because undolevel is set to a negative number, which disables undo operation.

Edit: It's better to write :g/^/d instead of ggdG, because in the latter case you can use p (put) to roll-back the changes.

saeedn

Posted 2011-11-29T23:21:40.057

Reputation: 1 241

ggdG could be replaced by: %d – kenorb – 2015-10-25T22:17:52.777

@kenorb Yes, that's shorter, but it also has the problem of ggdG, that can be inverted by putting (p), so :g/^/d seems still a better option :) – saeedn – 2015-11-04T01:41:08.433

@saeedn :%d_ is shorter than :g/^/d and also solves the put problem. – Rich – 2018-03-08T09:54:08.143

Also, outside of the possible context of writing this answer, I'm not sure if anyone would ever accidentally type those keystrokes in, thinking they were in insert mode. ;) – Rich – 2018-03-08T09:57:49.737

2

:set ul=-1
:%s///g
:r!head -c1G</dev/urandom
:w
  1. Disables undo
  2. Deletes characters
  3. Reads 1G of data from /dev/urandom
  4. Saves

Bending the rules, because I am adding characters (a character that isn't there and now is, means a character was changed). I can add as much characters as I want so this score is theoretically infinite.

Nejc

Posted 2011-11-29T23:21:40.057

Reputation: 241

0

5 bytes:

:bd!

followed by a press of enter.

Throws away the current state of the file you're editing from memory, so all unsaved changes in that file are lost. If you have other files open, they are not affected. This does not quit vim even if you have only one file open.

b_jonas

Posted 2011-11-29T23:21:40.057

Reputation: 341