7

I have a file with the name First.txt, which I opened that by vim. Now I want to go another file which its name is Second.txt .

  1. How to open Second.txt while I do not close First.txt.

  2. How to switch between them First.txt and Second.txt on the vim?

2 Answers 2

20

You can open a different file in vim with the :o command, like so:

:o /file/To/Open  

This closes your current file and opens /file/To/Open.

As of vim 7.0 you can use the tabs feature to open multiple files at once. Use vim's :tabnew command to open a new tab:

:tabnew [filename]  

Where [filename] is an optional file to open in the new tab.

To switch between tabs use any of the following commands:

  • :tabn - switches to the next tab.

  • :tabp - switches to the previous tab.

  • gt - toggles through all tabs, from left to right.

  • gT - similar to gt but in opposite direction.

  • :tabfirst - switches to the first tab.

  • :tablast - switches to the last tab.

You can also pass multiple files to vim and have it open them in different tabs like so:

vim -p file1 file2 etc  

To close a tab, use the :tabclose command.

For more tab related info see Vim tips: using tabs on Linux.com.

3
  • 2
    or just :tabe [filename] instead of :tabnew [filename]
    – vusan
    Jul 22, 2015 at 11:03
  • This is a really good answer. Better than what I found elsewhere.
    – don.joey
    Oct 24, 2016 at 16:01
  • 1
    also, you can switch between tabs (when open using :tabnew <file>) using Ctrl+Shift+Up or Down. (easier than enter :command).
    – user697104
    Jun 5, 2017 at 20:23
0

To open a file in a different subdirectory while in a common parent directory you could use:
:tabe ./src/[filename] or :tabnew ./src/[filename]

In my case I was in current directory lua-c, I had a /scripts subdirectory for *.lua scripts and a /src subdirectory for *.c source code. One file, in this scenario the *.lua file, was already open.

It's so simple in vi/vim to open and close files from the same directory that I put up with the minor inconvenience of only having one file open. When I started to use files in different folders, failing to remember their locations became very painful.

2
  • Who is a Udemy instructor, what is a Lua class and how is this important to your answer? Apr 12, 2021 at 2:02
  • I removed a reference to the online education provider's programming class.
    – Eric H.
    Apr 13, 2021 at 3:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .