UltraNoob File tree question

Hi Folks, not new to CLI editors (I actually started back in the 1980s with edlin (ed) and used it as my primary FORTRAN editor for almost 10 years - then every IDE became language specific ).

Now, after a coding break, I’m trying to leave the IDE world and get back to CLI tools.

Anyway…I’m trying to be sure I’ve got this in my head before I start adding plugins.

If I want to open a file while I’m in kak (I’m editing a c file and now need the h too) it appears to me that I would :grep first to select the file and then pipe it into kak.

Am I correct in thinking that’s the general workflow? And that the file trees just make it “prettier”? OK, maybe faster too but you get the point…

PS - I’m asking this without even having rudimentary muscle-memory on the motions yet :roll_eyes: That’s how new I am to Kakoune

The easiest way to open a new file while you’re in Kakoune is with the :e (or :edit) command. It offers completions for all the files in the current directory, so if you know the file you’re looking for, liberal use of <tab> will get you there quickly.

:grep will search for files containing a given regular expression in the current directory (like the Unix grep command). It’s useful if you don’t know the file you’re looking for, but you know it must contain some pattern. For example, if you want to look up the definition of a function and you don’t know what source file it’s in.

3 Likes

Awesome!

Thank you very much. I appreciate it.

1 Like

That’s the general workflow yeah. Built-in, you have :e, gf, :grep, <c-x>f and <c-r>% at your disposal. gf goes to the file corresponding to your selection (g is goto and it opens contextual “menu”). Filename are also auto-completed in both insertion and command line mode; when they are not (for example, typing out a local path in insert mode without starting by ./), you can force kakoune’s completion type to filename using <c-x>f (<c-x> is complete and it opens contextual “menu”). Finally, from insertion mode and command mode, you can paste from a register with <c-r>, with a contextual menu showing up; % in particular holds the buffer name, which defaults ot the filename.

So for your example, you could have done something along the lines of:

:e <c-r>%<del>h<tab><ret>

to open a new file, paste the name of your current buffer, replace the ‘.c’ with ‘.h’, and open it. The tabulation is for good measure, in case your header file is not right next to your c file, kakoune should still be able to fuzzy find it and tab would select it.

One last built-in way to navigate files is to actually quit kakoune and use the full power of your shell/file explorer/whatever environment. You can start a healdless kakoune server using:

setsid kak -d -s your_session_name

it will persist even with no client attached (you need kill to end it). So you can :quit it, do whatever you want, and reopen a client attached to it with:

kak -c your_session_name [file_names_pattern]

That’s particularly helpful to open multiple files at once using a glob pattern or a shell expansion. In your particular case, you could use find or fdfind ( GitHub - sharkdp/fd: A simple, fast and user-friendly alternative to 'find' ) from the shell.

About filetrees you can literally have one in kakoune piping in (!) the command tree. As far as speed is concerned, you can use the full power of a modal text editor to navigate that tree, it beats graphical representation for huge file tree where you at least vaguely know what you’re looking for. You can combine that with mapping shortcuts to make things faster.

Here is how you can make an actionable filetrees in kakoune less than 10 lines of code (excerpt from my kakrc):

define-command -file-completion -params .. -docstring \
  "tree [<arguments>]: execute tree into a scratch buffer, with the following options :
- -f to have full pathnames (easier to gf).
- --charset to have indentation using plain space characters instead of fancy character art (easier to gf, after replacing any fancy character with space).
- whatever arguments provided." \
tree %{
  try %{
     delete-buffer *tree*
  }
  edit -scratch *tree*
  execute-keys "! tree -f --charset ASCII %arg{@}<ret>%%s^[\h|\-`]+<ret>r<space>gg"
  map buffer normal <ret> 'x: goto-file<ret>'
}

Not as pretty as :grep, but gets the job done. I have something similar with find.

2 Likes

WOW. Something tells me that as I level up in kak my overall 'nix skills will level up right along with it. :sunglasses: :sunglasses:

Thank you sir. I’ll be sure to check up that github page as well.

2 Likes

I’ll also put in a quick plug for fzf. It’s my primary method of exploring codebases and easily integrated with Kakoune. After becoming familiar with FZF’s concise syntax, I personally find more traditional file trees tedious in comparison.

FZF has some good docs, but just LMK if you want the particular shell options I’m showing in this GIF:
output

1 Like