8

While navigating file system in :Explore mode, is it possible to select and open multiple files, each in a new tab?

Edit:

I ended up using a function proposed by Jair López, because it works pretty much as I envisioned it, although statox provided a good explanation for built-in netrw mechanics.

For consistency, I also added a normal mode mapping to make ShiftTT open a new tab, while keeping netrw tab in focus. It simplifies opening tabs for multiple files that aren't listed consecutively.

autocmd Filetype netrw nmap <buffer> <silent> TT VT
1
  • Right, this mapping worked fine with previous version of a function, which didn't have a mandatory argument. I changed it to your variant. Commented Aug 26, 2017 at 14:24

3 Answers 3

4

Let's Vim do it for you. You can put the following code at the end of your vimrc file:

function! NetrwOpenMultiTab(current_line,...) range
   " Get the number of lines.
   let n_lines =  a:lastline - a:firstline + 1

   " This is the command to be built up.
   let command = "normal "

   " Iterator.
   let i = 1

   " Virtually iterate over each line and build the command.
   while i < n_lines
      let command .= "tgT:" . ( a:firstline + i ) . "\<CR>:+tabmove\<CR>"
      let i += 1
   endwhile
   let command .= "tgT"

   " Restore the Explore tab position.
   if i != 1
      let command .= ":tabmove -" . ( n_lines - 1 ) . "\<CR>"
   endif

   " Restore the previous cursor line.
   let command .= ":" . a:current_line  . "\<CR>"

   " Check function arguments
   if a:0 > 0
      if a:1 > 0 && a:1 <= n_lines
         " The current tab is for the nth file.
         let command .= ( tabpagenr() + a:1 ) . "gt"
      else
         " The current tab is for the last selected file.
         let command .= (tabpagenr() + n_lines) . "gt"
      endif
   endif
   " The current tab is for the Explore tab by default.

   " Execute the custom command.
   execute command
endfunction

" Define mappings.
augroup NetrwOpenMultiTabGroup
   autocmd!
   autocmd Filetype netrw vnoremap <buffer> <silent> <expr> t ":call NetrwOpenMultiTab(" . line(".") . "," . "v:count)\<CR>"
   autocmd Filetype netrw vnoremap <buffer> <silent> <expr> T ":call NetrwOpenMultiTab(" . line(".") . "," . (( v:count == 0) ? '' : v:count) . ")\<CR>"
augroup END

Restart Vim and then you can:

  1. Open Netrw (e.g., with :Explore)
  2. Move the cursor to the first file.
  3. Enter Line-wise Visual mode by hitting Shift-V
  4. Hit J (That's not Shift-J, just in case) as much as you need to.
  5. Hit T

Advantages

  • You don't need to add any plugin
  • It doesn't stuff the arglist
  • You'll preserve the order of the buffer list and tabs
  • You can choose which tab will be the current one by using or not using a count in the mapping.

Explanation

There's a lot going on here so if you're in doubt, just let me know. Let's say the directory you open with :Explore is as follows:

../
./
a.txt
b.txt
c.txt
d.txt

And the cursor is on file a.txt and next, you press Shift-V and move the cursor to the line with d.txt. You can hit:

  • T to open each file in a tab and select the file d.txt as the current tab
  • 1T to open each file in a tab and select the file a.txt as the current tab
  • 2T to open each file in a tab and select the file b.txt as the current tab
  • And so on

Or you can hit:

  • Shift-T to open each file in a tab and keep the current tab.
  • 1Shift-T to do the same as 1T
  • 2Shift-T to do the same as 2T

The differences between Shift-T and T are:

  • T select the last selected file as the current tab by default
  • Shift-T keeps the current tab by default
17
  • I like the idea, but currently it doesn't work correctly for me. When I select multiple files with Shift-V and press t, I see a correct number of new tabs opened, but only the first tab is a file (first one in the list), while the rest are just copies of netrw tab. Commented Aug 25, 2017 at 16:26
  • I tried to debug this, and with V=15 I found that, for 2 selected files, the following command is executed: "normal tgTj:+tabmove^MtgT:tabmove -2^M3gt". Afterwards, I tried to call it manually. From what I see, the problem is that after first "tgT" the cursor jumps to the top of netrw list (to ".."), and "j" moves it down to ".", which is why rest of the tabs are opened as a copy of current directory. Commented Aug 25, 2017 at 16:54
  • I replaced "tgTj:+tabmove\<CR>" with "tgT:" . ( line(".") + i ) . "\<CR>:+tabmove\<CR>". Now it seem to work correctly. Commented Aug 25, 2017 at 17:17
  • @Thunderbeef Yes, You're right. And you could also avoid the line(".") function by adding a:firstline instead (see edit). Commented Aug 25, 2017 at 20:08
  • One extra thing I hope to achieve is to return the cursor to original line if netrw tab is in the focus at the end (e.g. when using Shift+T). Commented Aug 25, 2017 at 22:01
13

From :h netrw-t:

BROWSING WITH A NEW TAB             netrw-t

Normally one enters a file or directory using the <cr>.  The "t" map
allows one to open a new window holding the new directory listing or file in
a new tab.

So in the netrw window simply press t when your cursor is on the file or on the directory you want to open in the new tab.


To do that with multiple files you need to mark them, put them in the arglist and then use the arg list to open the tabs.

To mark a file in a netrw window use mf.

Once the files are marked put them in the arglist with ma.

Then simply use :argdo tabnew.

Edit To reduce the number of keypresses you can add the following line in your .vimrc:

autocmd Filetype netrw nmap <buffer> <F5> ma:argdo tabnew<CR>

This is an autocommand which is executed when you open a netrw buffer (FileType netrw) and creates a mapping only in this buffer which will put the marked files in the arglist and issue the command :argdo tabnew.

With that you can simply mark your files with mf and use F5 (or another key if you prefer) to open the files in new tabs.


See

6
  • Thanks. That sure is a lot of work. I was hoping for something like 'Enter Visual mode -> select multiple files -> press "t"'. Also, you a have a typo ('argo'). Commented Aug 24, 2017 at 10:19
  • @Thunderbeef Thanks for the typo. Now to leverage the number of keypresses I think you could create a mapping which would do the ma and :argdo tabnew and put the mapping in an autocommand triggered on the FileType netrw event.
    – statox
    Commented Aug 24, 2017 at 11:14
  • @Thunderbeef See my edit for the mapping.
    – statox
    Commented Aug 24, 2017 at 11:21
  • Thanks. That is certainly more convenient. Though there is still one thing that bothers me: in addition to opening marked files, it also opens empty [No Name] tab, and focuses on it by default. Is there a way to prevent this behavior? Commented Aug 24, 2017 at 12:20
  • 1
    Nevermind, I just added :q<CR> to the command. Commented Aug 24, 2017 at 13:17
1

If you don't mind installing a plugin, you could use dirvish.

Once the directory opened, visually select the files (e.g. with VG) and press t to open in a tab.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.