簡體   English   中英

Vim:“f”在無法找到角色時會破壞我的映射

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

我有一個(相當粗略的)映射我想用於Jade文件。

nmap <leader>jc ^f)Wc$

應該轉變這些例子

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

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

其中_是插入模式下的光標。

但是,當f找不到時)它停止運行序列中的剩余動作。 有沒有辦法我可以強迫它繼續執行命令序列?

您似乎需要重新考慮一下您的問題。

事實上,您的映射非常具體,它說:

  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

jump to the first closing parenthese on the line部分jump to the first closing parenthese on the line是問題所在。 這意味着您的整個映射取決於當前行中是否存在右括號。

您可以將該映射保持在當前狀態,並在沒有意義的情況下停止使用它(當前行上沒有關閉括號時),或者重新定義問題以便通過通用解決方案解決問題。

可能涉及一些腳本的通用解決方案。 或不。

編輯

這是當前宏背后的邏輯:

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

事實證明:

foo( bar, baz ) lorem ipsum

成:

foo( bar, baz ) |

而且顯然在下面的行上什么也沒做:

foo = { "bar" : "baz" }

宏的行為既一致又可預測。 它適用於特定情況,並且不會在輕微或完全不同的情況下起作用。

這就是你希望它如何工作:

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

這就像......放置一個在函數中間不執行任何操作的條件。

它會變成:

foo( bar, baz ) lorem ipsum

成:

foo( bar, baz ) |

和:

foo = { "bar" : "baz" }

成:

foo |

這會使宏的行為不一致:它會起作用,並且對任何文本行都有很大的影響。 不知何故,我認為這不是你想要的。

對於記錄, :^f)Wc$不“繼續”。 光標停留在該行的第一個可打印字符上並停在那里,這正是nmap <leader>jc ^f)Wc$作用。

實際上,你要求像/)\\{-,1}這樣的東西匹配零或1 ) 但我對這種事情的用處有嚴重懷疑。

你的初始宏開始是錯誤的,因為它似乎根本沒有映射你似乎試圖解決的問題。

您最終給出的兩個示例可以使用相同的命令完成:

^WC

羅曼爾已經試圖說服你,你的方法有一些缺陷。

無論如何,如果您希望在命令失敗后繼續宏,則必須將正常模式命令序列拆分為Ex命令(使用:normal ),因為這些命令不會中止:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM