Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find next occurrence of selected text? #2661

Open
tbone2k-git opened this issue Dec 10, 2022 · 11 comments
Open

Find next occurrence of selected text? #2661

tbone2k-git opened this issue Dec 10, 2022 · 11 comments

Comments

@tbone2k-git
Copy link

I tried micro and I think like it - especially for using "today's standards" of key bindings.
It makes switching editors, environments and operating systems a lot easier. o)

On thing I missed:
How do you search/find text, which is currently selected (to easily find next occurrence e.g.)?
Is this not possible yet? Is there a key binding I can add (I did not find any) - any hints appreciated! o)

Thank you! o)

@zyedidia
Copy link
Owner

If you open a search prompt, the selected text should automatically appear so you can press enter to automatically search, and then press Ctrl-n to find the next occurrence. Not the most efficient -- it's probably possible to write a small plugin that you can bind to a keybinding that automatically does it.

@tbone2k-git
Copy link
Author

I select text and press CTRL-F, the search prompt appears and shows: "Find (regex):", but my selected text is not there.
I use micro v2.0.8 installed from Debian stable repos, maybe I shall try a more recent version to get what you describe.
It's not exactly what I looked for, but sounds better than having to always retype or copy/paste into the find prompt.

Thank you! o)

@dmaluka
Copy link
Collaborator

dmaluka commented Dec 11, 2022

@tbone2k-git Yes, v2.0.8 is pretty old, the feature was added in v2.0.10.

@zyedidia Actually there already is a plugin https://github.com/dmaluka/micro-search allowing more efficient searching for the selected text with a single key press. Also it allows searching for the word under cursor with a single keypress, without the need to select this word.

Moreover, it also allows searching for the word with a mouse click on this word, i.e. even without the need to position the cursor before searching. But this feature depends on the PR #2605 which is still not merged.

@tbone2k-git
Copy link
Author

@dmaluka
It looks like you are the author of the plugin? I tried to install it, the plugin is not official yet it seems? I downloaded the file search.lua, put it into ~/.config/micro/plug/search, even created a repo.json but "nope"..

micro -plugin install search
Unknown plugin "search"

Anyway, thank you! It's good to have learned about the plugins in general! o)

@dmaluka
Copy link
Collaborator

dmaluka commented Dec 11, 2022

Yep, it cannot be installed officially yet, but if you put search.lua into ~/.config/micro/plug/search it means you effectively "installed" it, i.e. it should already work for you. (The search-on-right-click feature will not work though, it requires recompiling micro with #2605 pulled, basically that's the reason I haven't made the plugin official yet. But search via Alt-s key press should work.)

@tbone2k-git
Copy link
Author

tbone2k-git commented Dec 14, 2022

Hey! o) I got it working now, I had the actual lua file called "micro-search.lua" due to testing and trying around. Renaming (again) to "search.lua" made it work. Probably the lua file needs to be named like the parent folder, not sure, but great! o) Alt-s is jumping to next occurrence, very perfect.. o) I immediately tried "Shift + Alt +s" to "find previous occurrence".. it does not work, looking at the script.. it's obvious why, it's not implemented yet, maybe I can add this myself. o)

Thank you both for helping! o)

EDIT: I think jumping from current word/selection to next/previous occurrence by a simple key stroke is basic functionality these days, do you know why it is not built in yet? Is it due to special "micro" spirit of micro or something? o)

@dmaluka
Copy link
Collaborator

dmaluka commented Dec 15, 2022

I had the actual lua file called "micro-search.lua" due to testing and trying around. Renaming (again) to "search.lua" made it work. Probably the lua file needs to be named like the parent folder, not sure, but great!

For the record, it should work with "micro-search.lua" as well. The file name can be arbitrary, it only needs to have .lua extension. I've just checked that after renaming to "micro-search.lua" it works for me, both with the newest micro and with 2.0.8, so I have no idea why didn't it work for you.

I immediately tried "Shift + Alt +s" to "find previous occurrence".. it does not work, looking at the script.. it's obvious why, it's not implemented yet, maybe I can add this myself. o)

Yeah, that would be useful.

EDIT: I think jumping from current word/selection to next/previous occurrence by a simple key stroke is basic functionality these days, do you know why it is not built in yet? Is it due to special "micro" spirit of micro or something? o)

No one cared much enough to implement it as built in? Personally I'm fine with having it in a plugin. BTW actually I almost never use this Alt-s search myself anyway, instead I use search via right mouse button click on a word, since it allows me to quickly search for a word no matter where the text cursor currently is.

@tbone2k-git
Copy link
Author

Yeah, obviously no one cared much enough.. o)

Linux tools make me wonder at times, many are super nerdy and have an overkill on switches and functionality, but then there is a lot of stuff with unexpectedly limited functionality - especially on the desktop. Often it seems there is more focus on design and hiding complexity than on being useful and feature rich. I find desktop filemanagers and file pickers especially poor. Often there is no text input to be able to copy/paste a path and everything defaults to showing huge icons e.g., this is quite a contrast to the very functional, but minimal eyecandy on the command line approach. And then there is "sed", which does not know about non-greedy regex in 2022. How is that possible? o) I am no long term linux user, still finding my way and.. anyway.. getting offtopic.. o)

I like to keep my hands on the keyboard when working with text, so I prefer a shortcut right there to navigate/search. Everone is different I guess, which is fine! o) Thank you so much, I learned something new, have a nice weekend! o)

@inv2004
Copy link

inv2004 commented Jan 19, 2023

@tbone2k-git
It is my first days with micro-editor, and its the first issue I found here because I have the same problem: I use * and # a lot in vim

I wrote small plugin which redefines Ctrl-n and Ctrl-p to select current word if nothing selected and use it for search

Probably if would help you too:

I just put it in ~/.config/micro/plug/findword/findword.lua file

local micro = import("micro")
local config = import("micro/config")
local buffer = import("micro/buffer")
local util = import("micro/util")
local regexp = import("regexp")

function findUp(bp)
    if bp.Cursor:HasSelection() then
		bp:FindPrevious()
    else
        if not util.IsWordChar(util.RuneAt(bp.Buf:LineBytes(bp.Cursor.Y), bp.Cursor.X)) then
            return false
        end
        bp.Cursor:SelectWord()
        local search = "\\b"..util.String(bp.Cursor:GetSelection()).."\\b"
		bp:Search(search, true, false)
		bp:FindPrevious()
    end
    return true
end

function findDown(bp)
    if bp.Cursor:HasSelection() then
		bp:FindNext()
    else
        if not util.IsWordChar(util.RuneAt(bp.Buf:LineBytes(bp.Cursor.Y), bp.Cursor.X)) then
            return false
        end
        bp.Cursor:SelectWord()
        local search = "\\b"..util.String(bp.Cursor:GetSelection()).."\\b"
		bp:Search(search, true, true)
    end
    return true
end

function init()
    config.TryBindKey("Ctrl-p", "lua:findword.findUp", true)
    config.TryBindKey("Ctrl-n", "lua:findword.findDown", true)
end

@BrianNormant
Copy link

Hello I have the exact same problem, however Ctrl N and Ctrl P doesn't seem to work for me, no plugin, every settings to default don't work. I tried to make sure they are binded with TryBindKey but it doesn't work either. I don't think its the terminal or the shell because I have the exact same behaviour with cool-retro-term alacritty xterm zsh and bash.
When press Ctrl-f I can find text but pressing ctrl-N doesn't do anything. However pressing Ctrl-P seem to go back in the find history, Maybe that related to some option i don't know about?.

@dmaluka
Copy link
Collaborator

dmaluka commented Feb 28, 2023

@BrianNormant as a first step, you can use raw command to see which escape sequence does micro actually see when you press Ctrl-n.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants