I want to write a zsh vi-mode helper to wrangle complex commands but ran into a snag using \r
in a substitute()
function invocation. It results in \r
replaced with control characters ^M
in output. This works for command mode invocations, both mapping and creating a custom user command.
I've tried using single/double quotes for the {sub} string and surrounding the function declaration with set cpo+=C
and set cpo-=C
and replacing
\r
with ctrl+v ctrl+m (to get ^M
) but no joy.
The snippet below works for <leader>s
, :Split
but not for :Test
" .../after/ftplugin/zsh.vim
"
setlocal ts=2 sw=2 et sts=2 ai si
" For zsh vi-mode command split
" splits a single line command on every -- or -,
" appends \ on every eol
" mnemonic: cs = command split
" mapping shortcut to command works
" 1: Create a map, ask for confirmation
nmap <leader>s :s:\(\s\{1,}\)\(-\+\): \\\r \2:g<CR> | noh | retab
nmap <leader>sq :s:\(\s\{1,}\)\(-\+\): \\\r \2:cg<CR> | noh | retab
" user command works
" 2: Create a User Command
command! -nargs=0 Split s:\(\s\{1,}\)\(-\+\): \\\r\t\t\2:g | noh | retab
command! -nargs=0 Splitq s:\(\s\{1,}\)\(-\+\): \\\r\t\t\2:cg | noh | retab
" 3: VimScript function doesn't work
" Fails with: ls -l -a
" EXPECT:
" ls\
" -l\
" -a
"
" GOT : ls\^M -l\^M -a
command! -nargs=0 Test call SplitCommandLine()
func! SplitCommandLine()
let pat = '\(\s\{1,}\)\(-\+\)'
let sub = '\\\r \2'
let flags = 'g'
let line = getline('.')
call setline('.', substitute(line, pat, sub, flags))
endf