11
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ The [code-golf] tag means fewest characters wins. You challenge is more complicated than that, so I've changed the tagging. \$\endgroup\$ Nov 30, 2011 at 13:41

4 Answers 4

3
\$\begingroup\$
: 0,0 w 
:r

ruins everything no undo

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 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 :) \$\endgroup\$
    – saeedn
    Nov 30, 2011 at 21:38
  • \$\begingroup\$ @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 \$\endgroup\$ Nov 30, 2011 at 21:42
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – bitmask
    Nov 30, 2011 at 23:02
3
\$\begingroup\$
: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.

\$\endgroup\$
4
  • \$\begingroup\$ ggdG could be replaced by: %d \$\endgroup\$
    – kenorb
    Oct 25, 2015 at 22:17
  • \$\begingroup\$ @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 :) \$\endgroup\$
    – saeedn
    Nov 4, 2015 at 1:41
  • \$\begingroup\$ @saeedn :%d_ is shorter than :g/^/d and also solves the put problem. \$\endgroup\$
    – Rich
    Mar 8, 2018 at 9:54
  • \$\begingroup\$ 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. ;) \$\endgroup\$
    – Rich
    Mar 8, 2018 at 9:57
2
\$\begingroup\$
: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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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