0

I'm trying to set up visual mode mappings for quickly editing git's generated rebase list from when doing interactive rebase (e.g. git rebase --interactive upstream/master). In such you're presented with a text file that looks like this:

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

What I'd like to do is to do <c-v> and select the lines I'd like to switch over to another rebase method, e.g. use <localleader>f to change from pick to fixup in the first word of the line. I'd like to make this a bit fault tolerant, so it doesn't do this for other lines, like comments and empty lines.

What I've tried doing is to do a :substitute with a regexp group to only pick up on valid words: (pick|reword|edit|squash|fixup|exec|drop). Here is what I currently have in .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>

Unfortunately the regexes don't match anything. I tried adding a \= at the end of the pattern to match 0 or 1 of any word in the group and it adds the replacement before the word it is supposed to substitute.

What am I doing wrong?

  • You're re-inventing fugitive. – Satō Katsura May 24 '17 at 11:32
  • Does the match work in interactive mode? Magic setting? – Philippos May 24 '17 at 11:35
  • @SatoKatsura I'm not sure what you mean. Does fugitive handle gitrebase? A cursory look on the README tells me nothing of this. – Spoike May 24 '17 at 11:53
  • 1
    @Philippos I tried doing it manually in gitrebase file like this :s/^\(pick\|reword\)/fixup/ and that works. The mapping does not however and I get a Pattern not found error. – Spoike May 24 '17 at 11:55
  • @Spoike IIRC this exact use is shown in one of Drew Neil's screencasts. – Satō Katsura May 24 '17 at 12:03
1

It's an escaping thing. Either always use \\| instead of \| or try

autocmd FileType gitrebase vnoremap <buffer> <localleader>p :s/\v^(pick\|reword\|edit\|squash\|fixup\|exec\|drop)/pick/<cr>
  • The | is special in command-line mode to combine multiple commands in one line, so you have to escape it. So your backslash is eaten as escape by the command-line and is missing in your sed – Philippos May 24 '17 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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