如何替換 git rebase 檔案中的單字

如何替換 git rebase 檔案中的單字

我正在嘗試設定可視模式映射,以便在進行互動式變基(例如git rebase --interactive upstream/master)時快速編輯 git 生成的變基列表。在這種情況下,您將看到一個如下所示的文字檔案:

pick 12345678 commit message 1
pick 23456789 commit message 2
pick 34567890 commit message 3

我想做的是選擇<c-v>我想要切換到另一種變基方法的行,例如,使用該行的第一個單字<localleader>f從 更改為pickfixup我想讓它具有一定的容錯能力,因此它不會對其他行(例如註解和空白行)執行此操作。

我嘗試做的是使用:substitute正規表示式群組執行 a 來僅拾取有效單字:(pick|reword|edit|squash|fixup|exec|drop)。這是我目前擁有的.vimrc

autocmd FileType gitrebase vnoremap <buffer> <localleader>p :s/^\(pick\|reword\|edit\|squash\|fixup\|exec\|drop\)/pick/<cr>
autocmd FileType gitrebase vnoremap <buffer> <localleader>r :s/^\(pick\|reword\|edit\|squash\|fixup\|exec\|drop\)/reword/<cr>
autocmd FileType gitrebase vnoremap <buffer> <localleader>e :s/^\(pick\|reword\|edit\|squash\|fixup\|exec\|drop\)/edit/<cr>
autocmd FileType gitrebase vnoremap <buffer> <localleader>s :s/^\(pick\|reword\|edit\|squash\|fixup\|exec\|drop\)/squash/<cr>
autocmd FileType gitrebase vnoremap <buffer> <localleader>f :s/^\(pick\|reword\|edit\|squash\|fixup\|exec\|drop\)/fixup/<cr>
autocmd FileType gitrebase vnoremap <buffer> <localleader>x :s/^\(pick\|reword\|edit\|squash\|fixup\|exec\|drop\)/exec/<cr>
autocmd FileType gitrebase vnoremap <buffer> <localleader>d :s/^\(pick\|reword\|edit\|squash\|fixup\|exec\|drop\)/drop/<cr>

不幸的是,正規表示式不符合任何內容。我嘗試\=在模式末尾添加 a 以匹配組中任何單字的 0 或 1,並且它會在要替換的單字之前添加替換內容。

我究竟做錯了什麼?

答案1

這是一件逃避的事。要么總是使用\\|代替\|或嘗試

autocmd FileType gitrebase vnoremap <buffer> <localleader>p :s/\v^(pick\|reword\|edit\|squash\|fixup\|exec\|drop)/pick/<cr>

相關內容