How to effectively work with multiple files in Vim
Asked Answered
W

29

1235

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?

Wingback answered 10/9, 2008 at 9:3 Comment(4)
tmux... use vim in each paneJasmine
@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: #5533378)Flyfish
:N seems like an easier way to go to the previous file...Mongrelize
vimcasts.org/categories/the-argument-list ? VimCasts are really highly recommended when learning vim.Wentworth
R
1296

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)

Ruffi answered 10/9, 2008 at 9:9 Comment(36)
To save and close a tab, you can also use ZZ instead of :wq (just like would normally save and close)Urgency
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.Unhandsome
Also, in edit mode gt goes to the next tab, and gT goes to the previous tab.Flatboat
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.Hermaherman
CTRL+PageUp/PageDown also works in Debian Squeeze. I didn't know tabs existed by default! /upvoteWhicker
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.Rippy
Note: Ctrl+PageUp/PageDown won't work if you have multiple tabs open in Gnome terminal. The terminal tabs take the shortcut.Sacking
vertical split! :vsp filenameThole
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 <silent> <C-h> gT and nnoremap <silent> <C-l> gt.Livy
@Dean: I think gt and gT work in escape/command mode, not in edit mode.Mccorkle
@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.Entomophilous
@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.Furcate
@Hermaherman 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.Furcate
fyi, Buffers vs. TabsSurplice
This answer doesn't deserve so many up-votes because it made me (and probably many other people) think that tab in vim is the same as tab in any other editor (like notepad++). @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.'Thalassic
Downvote as Patryk.beza is correct, this isn't the intended function of tabs.Jewry
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.Euphonize
How to map :tabn and :tabp with F7/F8 keys?Sabrina
@Thalassic I think what the blog Buffers vs. Tabs is talking about is Terminal Tab, not vim tabs mentions in this answer.Izettaizhevsk
@Izettaizhevsk No. You're wrong. Buffers vs. Tabs clearly refers to vim's tabs, not terminal's tabs.Thalassic
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).Heathenish
In summary, 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.Tobias
This is very bad advice. Tabs are effectively a way to layout buffers.Norford
Use : tabn1, :tabn2, :tabn3 ... etc to switch to that particular tab.Kagu
@munesh You don't need F7/F8 key for doing :tabn and :tabp, just use gt or gTGereron
Iterating between tabs is already mapped to CTRL-PgUp & CTRL-PgDown by default (at least in Ubuntu 16.04)Tribesman
@munesh Add the lines map <F7> :tabp<CR> and map <F8> :tabn<CR> to your .vimrc file.Graybeard
Please consider adding a notice indicating that buffers should be used instead, and providing a link to a more idiomatic answer.Gnawing
Set the shortcuts to <Ctrl+Tab> and <Ctrl + Shift + Tab> to make them the same as browser tab switching.Paba
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?Brokaw
Also :tabfind filenam then <Tab> allows you to find any file with "filenam" in it and open it in new tab. This combined with :set wildmenu is pretty nice.Gluttonous
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.Niemann
In Ubuntu Terminal, <Ctrl + Page Up / Down> is already mapped to change Terminal tabs, but <Ctrl + Alt + Page Up / Down> works to change Vim tabs.Tapp
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.Baldhead
You can also move tabs with the :tabm command! Like :tabm +1 to move a tab one step forward etc.Erivan
@Brokaw 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, NateOutguess
E
613

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.

Empedocles answered 10/9, 2008 at 9:47 Comment(11)
In case you're playing with multiple buffers, I would recommend LustyJuggler.Rika
Thanks for making a very nice quick-reference for the vim commands used most often when working with multiple files.Busch
Or :b# where # is the file index in the buffer, not just last visitedChairborne
: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<Enter> or :b foo.txt or :b oo<Tab><Enter> to edit that file. Yes, the last one would complete 'oo' to 'foo.txt' when you press <Tab>.Angelika
I have this line in my .vimrc: nnoremap gb :ls<cr>:b<space>. 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.Tailpipe
and when using :ball it split the view in multiple windows, do you know how to swap from one to another ?Vena
+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.Glosso
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.Laurettelauri
Start vim via vim, not vi, and add set nocompatible hidden to your .vimrc. The handling of multiple buffers is limited in compatible mode.Yajairayajurveda
@Glosso 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 <leader> shortcut in .vimrc. I prefer vertical splits which requires first create a vertical split :vsp then jump in the new split via g Ctrl-].Niemann
ctrl-^ too will toggle last visited file.Catamite
P
253
: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. ;)

Pleurisy answered 15/9, 2008 at 19:3 Comment(3)
go to a specific buffer: N CTRL-^Hygrostat
@Pleurisy 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!Beccafico
Also, use the " mark to jump to where you were in the file last time.Lovage
C
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

Cabinet answered 10/9, 2008 at 9:34 Comment(1)
This is really the answer to the question asked, though given the title the other answers are perfectly fair.Laurettelauri
R
51

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.

Recoverable answered 10/9, 2008 at 9:43 Comment(0)
S
48

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.

Sweeting answered 10/9, 2008 at 9:34 Comment(1)
Nice to see the aside of related information like % and #. +1Marvelmarvella
E
42

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.

Encampment answered 10/9, 2008 at 9:7 Comment(2)
Thanks, sounds great, but we unfortunately only have VIM 6.1 installed on the server.Wingback
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.Euthanasia
I
29

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.

Ilia answered 31/3, 2016 at 11:47 Comment(3)
ctrlp has moved, and is now found at ctrlpvim/ctrlp.vimStephainestephan
exactly. I can't imagine vim in a dev setting without those plugins.Baugh
ctrlp and its alternatives are just drug man, you can't quit once you've had it.Danseuse
B
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/*
Bruis answered 8/12, 2011 at 19:43 Comment(0)
M
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.
Marcus answered 18/6, 2015 at 12:47 Comment(1)
You get that summary by typing :help window in VimNiemann
A
23

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

Abel answered 5/7, 2017 at 20:2 Comment(0)
R
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
Resendez answered 15/9, 2008 at 17:15 Comment(2)
350 files!!! That is impressive. how do you find the right buffer to jump to? Do you ever do splits?Recommendatory
@BrianO'Dell the :b <partial filename><tab> 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.Niemann
H
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.

Herodias answered 8/7, 2013 at 21:37 Comment(0)
D
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.

Dg answered 17/9, 2008 at 14:43 Comment(2)
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.Reprehend
use your leader key instead of ctrl to avoid clobbering existing mappingsReal
L
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'.

Lilithe answered 20/6, 2017 at 8:37 Comment(4)
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 :bNiemann
Glad to see it helps. Cheers! :)Lilithe
From this Vim wikia, you can shorten it a bit down to nnoremap <leader>f :ls<cr>:b<space>, that might hit issues with the 'more' command thoughNiemann
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.Lilithe
N
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
Niemann answered 19/8, 2017 at 10:31 Comment(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)Espinal
K
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.

Katz answered 15/9, 2008 at 14:39 Comment(0)
M
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

Middleoftheroader answered 19/1, 2014 at 18:24 Comment(3)
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.Almedaalmeeta
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.Jiggerypokery
I used to do this, but having multiple vim buffers is superior when you need to yank/pasteKosygin
S
8

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.

Septuor answered 19/1, 2020 at 13:38 Comment(0)
C
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.

Couvade answered 16/9, 2008 at 19:5 Comment(0)
C
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.

Cone answered 3/7, 2014 at 14:8 Comment(0)
C
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

Cyclonite answered 7/1, 2014 at 5:55 Comment(0)
T
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.

Tavi answered 5/12, 2015 at 18:59 Comment(0)
F
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

Fanchon answered 3/6, 2018 at 23:52 Comment(0)
S
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.

Spittoon answered 13/4, 2015 at 2:46 Comment(0)
C
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>
    
Cystoid answered 27/2, 2016 at 12:4 Comment(0)
S
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.

Shag answered 31/5, 2013 at 9:33 Comment(0)
P
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.

Pang answered 20/10, 2017 at 22:37 Comment(0)
R
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.

Rotary answered 30/3, 2021 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.