当你要使用 vim(或者neovim)来编辑 Svelte 下面的文件时,比如这些文件:
+page.svelte
+layout.svelte$ vim +page.svelte
$ vim "+page.svelte"
$ vim '+page.svelte'
$ vim \+page.svelte
使用上面的命令,你会遇到这个错误:
Error detected while processing command line:
E492: Not an editor command: page.svelte
难搞啊,我教你一个简单方法,使用--
选项就可以了,此选项的意思是,后面的参数都视作文件名:
$ vim -- +page.svelte
具体解释:
The problem you’re encountering is because the +
character is special to Neovim (and Vim) when used as an argument on the command line.
Neovim interprets arguments starting with +
in several ways:
+N
: Go to lineN
after opening the file.+/pattern
: Search forpattern
after opening the file.+command
: Executecommand
after opening the file.
When you type nvim +page.svelte
, Neovim thinks page.svelte
is a command it should execute, and since page.svelte
isn’t a valid Neovim command, it gives you E492: Not an editor command
.
The shell escaping ("
, '
, \
) doesn’t help here because the +
character isn’t special to the shell in this context; it’s special to the nvim program itself. The shell successfully passes +page.svelte
to nvim
, but nvim
then misinterprets it.
The Correct Way
The standard Unix way to tell a command-line program that “all subsequent arguments are filenames, not options or commands” is to use --
.
nvim -- +page.svelte
Explanation:
nvim
: The Neovim executable.--
: This tellsnvim
to stop processing command-line options and treat everything that follows as a filename or argument for a file.+page.svelte
: Now Neovim correctly interprets this as the literal filename you want to open.
Other Ways (Less Direct, but Work)
-
Using
:edit
command:
You can open Neovim first, then use the:edit
command.nvim :edit \+page.svelte
Or directly from the command line:
nvim -c "edit +page.svelte"
(The
-c
flag executes a command after startup.) -
Using
argv
(argument vector) with:edit
:
This is less common for simple cases but demonstrates flexibility.nvim '+page.svelte' --remote-send '<Esc>:edit %<CR>'
(This is overcomplicating it, but shows how you can pass filenames and then manipulate them, but
nvim -- +page.svelte
is far superior for this specific need).
The recommended and simplest solution is nvim -- +page.svelte
.