Show me your gf (goto-file)

A gripe I have with the default goto-file command is that it doesn’t understand syntax like file:line:column which is often outputted by debuggers (including kakoune’s !).

I’ve looked around and found some promising plugins, which I might link if I find again. They were focused on deciphering line and column numbers, and my current need is to quickly navigate rust’s cargo ouput.

So I made the following script file, which does 2 things:

  • provide a way to quickly navigate from filename to filename (candidates) (mapped to gF)
  • decipher one very simple format of file:line:column (overriding gf)
## better gf

map -docstring "file[:line[:column]]" global goto f "<esc>: goto-file<ret>"
define-command -docstring "go to the file corresponding to the current selection" goto-file %{
  # TODO add something to the effect of -itersel
  evaluate-commands edit -existing %sh{ echo "$kak_selection" | sed 's/:/ /g' }
}
alias global gf goto-file

# TODO use @val{count} if provided
define-command -docstring "go to the next filename candidate" -hidden goto-filename-regex %{
  evaluate-commands -draft %{
    try %{
      edit -existing *gf*
    } catch %{
      edit -scratch *gf*
      # turning the result of ls into a regex
      execute-keys '! ls -aF<ret><a-s>i|\Q<a-;><a-;>\E<del><esc>'
      # inserting absolute paths (root and home)
      execute-keys ',I((?<lt>=\s)~?/[\w\.]<esc>'
      # allowing suffixe
      execute-keys 'A)[^\s]*<esc>'
    }
    execute-keys -save-regs '' 'ggx_"/y'
  }
}

define-command -docstring "go to the next filename candidate" -hidden goto-filename %{
  evaluate-commands -save-regs '/' %{
    goto-filename-regex
    execute-keys '/<ret>'
  }
}
alias global gF goto-filename

define-command -docstring "go to the previous filename candidate. Unstable, might get replaced by count goto-filename" -hidden goto-filename-back %{
  evaluate-commands -save-regs '/' %{
    goto-filename-regex
    execute-keys '<a-/><ret>'
  }
}

map -docstring "goto filename" global goto F "<esc>: goto-filename<ret>g"
map -docstring "goto filename" global goto <a-F> "<esc>: goto-filename-back<ret>g"

I’m very curious how other people tackle that problem. I’m not entirely satisfied with what I have now, but for me, it’s a huge improvement over selecting filenames manually.

1 Like

I use this, it’s very helpful:

define-command -override my-goto-file -docstring "jump to file:line:col at cursor" %{
	evaluate-commands -save-regs ^ %{
		try %{
			execute-keys -save-regs '' Z
			execute-keys %{<semicolon><a-a><a-w>1s(?:cargo:warning=)?([^\s]+)(?::(\d+))(?::(\d+)\b(?![.]))<ret>)<space><esc>,<esc>}
		} catch %{
			execute-keys -save-regs '' z
			execute-keys %{<semicolon><a-a><a-w>1s(?:cargo:warning=)?([^:\s]+)(?::(\d+))?(?::(\d+)\b(?![.]))?<ret>)<space><esc>,<esc>}
		}
	}
	execute-keys -with-hooks "gf%reg{2}g<a-h>%reg{3}lh"
}

my current need is to quickly navigate rust’s cargo ouput.

cargo-next-error from the kakoune-cargo plugin should work.
We should try to add that to the stdlib, potentially merging into make.kak

2 Likes