1

Yeah... let me explain.

So I have this text that has hyphens on some words at the end of the line to join words, like this:

hello, my na- 
me is bruh... 

This is a scanned book with OCR, and on every line there's a trailing space.

So, what I wanna do is make a shortcut, or map a list of commands (normal mode commands? idk how they're called) to:

  • search - \n (hyphen space newline) and then
  • do xxgJ (delete two characters [the selected by the search, and the trailing space] and join the line without adding a space).

This:

map <C-`> /- \n<CR><bar>xxgJ

I'm aware I can also do 2x instead of xx.

And, it didn't work.

Now, maybe x or dl, same thing according to :help x, doesn't work when mapping it, like sort of how you have to use \r when replacing instead of \n (weird btw...).

But I don't know.

In normal mode they work as expected.

/- \n
<hit enter>
xxgJ

But when mapping them, the part that's supossed to delete the character under the cursor (x or dl) doesn't work.

Please help. And sorry for the humongous post.

2 Answers 2

1

I would do:

map <C-`> /- \n<CR>xxgJ

The right hand side term of a mapping is like a macro, a bare list of character that will be passed to Vim.

Since you have no need to type | to execute your action there are no need to insert <bar>.

5
  • I CANNOT BELIVE THE ANSWER WAS THAT SIMPLE... do you know why this is? Thank you very much.
    – IBM
    Feb 9, 2023 at 19:22
  • 1
    The mapping string is executed as is. You would not need to type | and so you would not need to insert <bar> :-) Feb 9, 2023 at 19:25
  • Thanks for the feedback :-) Feb 9, 2023 at 19:25
  • 1
    @VivianDeSmedt Perhaps add the explanation about why <bar> is incorrect in your answer.
    – filbranden
    Feb 9, 2023 at 19:33
  • Also, why does \n not work when replacing? Why do we have to use \r? It's an unrelated question.
    – IBM
    Feb 13, 2023 at 13:05
1

Another possible solution is to use the :s command to handle these cases, you can match the - \n snippet and just replace it by an empty string, which will also end up joining lines (since you're replacing the line break with an empty string.)

:%s/- \n//
1
  • 1
    Thank you! This is actually a better wway to do it...
    – IBM
    Feb 13, 2023 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.