# Most destructive “forgot-to-enter-insert-mode” sequence in Vim

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.

• 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 Nov 30 '11 at 13:41

: 0,0 w
:r


ruins everything no undo

• I 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 Nov 30 '11 at 21:38
• @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 Nov 30 '11 at 21:42
• Well, 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 Nov 30 '11 at 23:02
: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.

• ggdG could be replaced by: %d – kenorb Oct 25 '15 at 22:17
• @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 Nov 4 '15 at 1:41
• @saeedn :%d_ is shorter than :g/^/d and also solves the put problem. – Rich Mar 8 '18 at 9:54
• 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 Mar 8 '18 at 9:57
:set ul=-1
:%s///g
: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.

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.