Issue #327 resolved

'gn' motion not implemented yet

Bailey Ling
created an issue

emacs 24.3.1 evil 1123

the 'gn' motion is one of the newer ones added, i think around 7.3.500. it remembers the last searched pattern. so if you /foo<cr>. you can then cgnbar<esc> and it will search until the next foo and replace it with bar. and since this is repeatable, you can hit . and it will search/replace the next foo.

Comments (18)

  1. Bailey Ling reporter

    the vim documentation is actually pretty scarce on this, but you can find mention of it in the motion.txt document.

    "Also see gn and gN, operating on the last search pattern."

    and

    "dgn 'delete the next search pattern match'"

  2. Frank Fischer repo owner
    • changed status to open

    As long as there's not reasonable official documentation and my vim does not support the command (so I can test), I would prefer to not include these commands. However, I've prepared a patch you can test.

  3. Bailey Ling reporter

    after applying your patch it's not quite working for me. i've attached a gif of how the gn motion can be used with c and d operators, as well as repeating. i hope that makes it a bit clearer how the motion is used.

    i also did a search of the vim docs, and it looks like the visual.txt document contains the most information about gn.

    thanks.

  4. Bailey Ling reporter

    ok, so seems like the major difference between vim and evil is that vim will change the word under the cursor, so i did a minor hack to make it work over here:

    (evil-define-text-object evil-next-match (count &optional beg end type)
      "Select next match."
      (unless (eq evil-search-module 'evil-search)
        (error "next-match text objects only work with Evil search module."))
      (evil-ex-search-previous 1)
      (evil-ex-search-next count)
      (list evil-ex-search-match-beg evil-ex-search-match-end))
    

    undoing isn't exactly the same yet (on some occasions it takes 2 undoes when it should be one), and the highlighting gets out of sync, but i can live with that for now. thanks.

  5. arianaut

    I would like to add my support for this feature, as a vimmer transitioning to emacs.

    vim emphasizes commands and motions. You press a key for a command, then a motion for what to operate on. Then, these command snippets can be easily repeated. gn and gN allow operating on search/regex matches instead of simple motions like w. The lack of this feature was painful in vim as well (there have been quite a few hackish workarounds), and its recent addition was met with rejoicing.

    Here's the relevant vim documentation on gn and gN for reference:

                                    *gn* *v_gn*
    gn          Search forward for the last used search pattern, like
                with `n`, and start Visual mode to select the match.
                If the cursor is on the match, visually selects it.
                If an operator is pending, operates on the match.
                E.g., "dgn" deletes the text of the next match.
                If Visual mode is active, extends the selection
                until the end of the next match.
    
                                    *gN* *v_gN*
    gN          Like |gn| but searches backward, like with `N`.
    
  6. Bailey Ling reporter

    FWIW, i've been using the snippet since it was revealed until now and it continues to work great. also, it seems that some other upstream fixes have addressed the undo discrepancy, so now both evil and vim take the same number of undos to get back to the same place. the gUgn example from vimcasts works as well.

  7. Bailey Ling reporter
    • changed status to open

    just tested on a 'make terminal' off of the tip.

    create a new file:

    .
    hello
    hello
    hello
    hello
    
    1. go to line 1 and search for 'hel', enter.
    2. cgnfoo, esc
    3. .

    rather than searching to the next and replacing, it will actually go to the last line. if you repeat again it will zig zag back to the 2nd hello and replace it.

  8. Frank Fischer repo owner

    guard evil-next-match against search wrap (fix #327)

    evil-next-match has to do a backward search first in order to determine whether point is on a match, currently. However, this search must not wrap at the buffer boundaries.

    → <<cset 637573c60835>>

  9. Michael Hauser-Raspe

    Having had a look at this I've hit into: (error "next-match text objects only work with Evil search module."). looking at my evil-search-module value I have isearch. I can not find this anywhere in my configuration files, do you need to set up evil-search especially?

  10. Frank Fischer repo owner

    guard evil-next-match against search wrap (fix #327)

    evil-next-match has to do a backward search first in order to determine whether point is on a match, currently. However, this search must not wrap at the buffer boundaries.

    → <<cset 637573c60835>>

  11. Log in to comment