1219

I've started using Vim to develop Perl scripts and am starting to find it very powerful.

One thing I like is to be able to open multiple files at once with:

vi main.pl maintenance.pl

and then hop between them with:

:n
:prev

and see which file are open with:

:args

And to add a file, I can say:

:n test.pl

which I expect would then be added to my list of files, but instead it wipes out my current file list and when I type :args I only have test.pl open.

So how can I add and remove files in my args list?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
  • 9
    tmux... use vim in each pane – Tommy Sep 02 '16 at 18:00
  • 15
    @Tommy: this way you can't paste yanked/deleted content inbetween the files (vims buffer). If available only a clipboard may help (see accepted answer: https://stackoverflow.com/questions/5532878/how-to-copy-codes-in-vi-to-clipboard) – ChristophS Jun 27 '18 at 12:13
  • 4
    `:N` seems like an easier way to go to the previous file... – Gert van den Berg Nov 16 '18 at 09:55
  • 4
    http://vimcasts.org/categories/the-argument-list/ ? VimCasts are really highly recommended when learning vim. – mcepl Jan 21 '20 at 17:32

29 Answers29

1278

Why not use tabs (introduced in Vim 7)? You can switch between tabs with :tabn and :tabp, With :tabe <filepath> you can add a new tab; and with a regular :q or :wq you close a tab. If you map :tabn and :tabp to your F7/F8 keys you can easily switch between files.

If there are not that many files or you don't have Vim 7 you can also split your screen in multiple files: :sp <filepath>. Then you can switch between splitscreens with Ctrl+W and then an arrow key in the direction you want to move (or instead of arrow keys, w for next and W for previous splitscreen)

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
fijter
  • 17,607
  • 2
  • 25
  • 28
  • 79
    To save and close a tab, you can also use `ZZ` instead of `:wq` (just like would normally save and close) – Andreas Grech May 05 '10 at 14:34
  • 40
    I'm using the vim-gnome package in Ubuntu 10.10, and I can switch between tabs using Ctrl+PageUp and Ctrl+PageDown. I didn't have to configure anything; it was default. – Joey Adams Oct 01 '11 at 02:14
  • 295
    Also, in edit mode `gt` goes to the next tab, and `gT` goes to the previous tab. – Dean Dec 08 '11 at 22:07
  • 43
    You can jump to any tab by using ngt, where n is the index of the tab (beginning with one). I think there's an option that displays the index of each tab near the file name, but I don't know what it is. If anyone knows, I'd love to hear it. – void-pointer Jan 13 '12 at 02:57
  • 1
    CTRL+PageUp/PageDown also works in Debian Squeeze. I didn't know tabs existed by default! /upvote – Esa Sep 14 '12 at 06:25
  • 89
    Note also that you can use the `-p` flag to open multiple files in tabs from the command line. For example, `gvim -p main.pl maintenance.pl` will open these two files in tabs. – Matthew Strawbridge Nov 25 '12 at 10:06
  • 3
    Note: Ctrl+PageUp/PageDown won't work if you have multiple tabs open in Gnome terminal. The terminal tabs take the shortcut. – Chris Hanson Feb 02 '13 at 06:35
  • 9
    vertical split! `:vsp filename` – javier_domenech Jun 13 '13 at 17:43
  • 2
    I mapped `Ctrl-h` to previous tab and `Ctrl-l` to next tab, which goes well with Vim's default mappings of `h` and `l` (to move the cursor left and right): `nnoremap gT` and `nnoremap gt`. – gitaarik Jul 19 '13 at 08:17
  • 10
    @Dean: I think `gt` and `gT` work in escape/command mode, not in *edit* mode. – Nawaz May 06 '14 at 05:34
  • 1
    @JoeyAdams the problem with this package is that it alters some things that you don't expect it to or they work under some very specific conditions. For example in this case Ctrl+Pageup/Down is actually the default for switching tabs in terminal (at least in Ubuntu). If you have multiple tabs in your terminal (quite normal for me) and multiple tabs in Vim, the Ctrl+PageUp/Down will switch between terminal tabs and not Vim tabs! (tested it myself) That's why my advice is to always check the configuration file when you get it like this - out of the box. – rbaleksandar Jun 18 '14 at 09:40
  • 9
    @AndreasGrech `ZZ` is synonymous with `:x`, not `:wq`. Nit-picky, I know, but the difference is important if you ever need to care about last-modified timestamps of files, as `:x` (and `ZZ`) will only modify the file if it changed. `:wq` forces a write. – sanmiguel Mar 12 '15 at 13:30
  • @void-pointer every aspect of what is displayed in terminal vim's tab header is controlled by the `tabline` setting. See `:h setting-tabline` for full details. *NB:* This does not control the GUI tabline. – sanmiguel Mar 12 '15 at 13:35
  • 10
    fyi, [Buffers vs. Tabs](https://joshldavis.com/2014/04/05/vim-tab-madness-buffers-vs-tabs/) – Nikos Alexandris Apr 08 '15 at 14:04
  • 17
    This answer doesn't deserve so many up-votes because it made me (and probably many other people) think that **tab** in [vim](http://www.vim.org/) is the same as tab in any other editor (like [notepad++](https://notepad-plus-plus.org/)). @Nikos Alexandris pointed out nicely what is the difference between tabs and windows. _'Tabs were only designed to let us have different layouts of windows. They aren’t intended to be where an open file lives; an open file is instead loaded into a buffer.'_ – patryk.beza Oct 05 '15 at 14:09
  • 8
    Downvote as Patryk.beza is correct, this isn't the intended function of tabs. – ryanjdillon Nov 30 '15 at 14:13
  • If you're using MacVim you can also switch tabs using Command+{ and Command+} which is used everywhere on OSX to change tabs(Like terminal, Chrome, etc). Just a little tip. – Patrick Bassut Jan 21 '16 at 12:13
  • 2
    How to map :tabn and :tabp with F7/F8 keys? – MuneshSingh Mar 02 '16 at 05:45
  • @patryk.beza I think what the blog [Buffers vs. Tabs](http://joshldavis.com/2014/04/05/vim-tab-madness-buffers-vs-tabs/) is talking about is `Terminal Tab`, not `vim tabs` mentions in this answer. – Ryan Apr 11 '16 at 17:11
  • 3
    @Ryan No. You're wrong. [Buffers vs. Tabs](https://joshldavis.com/2014/04/05/vim-tab-madness-buffers-vs-tabs/) clearly refers to [vim's tabs](http://vim.wikia.com/wiki/Using_tab_pages), **not** [terminal's tabs](https://help.gnome.org/users/gnome-terminal/stable/gs-tabs.html.en). – patryk.beza Apr 12 '16 at 17:17
  • 3
    I believe that opening new buffers/files in tabs is sensible, especially when you have many windows of the same buffer in the current tab. However, it will often be more intuitive to close the current buffer with `:bd` (which also closes the tab/window unless there is a split with another buffer), instead of closing the tab/window (which will leave the buffer open if there is another active tab/window). – joelostblom Apr 17 '16 at 21:45
  • 4
    In summary, [Buffers vs. Tabs](http://joshldavis.com/2014/04/05/vim-tab-madness-buffers-vs-tabs/) was saying that, when using different tabs/split-windows to edit the same file, Vim actually only opens one buffer, causing you actually editing the same file(buffer) on different tabs/split-windows. I don't think this is a problem for me. I will still use tabs and split-windows. – AnnieFromTaiwan Aug 24 '16 at 09:40
  • 7
    This is very bad advice. Tabs are effectively a way to layout buffers. – JohnDoe Sep 24 '16 at 20:00
  • Use : tabn1, :tabn2, :tabn3 ... etc to switch to that particular tab. – Krishnadas PC Oct 05 '16 at 03:00
  • 1
    @munesh You don't need F7/F8 key for doing :tabn and :tabp, just use gt or gT – off99555 Nov 02 '16 at 21:00
  • Iterating between tabs is already mapped to CTRL-PgUp & CTRL-PgDown by default (at least in Ubuntu 16.04) – Moshe Bixenshpaner Dec 03 '16 at 08:37
  • 3
    @munesh Add the lines `map :tabp` and `map :tabn` to your .vimrc file. – RiaanDP Jan 02 '17 at 11:44
  • 2
    Please consider adding a notice indicating that **buffers** should be used instead, and providing a [**link**](https://stackoverflow.com/a/65732/365102) to a more idiomatic answer. – Mateen Ulhaq May 08 '18 at 07:58
  • Set the shortcuts to `` and ` to make them the same as browser tab switching. – Tejas Kale Jan 11 '19 at 17:15
  • 4
    I see many articles and opinions saying that tabs in Vim aren't really tabs, whatever, and yet nobody can come up with an _actual_ reason as to _why_ I can't just use tabs instead of buffers. Sure, buffers are intended for multiple files and tabs are intended for different layouts. Many things are intended for one use and users find that another use is more appropriate for their workflow. What is **wrong** with using tabs for files? – sleighty Apr 20 '19 at 20:32
  • 1
    Also `:tabfind filenam` then `` allows you to find any file with "filenam" in it and open it in new tab. This combined with `:set wildmenu` is pretty nice. – Raekkeri May 02 '19 at 05:34
  • 1
    I wish people would refer to Vim tabs as "**tab pages**" this is what they are called in `:help window` and helps differentiate them from tabs in browsers and other code editors. Having 100+ buffers open in Vim is easy (and what I have daily), having 100+ tabs open in another editor (or 100+ tab pages in Vim) is unworkable from my experience. – icc97 Jul 04 '19 at 09:43
  • In Ubuntu Terminal, is already mapped to change Terminal tabs, but works to change Vim tabs. – alandplm Sep 17 '19 at 16:42
  • I'd suggest to prefer buffers over tabs. Offers much easier and convenient switching IMO. Also keeps the window clutter free. See Sebastian's answer below. – sid-m Apr 05 '20 at 14:29
  • You can also move tabs with the `:tabm` command! Like `:tabm +1` to move a tab one step forward etc. – Sam Aug 19 '20 at 18:19
  • @sleighty I think the main issue is just that when you mix them, it gets weird. Like I didn't know about tabs, and now whenever I do `:bp` it will bounce me between tabs in addition to between buffers on the same screen. And otherwise I think folks are just cranky and criticizing something they're not used to :P All best, Nate – Nathan majicvr.com Feb 18 '23 at 14:39
605

Listing

To see a list of current buffers, I use:

:ls

Opening

To open a new file, I use

:e ../myFile.pl

with enhanced tab completion (put set wildmenu in your .vimrc).

Note: you can also use :find which will search a set of paths for you, but you need to customize those paths first.


Switching

To switch between all open files, I use

:b myfile

with enhanced tab completion (still set wildmenu).

Note: :b# chooses the last visited file, so you can use it to switch quickly between two files.


Using windows

Ctrl-W s and Ctrl-W v to split the current window horizontally and vertically. You can also use :split and :vertical split (:sp and :vs)

Ctrl-W w to switch between open windows, and Ctrl-W h (or j or k or l) to navigate through open windows.

Ctrl-W c to close the current window, and Ctrl-W o to close all windows except the current one.

Starting vim with a -o or -O flag opens each file in its own split.


With all these I don't need tabs in Vim, and my fingers find my buffers, not my eyes.

Note: if you want all files to go to the same instance of Vim, start Vim with the --remote-silent option.

pjz
  • 41,842
  • 6
  • 48
  • 60
Sébastien RoccaSerra
  • 16,731
  • 8
  • 50
  • 54
  • 4
    In case you're playing with multiple buffers, I would recommend [LustyJuggler](http://www.vim.org/scripts/script.php?script_id=2050). – Arun M Nov 06 '10 at 02:50
  • 1
    Thanks for making a very nice quick-reference for the vim commands used most often when working with multiple files. – quanticle Mar 19 '11 at 17:49
  • Or `:b#` where # is the file index in the buffer, not just last visited – Engineer2021 Oct 10 '13 at 15:54
  • 12
    `:b` is a very powerful command because it can accept both buffer numbers and buffer names as arguments. What more? It also supports tab-completion on any part of the filename. Say, you have foo.txt open in buffer 2, you can type `:b 2` or `:b foo.txt` or `:b oo` to edit that file. Yes, the last one would complete 'oo' to 'foo.txt' when you press . – Susam Pal May 01 '14 at 19:45
  • 24
    I have this line in my .vimrc: `nnoremap gb :ls:b`. When I type `gb` in command mode, it lists my open buffers and types `:b `, ready for me to start typing a buffer name/number. – nkorth Feb 27 '15 at 18:21
  • and when using `:ball` it split the view in multiple windows, do you know how to swap from one to another ? – Dimitri Kopriwa Oct 15 '15 at 13:13
  • 1
    +1 for "Using windows". If a file references another file, I open that file in a split window (I continue this process for all the files necessary). I can then quickly look at info I need in one window, while working on another window. Finally when I no longer need to look at a window I remove it. – Tom Jan 18 '17 at 18:32
  • 5
    It's also worth mentioning that `:b` will accept a substring match as long as it's unambiguous. So if you have open files `foo`, `bar`, and `baz`, just `:b z` is enough to switch you to the `baz` buffer, and `:b oo` or `:b o` will take you to the `foo` buffer. – Soren Bjornstad May 05 '18 at 01:39
  • Start vim via `vim`, not `vi`, and add `set nocompatible hidden` to your `.vimrc`. The handling of multiple buffers is limited in compatible mode. – sapanoia Jan 22 '19 at 21:06
  • @Tom that's a great idea! For others with `tags` setup, use `:stjump [name]` to jump to `[name]` and open the new buffer in a horizontal split. This is a more complex version of doing `g Ctrl-]` which jumps to the tag under the cursor, but you can just create a `` shortcut in `.vimrc`. I prefer vertical splits which requires first create a vertical split `:vsp` then jump in the new split via `g Ctrl-]`. – icc97 Jul 04 '19 at 09:32
  • ctrl-^ too will toggle last visited file. – user10439725 Aug 02 '21 at 09:22
252
:ls

for list of open buffers

  • :bp previous buffer
  • :bn next buffer
  • :bn (n a number) move to n'th buffer
  • :b <filename-part> with tab-key providing auto-completion (awesome !!)

In some versions of vim, bn and bp are actually bnext and bprevious respectively. Tab auto-complete is helpful in this case.

Or when you are in normal mode, use ^ to switch to the last file you were working on.

Plus, you can save sessions of vim

:mksession! ~/today.ses

The above command saves the current open file buffers and settings to ~/today.ses. You can load that session by using

vim -S ~/today.ses

No hassle remembering where you left off yesterday. ;)

user664833
  • 18,397
  • 19
  • 91
  • 140
shyam
  • 9,134
  • 4
  • 29
  • 44
  • 2
    go to a specific buffer: N CTRL-^ – Tom Yang Nov 10 '15 at 12:15
  • 8
    @shyam I think I love you! Somehow I never knew for sure about the mksession command, but today out of the blue I decided there MUST be some way to save a list of files to easily open them. This is even better as it even seems to store the complete state of things (window splits, buffer locations, etc). So, thanks! – Kasapo Jun 15 '16 at 19:42
  • Also, use the `"` mark to jump to where you were in the file last time. – Chromium May 28 '18 at 05:15
123

To add to the args list:

:argadd

To delete from the args list:

:argdelete

In your example, you could use :argedit test.pl to add test.pl to the args list and edit the file in one step.

:help args gives much more detail and advanced usage

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
MarkB
  • 5,117
  • 3
  • 22
  • 8
50

I use buffer commands - :bn (next buffer), :bp (previous buffer) :buffers (list open buffers) :b<n> (open buffer n) :bd (delete buffer). :e <filename> will just open into a new buffer.

Andy Whitfield
  • 2,373
  • 2
  • 19
  • 22
47

I think you may be using the wrong command for looking at the list of files that you have open.

Try doing an :ls to see the list of files that you have open and you'll see:

   1 %a   "./checkin.pl"            line 1
  2 #    "./grabakamailogs.pl"     line 1
  3      "./grabwmlogs.pl"         line 0
  etc.

You can then bounce through the files by referring to them by the numbers listed, e.g. :3b

or you can split your screen by entering the number but using sb instead of just b.

As an aside % refers to the file currently visible and # refers to the alternate file.

You can easily toggle between these two files by pressing Ctrl Shift 6

Edit: like :ls you can use :reg to see the current contents of your registers including the 0-9 registers that contain what you've deleted. This is especially useful if you want to reuse some text that you've previously deleted.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Rob Wells
  • 36,220
  • 13
  • 81
  • 146
41

Vim (but not the original Vi!) has tabs which I find (in many contexts) superior to buffers. You can say :tabe [filename] to open a file in a new tab. Cycling between tabs is done by clicking on the tab or by the key combinations [n]gt and gT. Graphical Vim even has graphical tabs.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    Thanks, sounds great, but we unfortunately only have VIM 6.1 installed on the server. – Edward Tanguay Sep 10 '08 at 09:16
  • 1
    Tabs are very handy with wildcards: `vim -p dir/*`. Max tab size is 10, but you can change it in `~/.vimrc` setting `tabpagemax` to some other value. – Campa Jun 18 '13 at 12:26
27

Many answers here! What I use without reinventing the wheel - the most famous plugins (that are not going to die any time soon and are used by many people) to be ultra fast and geeky.

  • ctrlpvim/ctrlp.vim - to find file by name fuzzy search by its location or just its name
  • jlanzarotta/bufexplorer - to browse opened buffers (when you do not remember how many files you opened and modified recently and you do not remember where they are, probably because you searched for them with Ag)
  • rking/ag.vim to search the files with respect to gitignore
  • scrooloose/nerdtree to see the directory structure, lookaround, add/delete/modify files

EDIT: Recently I have been using dyng/ctrlsf.vim to search with contextual view (like Sublime search) and I switched the engine from ag to ripgrep. The performance is outstanding.

EDIT2: Along with CtrlSF you can use mg979/vim-visual-multi, make changes to multiple files at once and then at the end save them in one go.

Petur Subev
  • 19,983
  • 3
  • 52
  • 68
27

Things like :e and :badd will only accept ONE argument, therefore the following will fail

:e foo.txt bar.txt
:e /foo/bar/*.txt
:badd /foo/bar/*

If you want to add multiple files from within vim, use arga[dd]

:arga foo.txt bar.txt
:arga /foo/bar/*.txt
:argadd /foo/bar/*
puk
  • 16,318
  • 29
  • 119
  • 199
26

Some answers in this thread suggest using tabs and others suggest using buffer to accomplish the same thing. Tabs and Buffers are different. I strongly suggest you read this article "Vim Tab madness - Buffers vs Tabs".

Here's a nice summary I pulled from the article:

Summary:

  • A buffer is the in-memory text of a file.
  • A window is a viewport on a buffer.
  • A tab page is a collection of windows.
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Andrew
  • 737
  • 2
  • 8
  • 24
22

To change all buffers to tab view.

 :tab sball

will open all the buffers to tab view. Then we can use any tab related commands

gt or :tabn           "    go to next tab
gT or :tabp or :tabN  "    go to previous tab

details at :help tab-page-commands.

We can instruct vim to open ,as tab view, multiple files by vim -p file1 file2. alias vim='vim -p' will be useful.
The same thing can also be achieved by having following autocommand in ~/.vimrc

 au VimEnter * if !&diff | tab all | tabfirst | endif

Anyway to answer the question: To add to arg list: arga file,

To delete from arg list: argd pattern

More at :help arglist

dlmeetei
  • 9,905
  • 3
  • 31
  • 38
20

When using multiple files in vim, I use these commands mostly (with ~350 files open):

  • :b <partial filename><tab> (jump to a buffer)
  • :bw (buffer wipe, remove a buffer)
  • :e <file path> (edit, open a new buffer>
  • pltags - enable jumping to subroutine/method definitions
Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
zigdon
  • 14,573
  • 6
  • 35
  • 54
  • 2
    350 files!!! That is impressive. how do you find the right buffer to jump to? Do you ever do splits? – Brian O'Dell Apr 07 '14 at 14:48
  • 3
    @BrianO'Dell the `:b ` plus `set wildmenu` in your `.vimrc` works well for when you've got tons of buffers open. Then you just partial match and scroll through the ones it finds. In other decent editors you have fuzzy finding but normally you can't restrict that fuzzy finding to just the files you have open. – icc97 Sep 01 '17 at 09:36
17

You may want to use Vim global marks.

This way you can quickly bounce between files, and even to the marked location in the file. Also, the key commands are short: 'C takes me to the code I'm working with, 'T takes me to the unit test I'm working with.

When you change places, resetting the marks is quick too: mC marks the new code spot, mT marks the new test spot.

glts
  • 21,808
  • 12
  • 73
  • 94
user2179522
  • 171
  • 1
  • 4
12

If using only vim built-in commands, the best one that I ever saw to switch among multiple buffers is this:

nnoremap <Leader>f :set nomore<Bar>:ls<Bar>:set more<CR>:b<Space>

It perfectly combines both :ls and :b commands -- listing all opened buffers and waiting for you to input the command to switch buffer.

Given above mapping in vimrc, once you type <Leader>f,

  • All opened buffers are displayed
  • You can:
    • Type 23 to go to buffer 23,
    • Type # to go to the alternative/MRU buffer,
    • Type partial name of file, then type <Tab>, or <C-i> to autocomplete,
    • Or just <CR> or <Esc> to stay on current buffer

A snapshot of output for the above key mapping is:

:set nomore|:ls|:set more
  1  h    "script.py"    line 1
  2 #h  + "file1.txt"    line 6  -- '#' for alternative buffer
  3 %a    "README.md"    line 17 -- '%' for current buffer
  4       "file3.txt"    line 0  -- line 0 for hasn't switched to
  5     + "/etc/passwd"  line 42 -- '+' for modified
:b '<Cursor> here'

In the above snapshot:

  • Second column: %a for current, h for hidden, # for previous, empty for hasn't been switched to.
  • Third column: + for modified.

Also, I strongly suggest set hidden. See :help 'hidden'.

K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
qeatzy
  • 1,363
  • 14
  • 21
  • This is a great idea! I've been using `:b` and `:ls` a lot but independently. I think this becomes less useful when you've got more than a page worth of buffers, but then you can still just fall back to `:b` – icc97 Sep 01 '17 at 08:37
  • Glad to see it helps. Cheers! :) – qeatzy Sep 01 '17 at 09:13
  • 1
    From this [Vim wikia](http://vim.wikia.com/wiki/Easier_buffer_switching), you can shorten it a bit down to `nnoremap f :ls:b`, that might hit issues with the 'more' command though – icc97 Sep 21 '17 at 12:14
  • Yes, that way works perfectly if fewer files are open (the usual use case). `set more` stuff only makes difference when there are many files open (more than one page), eg, `vim /usr/include/*`, though less useful then. – qeatzy Sep 22 '17 at 07:01
12

I use the same .vimrc file for gVim and the command line Vim. I tend to use tabs in gVim and buffers in the command line Vim, so I have my .vimrc set up to make working with both of them easier:

" Movement between tabs OR buffers
nnoremap L :call MyNext()<CR>
nnoremap H :call MyPrev()<CR>

" MyNext() and MyPrev(): Movement between tabs OR buffers
function! MyNext()
    if exists( '*tabpagenr' ) && tabpagenr('$') != 1
        " Tab support && tabs open
        normal gt
    else
        " No tab support, or no tabs open
        execute ":bnext"
    endif
endfunction
function! MyPrev()
    if exists( '*tabpagenr' ) && tabpagenr('$') != '1'
        " Tab support && tabs open
        normal gT
    else
        " No tab support, or no tabs open
        execute ":bprev"
    endif
endfunction

This clobbers the existing mappings for H and L, but it makes switching between files extremely fast and easy. Just hit H for next and L for previous; whether you're using tabs or buffers, you'll get the intended results.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Max Cantor
  • 8,229
  • 7
  • 45
  • 59
  • 1
    I like these mappings. Also try Ctrl-H, Ctrl-L. I setup Firefox and gnome terminal with the same mappings. Very nice to have consistent tab key shortcuts. – cmcginty Jul 03 '09 at 20:21
  • 4
    use your leader key instead of ctrl to avoid clobbering existing mappings – aehlke Aug 10 '10 at 11:03
11

I use the following, this gives you lots of features that you'd expect to have in other editors such as Sublime Text / Textmate

  1. Use buffers not 'tab pages'. Buffers are the same concept as tabs in almost all other editors.
  2. If you want the same look of having tabs you can use the vim-airline plugin with the following setting in your .vimrc: let g:airline#extensions#tabline#enabled = 1. This automatically displays all the buffers as tab headers when you have no tab pages opened
  3. Use Tim Pope's vim-unimpaired which gives [b and ]b for moving to previous/next buffers respectively (plus a whole host of other goodies)
  4. Have set wildmenu in your .vimrc then when you type :b <file part> + Tab for a buffer you will get a list of possible buffers that you can use left/right arrows to scroll through
  5. Use Tim Pope's vim-obsession plugin to store sessions that play nicely with airline (I had lots of pain with sessions and plugins)
  6. Use Tim Pope's vim-vinegar plugin. This works with the native :Explore but makes it much easier to work with. You just type - to open the explorer, which is the same key as to go up a directory in the explorer. Makes navigating faster (however with fzf I rarely use this)
  7. fzf (which can be installed as a vim plugin) is also a really powerful fuzzy finder that you can use for searching for files (and buffers too). fzf also plays very nicely with fd (a faster version of find)
  8. Use Ripgrep with vim-ripgrep to search through your code base and then you can use :cdo on the results to do search and replace
icc97
  • 11,395
  • 8
  • 76
  • 90
  • 1
    fzf and fzf-vim changed my life, can't recommend it enough. It also works very well on windows, even in the console to fuzzy search for files. On windows use the ripgrep (rg) backend, which in my experience outperforms any other find/grep tool (like silversearcher or de default windows find that fzf uses iirc). You can use it to search through any list of items: most recent used files, currently opened buffers, lines in current buffer, lines in codebase (cwd), tags, .. you can easily program your own list fuzzy search. So good! (I came from ctrl-p and never looked back) – Emile Vrijdags Mar 13 '19 at 15:26
10

If you are going to use multiple buffers, I think the most important thing is to set hidden so that it will let you switch buffers even if you have unsaved changes in the one you are leaving.

indentation
  • 9,895
  • 6
  • 21
  • 14
8

My way to effectively work with multiple files is to use tmux.

It allows you to split windows vertically and horizontally, as in:

enter image description here

I have it working this way on both my mac and linux machines and I find it better than the native window pane switching mechanism that's provided (on Macs). I find the switching easier and only with tmux have I been able to get the 'new page at the same current directory' working on my mac (despite the fact that there seems to be options to open new panes in the same directory) which is a surprisingly critical piece. An instant new pane at the current location is amazingly useful. A method that does new panes with the same key combos for both OS's is critical for me and a bonus for all for future personal compatibility. Aside from multiple tmux panes, I've also tried using multiple tabs, e.g. enter image description here and multiple new windows, e.g. enter image description here and ultimately I've found that multiple tmux panes to be the most useful for me. I am very 'visual' and like to keep my various contexts right in front of me, connected together as panes.

tmux also support horizontal and vertical panes which the older screen didn't (though mac's iterm2 seems to support it, but again, the current directory setting didn't work for me). tmux 1.8

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
  • 1
    I had to lol when I saw your artifical example of "multiple windows". I hope nobody work like that :-) Anyway, I use tiling wm which is even better (I switched from tmux to i3wm). Just to complete your answer. – lzap Jul 08 '14 at 09:15
  • 1
    Tmux is awesome and I used this approach for a long time, but it makes navigation and copying and pasting between two files more difficult. – neallred Jan 18 '16 at 22:05
  • 1
    I used to do this, but having multiple vim buffers is superior when you need to yank/paste – Titou Nov 16 '17 at 15:51
7

In my and other many vim users, the best option is to,

  • Open the file using,

:e file_name.extension

And then just Ctrl + 6 to change to the last buffer. Or, you can always press

:ls to list the buffer and then change the buffer using b followed by the buffer number.

  • We make a vertical or horizontal split using

:vsp for vertical split

:sp for horizantal split

And then <C-W><C-H/K/L/j> to change the working split.

You can ofcourse edit any file in any number of splits.

AEM
  • 1,354
  • 8
  • 20
  • 30
Mou Sam Dahal
  • 273
  • 2
  • 10
3

I use the command line and git a lot, so I have this alias in my bashrc:

alias gvim="gvim --servername \$(git rev-parse --show-toplevel || echo 'default') --remote-tab"

This will open each new file in a new tab on an existing window and will create one window for each git repository. So if you open two files from repo A, and 3 files from repo B, you will end up with two windows, one for repo A with two tabs and one for repo B with three tabs.

If the file you are opening is not contained in a git repo it will go to a default window.

To jump between tabs I use these mappings:

nmap <C-p> :tabprevious<CR>
nmap <C-n> :tabnext<CR>

To open multiple files at once you should combine this with one of the other solutions.

Jens
  • 5,767
  • 5
  • 54
  • 69
3

I use multiple buffers that are set hidden in my ~/.vimrc file.

The mini-buffer explorer script is nice too to get a nice compact listing of your buffers. Then :b1 or :b2... to go to the appropriate buffer or use the mini-buffer explorer and tab through the buffers.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
projecktzero
  • 1,192
  • 1
  • 9
  • 11
2

have a try following maps for convenience editing multiple files

" split windows

nmap <leader>sh :leftabove vnew<CR>

nmap <leader>sl :rightbelow vnew<CR>

nmap <leader>sk :leftabove new<CR>

nmap <leader>sj :rightbelow new<CR>

" moving around

nmap <C-j> <C-w>j

nmap <C-k> <C-w>k

nmap <C-l> <C-w>l

nmap <C-h> <C-w>h

2

I made a very simple video showing the workflow that I use. Basically I use the Ctrl-P Vim plugin, and I mapped the buffer navigation to the Enter key.

In this way I can press Enter in normal mode, look at the list of open files (that shows up in a small new window at the bottom of the screen), select the file I want to edit and press Enter again. To quickly search through multiple open files, just type part of the file name, select the file and press Enter.

I don't have many files open in the video, but it becomes incredibly helpful when you start having a lot of them.

Since the plugin sorts the buffers using a MRU ordering, you can just press Enter twice and jump to the most recent file you were editing.

After the plugin is installed, the only configuration you need is:

nmap <CR> :CtrlPBuffer<CR>

Of course you can map it to a different key, but I find the mapping to enter to be very handy.

fede1024
  • 3,099
  • 18
  • 23
2

I would suggest using the plugin

NERDtree

Here is the github link with instructions.

Nerdtree

I use vim-plug as a plugin manager, but you can use Vundle as well.

vim-plug

Vundle

thenakulchawla
  • 5,024
  • 7
  • 30
  • 42
1

When I started using VIM I didn't realize that tabs were supposed to be used as different window layouts, and buffer serves the role for multiple file editing / switching between each other. Actually in the beginning tabs are not even there before v7.0 and I just opened one VIM inside a terminal tab (I was using gnome-terminal at the moment), and switch between tabs using alt+numbers, since I thought using commands like :buffers, :bn and :bp were too much for me. When VIM 7.0 was released I find it's easier to manager a lot of files and switched to it, but recently I just realized that buffers should always be the way to go, unless one thing: you need to configure it to make it works right.

So I tried vim-airline and enabled the visual on-top tab-like buffer bar, but graphic was having problem with my iTerm2, so I tried a couple of others and it seems that MBE works the best for me. I also set shift+h/l as shortcuts, since the original ones (moving to the head/tail of the current page) is not very useful to me.

map <S-h> :bprev<Return>
map <S-l> :bnext<Return>

It seems to be even easier than gt and gT, and :e is easier than :tabnew too. I find :bd is not as convenient as :q though (MBE is having some problem with it) but I can live with all files in buffer I think.

superarts.org
  • 7,009
  • 1
  • 58
  • 44
1

Most of the answers in this thread are using plain vim commands which is of course fine but I thought I would provide an extensive answer using a combination of plugins and functions that I find particularly useful (at least some of these tips came from Gary Bernhardt's file navigation tips):

  1. To toggle between the last two file just press <leader> twice. I recommend assigning <leader> to the spacebar:

    nnoremap <leader><leader> <c-^>
    
  2. For quickly moving around a project the answer is a fuzzy matching solution such as CtrlP. I bind it to <leader>a for quick access.

  3. In the case I want to see a visual representation of the currently open buffers I use the BufExplorer plugin. Simple but effective.

  4. If I want to browse around the file system I would use the command line or an external utility (Quicklsilver, Afred etc.) but to look at the current project structure NERD Tree is a classic. Do not use this though in the place of 2 as your main file finding method. It will really slow you down. I use the binding <leader>ff.

These should be enough for finding and opening files. From there of course use horizontal and vertical splits. Concerning splits I find these functions particularly useful:

  1. Open new splits in smaller areas when there is not enough room and expand them on navigation. Refer here for comments on what these do exactly:

    set winwidth=84
    set winheight=5
    set winminheight=5
    set winheight=999
    
    nnoremap <C-w>v :111vs<CR>
    nnoremap <C-w>s :rightbelow split<CR>
    set splitright
    
  2. Move from split to split easily:

    nnoremap <C-J> <C-W><C-J>
    nnoremap <C-K> <C-W><C-K>
    nnoremap <C-L> <C-W><C-L>
    nnoremap <C-H> <C-W><C-H>
    
Dionysis
  • 804
  • 8
  • 25
0

if you're on osx and want to be able to click on your tabs, use MouseTerm and SIMBL (taken from here). Also, check out this related discussion.

Community
  • 1
  • 1
Cpt. Senkfuss
  • 1,347
  • 3
  • 12
  • 20
0

You can be an absolute madman and alias vim to vim -p by adding in your .bashrc:

alias vim="vim -p"

This will result in opening multiple files from the shell in tabs, without having to invoke :tab ball from within vim afterwards.

npit
  • 2,219
  • 2
  • 19
  • 25
0
  1. To open 2 or more files with vim type: vim -p file1 file2

  2. After that command to go threw that files you can use CTRL+Shift+ or , it will change your files in vim.

  3. If u want to add one more file vim and work on it use: :tabnew file3

  4. Also u can use which will not create a new tab and will open file on screen slicing your screen: :new file3

  5. If u want to use a plugin that will help u work with directories and files i suggest u NERDTree.

  6. To download it u need to have vim-plug so to download other plugins also NERDTree to type this commands in your ~/.vimrc.

    let data_dir = has('nvim') ? stdpath('data') . '/site' : '~/.vim'
    if empty(glob(data_dir . '/autoload/plug.vim'))
      silent execute '!curl -fLo '.data_dir.'/autoload/plug.vim --create-dirs 
      https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
      autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
    endif

    call plug#begin('~/.vim/plugged')
    Plug 'scrooloose/nerdtree'
    call plug#end()
  1. Then save .vimrc via command :wq , get back to it and type: :PlugInstall

After that the plugins will be installed and u could use your NERDTree with other plugins.

AEM
  • 1,354
  • 8
  • 20
  • 30
Aram Simonyan
  • 162
  • 1
  • 5