helix
helix copied to clipboard
Search and select in case sensitive mode and whole-word mode
Features I use the most in VS Code are:
- Non-regex search/replace with switchable "match case" and "match whole word" (switched with Alt+C and Alt+W)
- "Change all occurrences" of the selected text (a cursor will be placed at each occurrence), with Alt+C and Alt+W working here as well
- Change all occurrences of the word at the cursor: performs (2) on the word at the cursor with "match case" and "match whole word" enabled.
- Perform any of the above inside the selected region.
The most frequent thing I'm achieving with these is renaming stuff either in the whole file or inside a specific function.
I couldn't find how any of the above can be achieved without typing some regex. (which is a complete deal-breaker)
I would imagine this could be done in helix in a similar fashion to VS Code. Or any other way, if these actions can be done with same efficiency.
If there's no place for such functionality, I guess this editor is not for me and the issue can be closed.
You may already know this, but regarding 1), 2) and 4), usually the workflow goes something like this:
- You select the range you want to search and replace in (
%
for the whole document,maf
for the current function,map
for the current paragraph etc.). -
s
to split the selection based on a regex - Once you confirm, you can
c
change orr
replace each occurrence,a
append to it, etc.
I understand that regex can be a bit more unwieldy than plain text search, but I've found that I rarely use characters that I need to escape so usually there's little or no difference. I think it's possible to integrate a non-regex search but I'm not sure if it's worth the hassle and added complexity. Could you elaborate why it's a "deal-breaker" to you?
If you do want to explicitly enable or disable case sensitivity you can do so by prefixing your search with (?i)
(case insensitive) or (?-i)
(case sensitive). The default is using smart case.
Shortcuts for case sensitivity / whole word only sound interesting but probably require larger reworks to the prompt, and I don't think I would end up using these personally.
Regarding 3), you can select the word under the cursor (miw
/miW
), yank it to the search register ("/y
) and then perform the search and replace steps I outlined above.
If you need these functions very often and find the key sequences are too long to type repeatedly, you could add custom key binds that perform them.
Also as a side note, if you do have a language server set up, I recommend using the LSP's rename function when possible (Space r).
@CptPotato
Could you elaborate why it's a "deal-breaker" to you?
Because I constantly switch case sensitivity and whole word matching, when viewing/editing large code bases or any serious projects. Typing out regex for something that is so simple and I constantly use makes no sense to me. Especially, if it just works with another editor.
you could add custom key binds that perform them
Do you mean adding three additional shortcuts? (Each entering select_regex
with needed regex prefix)
(with default set to case insensitive)
[keys.normal]
# alternative select
A-s = { c = <sensitive>, w = <word>, s = <sensitive+word> }
Is there a prefix-only regex sequence for matching words("whole word")? Otherwise, I don't see how it can be done.
Trying to add custom mapping I had the following problems:
- Can't set
smart-case
for theselect_regex
command? - How to specify text to be typed out (pass the needed regex prefix to
select_regex
)?
I've found that I rarely use characters that I need to escape
For me it's quite often(often enough) that I perform search/replace with such characters.
But tbh, helix can't replace VS Code for me, since it works on a file, not on a directory. Search/replace on the whole directory, with the search window containing all the matches, is something I use probably more than in-file search/replace (with case/word toggle as important).
@amfogor Btw, as a current workaround, you can do dir-wide search/repacements in a shell by doing ambr FOO BAR
with amber. Another alternative is fastmod. I mostly use amber, it has the option to replace all automatically or ask you for every match.
I agree it would be very useful to have a faster way to enable case-sensitivity than having to type (?-i)
in front of a search term (maybe by pressing Ctrl+i after /
or s
?), and disabling regex for search terms would also be nice (maybe with (?#)search-term
or something like that which doesn't already have a meaning in regex.
Then one can just type (?#)
and then paste from the system clipboard to search something verbatim which might contain regex symbols. Or maybe with Ctrl+?
after /
or s
.
Btw, how to yank from Helix's internal clipboard after entering (?-i)
or anything else in the search field? Is the only workaround to yank it to the system's clipboard before typing /
or s
?
Another thing I noticed, when pressing /
it shows a list of previously entered terms to choose from, but not when pressing s
!
Any idea why?
(And when I press Ctrl+r after /
or s
, it shows a list of different terms, not sure what that is...)
Also, there is no way to select & cut/replace within this search field (nor when editing a path in the file picker) :/
+1 for non-regex search. My search strings often contain (
, [
, +
, etc and having to backslash them all out gets annoying.
A config option to set the search method to either regex or raw text would be nice. Or a separate command for raw text search that can be bound to something like Alt /
.
The ability to easily switch between case-sensitive and whole-word matching is something I sorely miss in Helix. Multi-cursor editing based on those switches is something I do very frequently.
A config option to set the search method to either regex or raw text would be nice. Or a separate command for raw text search that can be bound to something like
Alt /
.
I think this should be a separate binding, that way you can use both without needing to set a config every time.
Any possible workarounds for non-regex search? It would be cool to have search_raw
command.