I've been trying to write a vimscript that reads the output of a shell command (figlet
) and comments every line of it. I want to use the &commentstring
variable since I want this script to work with any filetype.
So far I have achieved only inconsistent results. Here is and example:
- Prompt the user to enter his/her initials and pass that to the shell command
figlet
to print ascii art at the top of the file:
let g:initials = input('Authors initials: ')
execute '0r! figlet -W ' . toupper(g:initials)
The output is the following (e.g. g:initials = JT):
_ _____
| | |_ _|
_ | | | |
| |_| | | |
\___/ |_|
- Get the amount of lines of the output:
let s:line_count = system("figlet -W " . g:signit_initials . "| wc -l")
- Use the vim function
substitute()
to comment every line:
let s:i = 1
while s:i < s:line_count
execute 'silent' . s:i . 's/^.*$/\=substitute(&commentstring, "%s", "\t" . getline(".") . "\t", "")'
let s:i += 1
endwhile
The output is the following:
/* _ _____ */
/* | | |_ _|*/
/* _ | | | | */
/* | |_| | | | */
/* ___/ |_| */
As you can see, it works well until the last line and I dont understand why. Maybe there is a better way to aproach this. Anyways, I would really appriciate if someone would provide me with some help on how to solve this little issue.