Reputation: 4437
I am using the substitute()
method to script Vim so that code folds look different.
Code in ~/.vimrc
is as follows:
function! FoldText()
let width = winwidth(0) - &foldcolumn - (&number ? 4 : 0)
let first_line = getline(v:foldstart) . " ..."
let edited_line = subsitute(first_line, "{ \.\.\.", "{ ... }", "")
let expansion_string = repeat(" ", width)
return edited_line . expansion_string
endfunction
set foldtext=FoldText()
The problem is that this doesn’t work, I just get this for each fold:
0-------------------------------------------------------------------------------
If I take out the edited_line
and return first_line . expansion_text
the first part works:
class Whatever { ...
But for purely aesthetic reasons I’d like folds which start with a brace to show the brace like so:
class Whatever { ... }
I’ve not used VimScript much, and this is mostly pinched from this site, so I expect I’m missing something basic. Thanks!
Upvotes: 0
Views: 44
Reputation: 172570
The 0
comes from the typo in your function name. When Vim evaluates the 'foldtext'
function, it apparently suppresses errors (which would overwhelm the editor), so that undefined function subsitute()
returns the default value 0
.
Additionally, (though it doesn't cause harm here), you should escape the .
in the pattern, as this specifies a regular expression, not literal text; either escape them individually ({ \.\.\.
), or by switching to very nomagic:
let edited_line = substitute(first_line, "\V{ ...", "{ ... }", "")
Upvotes: 3
Reputation: 196546
.
means "any character". You must escape it to match an actual .
:
let edited_line = substitute(first_line, "{ \.\.\.", "{ ... }", "")
Note that you could avoid that substitution with something like:
return getline(v:foldstart) . " ... }" . repeat(" ", width)
Upvotes: 3