简体   繁体   中英

Vim: “f” breaks my mapping when it can't find a character

I have a (fairly crude) mapping I want to use for Jade files.

nmap <leader>jc ^f)Wc$

should turn these examples

a.classname(href='url', title='title') Click here
p This is a paragraph

into

a.classname(href='url', title='title') _
p _

where _ is the cursor in insert mode.

However, when f can't find ) it ceases to run the remaining actions in the sequence. Is there a way I can force it to continue with the sequence of commands regardless?

You seem to need to rethink your problem a bit.

As it is, your mapping is very specific, it says:

  jump to the first printable character on the line
> jump to the first closing parenthese on the line
  jump to next WORD
  delete from the cursor to the end of the line
  enter insert mode

The jump to the first closing parenthese on the line part is where the problem is. It means that your whole mapping depends on the presence of a closing parenthese in the current line.

Either you keep that mapping in its current state and stop using it where it doesn't make sense (when there's no closing parenthese on the current line) or you redefine your problem so that it can be solved by a generic solution.

A generic solution that may involve a bit of scripting. Or not.

edit

This the logic behind your current macro:

1. jump to a specific "anchor" on the line
            |
            V
2. perform an action on whatever comes after that anchor 
   on that line

It turns:

foo( bar, baz ) lorem ipsum

into:

foo( bar, baz ) |

and rather obviously does nothing on the line below:

foo = { "bar" : "baz" }

The behavior of your macro is both consistent and predictable. It is taylored to a specific situation and doesn't act up in slightly or completely different situations.

This is the how you would like it to work:

1.  try to jump to a specific "anchor" on the line
            |
            V
2a. if success: perform an action on whatever comes after that anchor 
    on that line
            |
            V
2b. if failure: perform an action on whatever comes after that anchor 
    on that line

It's like… placing a conditional that does nothing in the middle of a function.

It would turn:

foo( bar, baz ) lorem ipsum

into:

foo( bar, baz ) |

and:

foo = { "bar" : "baz" }

into:

foo |

This would make the behavior of your macro inconsistent: it would work and have vastly varying effects on any line of text. Somehow I think that's not what you want.

For the record, :^f)Wc$ doesn't "continue". The cursor stays on the first printable character on the line and stops there and this is exactly what nmap <leader>jc ^f)Wc$ does as well.

In practical terms, you are asking for something like /)\\{-,1} which would match zero or 1 ) . But I have serious doubts about the usefulness of such a thing.

Your initial macro is wrong to begin with because it doesn't seem to map at all with the problem you seem to be trying to solve.

The two examples you finally gave can be done with the same command:

^WC

romainl has already tried to convince you that your approach has some flaws.

Anyway, if you want your macro to continue after a command failure, you have to split the sequence of normal mode commands into Ex commands (with :normal ), as these do not abort:

nmap <leader>jc :execute 'normal ^f)'<CR>Wc$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM