3

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
  • Right, this mapping worked fine with previous version of a function, which didn't have a mandatory argument. I changed it to your variant. – Thunderbeef Aug 26 '17 at 14:24
3

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
  • 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. – Thunderbeef Aug 25 '17 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. – Thunderbeef Aug 25 '17 at 16:54
  • I replaced "tgTj:+tabmove\<CR>" with "tgT:" . ( line(".") + i ) . "\<CR>:+tabmove\<CR>". Now it seem to work correctly. – Thunderbeef Aug 25 '17 at 17:17
  • @Thunderbeef Yes, You're right. And you could also avoid the line(".") function by adding a:firstline instead (see edit). – Jair López Aug 25 '17 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). – Thunderbeef Aug 25 '17 at 22:01
7

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

  • 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'). – Thunderbeef Aug 24 '17 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 Aug 24 '17 at 11:14
  • @Thunderbeef See my edit for the mapping. – statox Aug 24 '17 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? – Thunderbeef Aug 24 '17 at 12:20
  • 1
    Nevermind, I just added :q<CR> to the command. – Thunderbeef Aug 24 '17 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, privacy policy and cookie policy

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