mawww / kakoune

mawww's experiment for a better code editor
http://kakoune.org
The Unlicense
9.94k stars 716 forks source link

Last modified buffer #3607

Open pickfire opened 4 years ago

pickfire commented 4 years ago

Feature

^ in vim. To jump to the previous modified buffer. When opening like 10+ buffer, I find myself frequently typing a few times (~5x) to :bp or :bn and I get lost easily when switching to previous buffer.

Usecase

It would be good to quickly switch to last modified buffer easily. It should be similar to :bp and :bn except the ordering is based on last modified rather than buffer creation time.

Screwtapello commented 4 years ago

There's ga, although I think that switches to the previously-active buffer, not the previously-modified buffer, which is almost the same but not quite.

As an alternative, instead of switching between recently-used files (which can get confusing when you need to switch to a third file and suddenly your other files are no longer at the same place in the rotation), Kakoune makes it easy to switch between files by name. For example, if I already have the buffer open, I can type:

:b wc<tab>

...to automatically complete:

:b ~/.config/kak/autoload/word-count.kak

...and while :b wc<tab> is more keys than ^, it beats the heck out of :bn :bn :bn :bn, and it doesn't suddenly do something different if I briefly switch to a third file.

Some Kakoune users even define a custom command that uses a tool like ripgrep to list all the files in the current directory and add them to the completion list, so (if Kakoune's current directory is in the right place) you don't even have to have the buffer open - you can just :find wc<tab> whether the file is already open or not.

pickfire commented 4 years ago

There's ga, although I think that switches to the previously-active buffer, not the previously-modified buffer, which is almost the same but not quite.

Ah, I didn't know about that. Let me check that.

As an alternative, instead of switching between recently-used files (which can get confusing when you need to switch to a third file and suddenly your other files are no longer at the same place in the rotation), Kakoune makes it easy to switch between files by name. For example, if I already have the buffer open, I can type:

Oh, but the issue is I don't usually remember what file name am I working on or the previous one, it take me around 5-10 seconds to recall that which I think :bp multiple times is faster.

Some Kakoune users even define a custom command that uses a tool like ripgrep to list all the files in the current directory and add them to the completion list, so (if Kakoune's current directory is in the right place) you don't even have to have the buffer open - you can just :find wc whether the file is already open or not.

I have fzf.kak (using sk) in place but I almost never used that since I don't remember file names like I mentioned (I only remember what part of code I worked on). Maybe I think I should do now.

krobelus commented 4 years ago

ga is incredibly useful when navigating grep results because it always goes back to the *grep* buffer unless you explicitly switched buffer.

There is the kakoune-buffers plugin which provies a user mode where m is bound to buffer-first-modified. You can probably also write a :modified-buffer command that works like :buffer but only shows modified files.

If you're using git and have saved your buffers, then you can run :git diff and use a keybinding like this to directly jump to a line from the diff.

hook global BufCreate \*git\* %{
    map buffer=*git* normal <ret> %{: git-goto-line<ret>}
}
define-command git-goto-line %{ evaluate-commands -save-regs c %{
    evaluate-commands -draft %{
        set-register c %val{cursor_column}
        execute-keys <a-l><semicolon><a-?>^diff<ret><a-x>
        set-register c %sh{
            printf %s "$kak_selection" | column="$kak_reg_c" perl -wne '
            use Env;
            $file = $1 if m{^diff --git a/\S* b/(.*)$};
            $line = $1 - 1 if m{^@@ -\d+,\d+ \+(\d+),\d+ @@};
            $line++ if defined $line and m{^[ +]};
            END {
                $root = `git rev-parse --show-toplevel`;
                chomp $root;
                $column--;
                print "edit -- $root/$file $line $column"
            }
            '
        }
    }
    evaluate-commands -try-client %opt{jumpclient} %reg{c}
    try %{ focus %opt{jumpclient} }
}}

Nothing to exactly replicate ^ but in general I find :b to work really well. It might be possible to try to implement ^ but as far as I know Kakoune does not store full recently-used order so it seems tricky and I think usually there are better alternatives.

Screwtapello commented 4 years ago

Yeah, trying to remember a file-name is definitely more effort than just remembering "the last file I was in", but it's one of those mental muscles that gets stronger the more you use it. I understand it might not be the right approach for everyone, but I think the extra effort pays off for me because:

On the other hand, if you don't primarily work on long-lived projects containing a fixed set of files, those reasons may not apply to you, and a command like ^ or ga may well be the best option!

pickfire commented 4 years ago

If you're using git and have saved your buffers, then you can run :git diff and use a keybinding like this to directly jump to a line from the diff.

But I usually have ~10 files being modified in git at the same time, I still need to search one by one which one was I working on.

with tools like :find or "fzf.kak", I can think about "files in this project" and not care which of them happen to be open in the current session, which actually saves some mental room

I have been thinking on how to use that but usually I just go up by few levels of directories.

pickfire commented 4 years ago

Looks like ga is indeed what I want, the behavior seemed like ^.

pickfire commented 4 years ago

ga isn't what I am looking for, I tried bn a few times but ga goes back to the previous buffer, can we change the behavior of ga to track the last modified buffer or can we add gm? So ga means last accessed and gm means last modified, I personally find ga pretty useless.