[vim/vim] vim9: Possible documentation error regarding `vim9script` location (Issue #10644)

88 views
Skip to first unread message

Adam Jimerson

unread,
Jul 1, 2022, 9:57:39 AM7/1/22
to vim/vim, Subscribed

Steps to reproduce

  1. Open up a Vim script file (example $HOME/.vimrc or $HOME/.vim/vimrc)
  2. Add vim9script below a comment
  3. Try to launch vim.

Example file:

##############################
#                            #
#      Custom Functions      #
#                            #
##############################

vim9script

command! Cnext try | cbelow | catch | cabove 9999 | catch | endtry
nnoremap <leader>m :Cnext<CR>

# Append modeline after last line in buffer.
# Use substitute() instead of printf() to handle '%%s' modeline in LaTeX
# files.
function! AppendModeline()
let l:modeline = printf(" vim: set ts=%d sw=%d tw=%d %set :",
\ &tabstop, &shiftwidth, &textwidth, &expandtab ? '' : 'no')
let l:modeline = substitute(&commentstring, "%s", l:modeline, "")
call append(line("$"), ['', l:modeline])
endfunction
nnoremap <Leader>sm :call AppendModeline()<CR>

function! AddSubtract(char, back)
let pattern = &nrformats =~ 'alpha' ? '[[:alpha:][:digit:]]' : '[[:digit:]]'
call search(pattern, 'cw' . a:back)
execute 'normal! ' . v:count1 . a:char
silent! call repeat#set(":\<C-u>call AddSubtract('" .a:char. "', '" .a:back. "')\<CR>")
endfunction
nnoremap <silent>         <C-a> :<C-u>call AddSubtract("\<C-a>", '')<CR>
nnoremap <silent> <Leader><C-a> :<C-u>call AddSubtract("\<C-a>", 'b')<CR>
nnoremap <silent>         <C-x> :<C-u>call AddSubtract("\<C-x>", '')<CR>
nnoremap <silent> <Leader><C-x> :<C-u>call AddSubtract("\<C-x>", 'b')<CR>

# vim: set ts=4 sw=4 tw=78 et :

This results in Vim chocking on the use of # instead of " as the comment.

If vim9script is on the first line, like a shebang line in a script, then it works as expected. Yet according to the vim9 help text the vim9script just needs to be the first command.

Expected behaviour

One of the following I guess:

  1. The doc be updated to state vim9script should be treated like a sheban line
  2. Vim 9.x parse both " and # as a comment (probably harder due to legacy vim script support)

Version of Vim

9.0

Environment

Operating System: Arch Linux
Terminal: Konsole
Shell: Elvish

Logs and stack traces

Error detected while processing /home/vendion/.vim/vimrc[34]../home/vendion/.vim/functions.vim:
line    1:
E749: Empty buffer
line    2:
E749: Empty buffer
line    3:
E488: Trailing characters: Custom Functions      #: #      Custom Functions      #
line    4:
E749: Empty buffer
line    5:
E749: Empty buffer
line    7:
E1039: "vim9script" must be the first command in a script
line   12:
E488: Trailing characters: Append modeline after last line in buffer.: # Append modeline after last line in buffer.
line   13:
E488: Trailing characters: Use substitute() instead of printf() to handle '%%s' modeline in LaTeX: # Use substitute() instead of printf() to handle '%%s' modeline in LaTeX
line   14:
E488: Trailing characters: files.: # files.
line   34:
E488: Trailing characters: vim: set ts=4 sw=4 tw=78 et :: # vim: set ts=4 sw=4 tw=78 et :
Press ENTER or type command to continue


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/10644@github.com>

Bram Moolenaar

unread,
Jul 1, 2022, 10:50:24 AM7/1/22
to vim/vim, Subscribed

The help is correct, "vim9script" must be the first command. But until it's found, comments are handled like in legacy script.
That is why it is stated that it's best to put "vim9script" in the very first line.
A comment is not a command, in case that is your confusion.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/10644/1172427923@github.com>

Bram Moolenaar

unread,
Jul 1, 2022, 10:50:30 AM7/1/22
to vim/vim, Subscribed

Closed #10644 as completed.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issue/10644/issue_event/6919134920@github.com>

Adam Jimerson

unread,
Jul 1, 2022, 7:40:14 PM7/1/22
to vim/vim, Subscribed

Maybe I overlooked it, but reading through this https://vimhelp.org/vim9.txt.html I don't see where it says it needs to be the first line only the first command which is what caused my confusion.

The only places where I see "first line" are in the following:

Note th[a](https://vimhelp.org/insert.txt.html#a)t this means that in heredoc the first line cannot start with a bar: 
        var lines =<< trim END
           | this doesn't work
        END
Since [a](https://vimhelp.org/insert.txt.html#a) continuation line cannot be easily recognized the parsing of commands
has been made stricter.  E.g., because of the error in the first line, the
second line [is](https://vimhelp.org/motion.txt.html#is) seen [as](https://vimhelp.org/motion.txt.html#as) [a](https://vimhelp.org/insert.txt.html#a) separate command: 
        popup_create(some invalid expression, {
           exit_cb: Func})

I only see the following:

- a script file where the first command is vim9script

Which doesn't imply that it should be first line and there cannot be any comments before it. It would be clearer if it read
"a script file where the first line is vim9script"


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/10644/1172785510@github.com>

Bram Moolenaar

unread,
Jul 2, 2022, 6:35:58 AM7/2/22
to vim/vim, Subscribed


> Maybe I overlooked it, but reading through this
> https://vimhelp.org/vim9.txt.html I don't see where it says it needs

> to be the first line only the first command which is what caused my
> confusion.

It says "first command". As I mentioned before, you might have been
confused by the term "command". We consider a comment to not be a
command, but you might not know that.


> I only see the following:
>
> ```
> - a script file where the first command is vim9script
> ```
>
> Which doesn't imply that it should be first line and there cannot be any comments before it. It would be clearer if it read
> "a script file where the first line is vim9script"

But that is not correct. As you noticed it also works in a line following
legacy comments.

Anyway, to avoid confusion I can add an exampl:


When starting to read a script file Vim doesn't know it is |Vim9| script until
the `vim9script` command is found. Until that point you would need to use
legacy comments: >
" legacy comment
vim9script
# Vim9 comment

That looks ugly, better put `vim9script` in the very first line: >
vim9script
# Vim9 comment

--
He who laughs last, thinks slowest.

/// Bram Moolenaar -- ***@***.*** -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/10644/1172876532@github.com>

Reply all
Reply to author
Forward
0 new messages