How do I run a vim script that alters the current buffer?
- by Dan
I'm trying to write a beautify.vim script that makes C-like code adhere to a standard that I can easily read.
My file contains only substitution commands that all begin with %s/...
However, when I try to run the script with my file open, in the manner :source beautify.vim, or :runtime beautify.vim, it runs but all the substitute commands state that their pattern wasn't found (patterns were tested by entering them manually and should work).
Is there some way to make vim run the commands in the context of the current buffer?
beautify.vim:
" add spaces before open braces
sil! :%s/\%>1c>\s\@<!{/ {/g
" beautify for
sil! :%s/for *( *\([^;]*\) *; *\([^;]*\) *; *\([^;]*\) *)/for (\1; \2; \3)/
" add spaces after commas
sil! :%s/,\s\@!/, /g
In my tests the first :s command should match (it matches when applied manually).