Filter lines containing string and be able to edit

I would like to create a package that enables filtering lines containing a certain string similar to the KEdit command “ALL”.

This is different from using “Project Find Results” on the opened file because of two things: The ALL command does not open up a new TextBuffer, it just hides all the lines not containing the given string. Also, and this is the main advantage, it allows you to perform edits on the filtered lines right away (no need to click on a specific line, do some edits, switch back to the find results and go to the next line etc.).

Another nice feature is, that once lines were filtered using “ALL”, you can use “LESS” to reduce the results by lines containing a certaing other string, or “MORE” to do the opposite.

Now my question is, how could a package providing this functionality look like? I wouldn’t mind opening a new TextBuffer, but at least I want to be able to do edits within that TextBuffer resulting in the correct changes to the original file. Folding does not seem to be an option as I could not find a way to set Folding levels manually.

Thanks for your help!

Some references to packages for other editors featuring similar behaviour:
ALL command

1 Like

I haven’t seen any features in Atom to filter out certain lines for displaying. You don’t want every line to be folded, that’s for sure, but you might be able to make a compromise by folding the entire document and recursively unfold each line that matches the search string. Here’s an example of recursively unfolding just the cursor line.

Thanks for your reply! I tried your folding example, and while it goes into the right direction, the way folding is currently implemented in Atom it doesn’t allow me to truly fold the entire content of the document. Basically your left with a ton of unfoldable lines (depending on the length of your document indentation). This would work fine if there was a way to control the folding, like setting manual “fold points”.

For that, you would require more expertise in how the editor object is set up than I have. Maybe a dev knows if that would be implementable given the architecture of how Atom presents a file.

Here’s a package I made for this after finding myself with the same needs.

It’s very much a work in progress so feedback / pull-requests welcome :slight_smile:

FYI the crux of the folding is by editor.foldBufferRange, which I cannot find API docs for, but I borrowed the implementation from here.

Wow, that’s really cool! I’ll checkout the repo asap.

Hi, jessexknight, I am new to Atom, just installed your filter lines package, but don’t know how to exactly use it, I tried to use Ctrl+Alt+f, but nothing happened, can you please help?

The documentation is wrong. The only keybinding in the package is ctrl-cmd-f. You can use the filter-lines:toggle command directly through the Command Palette or set a custom keybinding in your keymap.cson.

thank you so much for the tip! I added the following lines to my keymap.cson, now the toggle works

‘ctrl-alt-f’: ‘filter-lines:toggle’

Oops, sorry for the delay here. Thanks @DamnedScholar for the documentation error. Should be fixed in v2.0.0 (using ctrl+cmd+f as intended). You can now also select the filtering result, to copy, using ctrl+cmd+alt+f.

You can also change the number of “context” lines before & after the filtered result.

1 Like