Substring search like sublimes ⌘+d function


#1

Let’s say I’ve highlighted the string “atom” preceding this text.

Atom
atom-editor
editor-atom
atom/editor
editor/atom
atomEditor
editorAtom
alk;dggklakj;latomsdl;kgj;lkgdjs;lkjag

atom’s cmd+d function will only highlight 4 of the 8 occurrences; atom-editor, editor-atom, atom/editor, editor/atom

sublime text’s cmd+d function would highlight every occurrence of string “atom” regardless of case or context. Is there a similar command that already exits in atom? Should this be the default behaviour of cmd+d in atom?


#2

You found a bug! Would you mind opening an issue on the find-and-replace repo? Thanks!


#3

Actually I quite like this ‘feature’ - it kinda makes sense: if you highlight the ‘subword’ variant, you get all occurrences selected, but if you select the ‘word’ variant, you only get ones that act the same (i.e. are word-like).

Makes sense to me, and was a bit of a bug bear of mine with Sublime. And if you want to get all based on a word, you can still use find.


#4

Sublime Text 3 behaves differently based on whether you selected text before the first cmd-d or not.

If you use cmd-d to select the initial word, it will only select complete words - this is handy for selecting occurrences of a variable, for example.

If you made the selection manually and then pressed cmd-d it would then select any occurrence of the selection, even in the middle of a word. This is handy when renaming multiple variables that have a common prefix or suffix, etc.


#5

True - that’s pretty much the same distinction in Atom, except that with Sublime, once you have a selection, CMD+D selects the next thing that matches regardless of word/subword state, whereas Atom keeps making the word/subword distinction. I personally prefer Atom’s take.


#6

I think you might have it backwards. I just tested again with the example data from the post to be sure:

  • In Sublime I use cmd-d to select to select “Atom”. Subsequent presses select all instances of the word “atom” but nothing that is part of a word such as “atomEditor”.
  • In Sublime I manually select “Atom” before pressing cmd-d. Subsequent presses select all instances of the text “atom”, even in “atomEditor”.
  • In Atom, regardless of whether I manually select or not, it always only selects words.

(Also it seems to be case-senstive and Sublime is not, but that could be settings I left on or something.)

I’m not trying to be argumentative, I just don’t want the developers who eventually read this to think it is working as requested and not at least try it themselves.


#7

Don’t worry - it’s good to hammer out the details. Yeah I think we’re both right - what I meant was that in Atom, if the thing that is selected is a sub-word it’s in sub-word mode. If it’s a word it’s in word mode. Regardless of how the word is selected (double-click, cmd+d, click+drag, etc.). In Sublime you’re saying that if you double click or use cmd+d it’s in word mode, if you click and drag it’s in sub-word mode. So the context of word vs. sub-word mode is different between the two in that in Atom it’s based on the wordness (yes I made that up) of the original selection whereas in Sublime it’s based on the wordness of the method of selection. Is that correct as you see it?

If so, I still personally prefer the Atom way, but I completely hear what you’re saying, and it wouldn’t be the end of the world for me if Atom changed to the Sublime way (that said it may be beneficial for Atom to have some differences to Sublime :wink:).


#8

Now I see what you mean.

One benefit of the Sublime way is that you can start on a plain word like “atom” and still select sub words. However, as has become obvious in this post, using the initial selection *mechanism" like Sublime does can be very confusing so Atom’s behavior seems better there.

Another approach would be to have another button on the find (with an easy to use key binding once the Find pane is open), similar to emacs. There is already buttons for regex and case-sensitive – maybe one for ‘whole word only’ would be handy.


#9

Is it possible that when you select a word (either by cmd+d or by click) then Atom will automatically highlight all the occurrences of the same word in the current file?


#10

I think you’re looking for ⌃⌘G, aka Find ⟶ Select All. You don’t actually need a selection for this, but if you do it works with that too.


#11

Thanks.

Indeed it selects every occurrence but I just noticed an issue with the command: when I do a Select All and there are occurrences below the cursor and then I press Esc (to deselect) it moves the cursor after the last occurrence.

Isn’t it supposed to keep the cursor where it was in the first place when the Select All was performed?

I just checked with Sublime and it keeps the cursor at the original position.


#12

There’s also the highlight-selected package and its minimap binding that does exactly that :).


#13

Ahh I didn’t realise this was being asked for - I thought @luciancancescu wanted to select the occurrences rather than simply highlight them in a pointing-them-out sense.

That highlight-selected package is pretty cool. Downloaded.


#14

Yeah, I meant highlighting. Is it just me or the plugin does not work with the latest version of Atom? I’m using 0.94, the plugin is installed and double-clicking on a word (like it states in the readme) does not highlight the other occurrences.


#15

Actually I use it both on a 0.94 at work and a build from source at home and it works fine. Have you an error in the console when opening the editor ?


#16

No errors in the console, and the plugin simply did nothing.

I deleted ~/.atom and restarted the editor, reinstalled the highlight_selected plugin and it started to work. It must have been some conflict with other plugins or some old caches from previous updates.

So this is what I was looking for. Thanks @abe and @alexrussell for your answers. :wink: