Kakoune: Scratch buffer contents is lost by accident

Created on 2 Mar 2019  路  14Comments  路  Source: mawww/kakoune

This put me off Kakoune for a long time:

  1. kak
  2. Enter lots of text, intended to be the contents of a new file.
  3. Accidentally :q

And you lose the whole lot! Now I understand that *scratch* is designed to be lost, but this will definitely burn people, as most other editors don't delete data this readily. Emacs solves this by adding text at the beginning of *scratch*:

;; This buffer is for notes you don't want to save, and for Lisp evaluation.                                                                                                                          
;; If you want to create a file, visit that file with C-x C-f,                                                                                                                                        
;; then enter the text in that file's own buffer.                                                                                                                                                     

But I'd suggest copying vim, and opening a non-scratch unsaved buffer by default.

Most helpful comment

map global user s -docstring 'Evaluate the main selection' ':<c-r>.<ret>'
map global user S -docstring 'Evaluate the current buffer' '%:<c-r>.<ret>'

All 14 comments

I like the current design and get annoyed when using vi(m) and cannot just :q in some usecases. however you can set an alias in your shell: alias k='kak newfile'

Maybe going the emacs route is a viable option to improve the user experience.

related #2360 & #1496

I'd also wanted *scratch* to be something like a buffer for kakscript evaluation, similar to Emacs' *scratch*. I really love that in Emacs you can fire up a scratch buffer, write some Emacs Lisp in it, and evaluate it. This is certainly doable with Kakoune's source %val{buffile} but it requires a file to do evaluation. It's extremely useful when you want to quicly define a command or a hook, without using singleline prompt mode

map global user s -docstring 'Evaluate the main selection' ':<c-r>.<ret>'
map global user S -docstring 'Evaluate the current buffer' '%:<c-r>.<ret>'

Thanks, @alexherbo2. That's a nice addition to my configuration

@mawww I preferred the scratch buffer being empty.

hook -always -once global WinCreate '\Q*scratch*' %{
  hook -always -once window InsertEnd '' %{
    info -title 'Information' %[

This is a *scratch* buffer which won鈥檛 be automatically saved.
Use it for notes or open a file buffer with the :edit command.

    ]
  }
}

can I delete *scratch* automatically if no changes were made to it and another file was opened?

  1. Start kakoune with no arguments

    • buffers: *scratch*, *debug*

  2. Open file

    • buffers: file, *debug*

Or:

  1. Start kakoune with no arguments

    • buffers: *scratch*, *debug*

  2. Write something to scratch and open file.

    • buffers: file, *scratch*, *debug*

@andreyorst I've got this in my kakrc that pretty much does it

# delete the *scratch* buffer as soon as another is created, but only if it's empty
hook global BufCreate '^\*scratch\*$' %{
    hook -once -always global BufCreate '^(?!\*scratch\*).*$' %{
        try %{
            # throw if the buffer has more than one character
            exec -buffer *scratch* 'L<a-K>..<ret>'
            db *scratch*
        }
    }
}

Likely needs to be updated now that *scratch* contains default text

The scratch buffer being populated with text feels like my buffer is dirty.

The scratch buffer being populated with text feels like dirty.

I think that the default text should be configured via option, much like in Emacs. I don't use default text there, but some people may do.

I've got this in my kakrc that pretty much does it

That's clever. Added this to my kakrc, thanks!

I don鈥檛 agree on the direction taken to solve this issue.

The root of the issue was not the scratch feature by itself, but filling a scratch buffer to edit a new document when no explicit file-name was given.

@alexherbo2 I agree it is not very satisfying, there is a already a [scratch] marker in the mode line, but this is an issue that comes back regularly and I figured that would make new comers more aware of the semantics of scratch buffers in Kakoune.

Happy to revisit if we find a better way to make scratch buffer behaviour more obvious (I think the scratch buffer behaviour is sane, but newcomers do not know what to expect from them and get confused that what they have typed in it gets lost with no warning).

I think that the default text should be configured via option, much like in Emacs. I don't use default text there, but some people may do.

I agree, this way it is on by default, but easy to turn off, just like a lot of other settings (clippy comes to mind)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

valerdi picture valerdi  路  4Comments

vbauerster picture vbauerster  路  3Comments

MasterOfTheTiger picture MasterOfTheTiger  路  4Comments

radare picture radare  路  3Comments

notramo picture notramo  路  3Comments