ChatGPT解决这个技术问题 Extra ChatGPT

How to open a new file in vim in a new window

Is there a way to open vim in a new shell window or tab? I'm used to doing $ mate file, which opens the file in a new window.

I prefer having one 'central shell' where I issue commands and edit files in other windows or tabs, as necessary. How do people normally open vim files locally?

You can use ranger, or other terminal-based file managers and open several tabs. Inside the tabs, you can access directly to open files in vim.

x
xavdid

from inside vim, use one of the following

open a new window below the current one:

:new filename.ext

open a new window beside the current one:

:vert new filename.ext

@anu, you can use v, ctrl+v, or V to mark your text on the old file, y to yank it to the "clipboard", and p or P to paste it...
:vert new filename.txt could be shortened to :vnew filename.txt
...or :vsp filename.txt would also open it in a new split.
To switch between the two windows once you've got them is Control + W x2. That's Control + Shift + w (tap w twice with Control and Shift held down).
@DanielPorteous On my Windows 10 PC, Ctrl + wx2 is enough, no Shift is needed.
E
Eliran Malka

You can do so from within vim, using its own windows or tabs.

One way to go is to utilize the built-in file explorer; activate it via :Explore, or :Texplore for a tabbed interface (which I find most comfortable).

:Texplore (and :Sexplore) will also guard you from accidentally exiting the current buffer (editor) on :q once you're inside the explorer.

To toggle between open tabs, use gt or gT (next tab and previous tab, respectively).

See also Using tab pages on the vim wiki.


s
srinivasu u

I use this subtle alias:

alias vim='gnome-terminal -- vim'

-x is deprecated now. We need to use -- instead


I use the following code (based on this answer) in my startup script to allow me to continue using the same terminal window once I open a gnome-terminal with vim. This adds a '&' character to the end of the call to run gnome in the background and suppresses output. "vim_run () { gnome-terminal -- vim $1 &> /dev/null & } alias vim=vim_run"
a
aegis

If you don't mind using gVim, you can launch a single instance, so that when a new file is opened with it it's automatically opened in a new tab in the currently running instance.

to do this you can write: gVim --remote-tab-silent file

You could always make an alias to this command so that you don't have to type so many words. For example I use linux and bash and in my ~/.bashrc file I have:

alias g='gvim --remote-tab-silent'

so instead of doing $ mate file I do: $ g file


Question asks how to do this in vim. See related answer and associated downvotes above.
C
Chris Stryczynski

I'm using the following, though it's hardcoded for gnome-terminal. It also changes the CWD and buffer for vim to be the same as your current buffer and it's directory.

:silent execute '!gnome-terminal -- zsh -i -c "cd ' shellescape(expand("%:h")) '; vim' shellescape(expand("%:p")) '; zsh -i"' <cr>

j
jahroy

Check out gVim. You can launch that in its own window.

gVim makes it really easy to manage multiple open buffers graphically.

You can also do the usual :e to open a new file, CTRL+^ to toggle between buffers, etc...

Another cool feature lets you open a popup window that lists all the buffers you've worked on.

This allows you to switch between open buffers with a single click.

To do this, click on the Buffers menu at the top and click the dotted line with the scissors.

https://i.stack.imgur.com/sckza.png

Otherwise you can just open a new tab from your terminal session and launch vi from there.

You can usually open a new tab from terminal with CTRL+T or CTRL+ALT+T

Once vi is launched, it's easy to open new files and switch between them.


Downvote because the subsequent answer is the way to do this in vim, which was what I was looking for. While handy (and obviously good enough for the asker), gVim is not the same, and many people who find this answer when searching for "how to open a new window in vim" will be looking for the more popular answer.
Perhaps the question title/text should be clarified, then; it doesn't mention gvim or graphical windows, and "window" is a term of art within the context of vim specifically, as is "tab": vimdoc.sourceforge.net/htmldoc/windows.html The "from inside vim" answer is a way to open a new "vim window"; a user who finds this page, having searched "how to open a new vim window", should see that answer.
+1, obviously this is the correct answer to the OPs question and what he was asking. Whether it's correct for what subsequent readers need is not a reason to downvote.
I think original question was ambigious, but it is true that this answer is a little inflexible.