如何在Vim中为:tabedit命令指定多个+cmd参数?
+cmd arguments with Vim's :tabedit command Great question! I've run into this exact issue before—Vim's :tabedit command only natively accepts a single +{command} parameter, so your initial attempt with multiple + flags won't work (Vim will treat those extra +star/+set dir=/ bits as filenames, which is definitely not what you want).
Luckily, there's a straightforward way to chain multiple commands together. Here's how to make your desired workflow work:
Method 1: Chain commands with | (or <Bar>)
You can combine all your desired commands into a single + argument using the command separator |. Since Vim might try to interpret | as a separator for the current command line (instead of passing it to the :tabedit context), you have two solid options:
Option A: Wrap the command chain in quotes
This keeps Vim from parsing the | early:
:tabedit filename +"norm Go | norm * | set dir=/"
Option B: Use <Bar> instead of |
<Bar> is Vim's internal alias for the command separator, so it doesn't need escaping:
:tabedit filename +norm Go <Bar> norm * <Bar> set dir=/
A quick note: The star you mentioned refers to Vim's normal-mode * command (search for the current word). To execute normal-mode commands from the Ex command line (which is what +cmd uses), you need to wrap them in :norm (short for :normal).
Method 2: Use a custom function (for complex workflows)
If you have a longer list of commands or want to reuse this setup later, defining a small function is cleaner and more maintainable:
" First, define the function in your vimrc or current session function! TabEditInitialSetup() " Append a new line at the end of the file norm Go " Search for the current word (like hitting * in normal mode) norm * " Set the working directory to root set dir=/ endfunction " Then call it with tabedit :tabedit filename +call TabEditInitialSetup()
Why your original command didn't work
Just to clarify: When you run :tabedit +norm\ Go +star +set\ dir=/ filename, Vim interprets +norm\ Go as the single command argument, and then treats +star, +set\ dir=/, and filename as three separate files to try to open in tabs. That's why you were getting unexpected behavior!
内容的提问来源于stack exchange,提问作者elig

