I want to open the list of visual selection's files on new tabs such that the top-tab-bar is visible

enter image description here

Mapping based on the answer here about How do I loop over a list of files from a visual selection?

:nnoremap <Leader>gf :silent! exec "!nvim " shellescape('<cfile>') <bar> redraw!<cr>

where in the place of !nvim, I have tried unsuccessfully

  • nvim split
  • tabe

The main point of the question is to open a list of several files formatted like this:


Where you select in visual mode (v and cover for instance two lines) and press <Leader>gf for the activation of the command which will open all the selected files in new tabs.

My Applicable .vimrc after statox and nobe4's answers

I think semantics can confuse them.

"" Mapleader is space now from \ 
let mapleader=" " 
"" Quick tabs - Go to tab by number 
noremap <leader>1 1gt 
noremap <leader>2 2gt 
noremap <leader>3 3gt 
noremap <leader>4 4gt 
noremap <leader>5 5gt 
noremap <leader>6 6gt 
noremap <leader>7 7gt 
noremap <leader>8 8gt 
noremap <leader>9 9gt 
noremap <leader>0 :tablast<CR> 
"" Go to last active tab 
au TabLeave * let g:lasttab = tabpagenr() 
nnoremap <C-Left> :tabprevious<CR> 
nnoremap <C-Right> :tabnext<CR>      

"" https://vi.stackexchange.com/a/7787/2923 
function! OpenSelectionAsTabs() range 
    let tabnr = tabpagenr() 
    for line in range(a:firstline, a:lastline) 
        "" to allow whitespaces in names https://vi.stackexchange.com/a/7865/2923
        execute "tabedit " . fnameescape(getline(line))
        execute "tabp" 

nnoremap <leader>gf :'<,'>call OpenSelectionAsTabs()<CR> 
vnoremap <leader>gf :call OpenSelectionAsTabs()<CR> 

How can you open a list of files into tabs with Tab-top-bar?


1 Answer 1


If you want to open the file under your cursor in a new tab your mapping would be:

nnoremap <leader>gf :silent! execute "tabe " . expand('<cfile>') <bar> redraw!<CR>

Your mapping didn't work because shellescape only escapes some characters in the string it get as arguments, what you need is expand to convert <cfile> to its value.

Then to customize the appearance of the tabline you can see :h setting-tabline


Here is a function and a mapping to answer your update:

function! OpenSelectionAsTabs() range
    let tabnr = tabpagenr()
    for line in range(a:firstline, a:lastline)
        execute "tabedit " . getline(line)
        execute "tabp"

nnoremap <leader>gf :'<,'>call OpenSelectionAsTabs()<CR>
vnoremap <leader>gf :call OpenSelectionAsTabs()<CR>

Now in visual and in normal mode <leader>gf will take the file visually selected and open them in new tabs.

Here is the result of the command: (my leader is <space>, so the trigger of the command is <space>gf):

Execution of the command

Edit 2

Follow up of this question: The function I created will fail if the path contains whitespaces. (Since Vim will consider the part after the space as a parameter of the command)

So as @Nobe4 said in his answer you'll need to replace:

execute "tabedit " . getline(line)


execute "tabedit " . fnameescape(getline(line))
  • 1
    Could you please up update your question to clarify what you mean by 'files of visual selection'? Especially of is formatted your list of files? Are the separated by comas, by whitespaces, by new lines?
    – statox
    May 13, 2016 at 13:53
  • @Masi Does my update answers your question?
    – statox
    May 13, 2016 at 14:23
  • 1
    @Masi: Yup I had left '<,'> in the visual mapping now it should work. I recorded a gif to show the result.
    – statox
    May 13, 2016 at 17:50
  • 1
    I'm glad it fits your needs but I don't understand why you added a condition on the OS: both mappings and function should work fine on Windows too. For the gif I use this answer on meta: meta.vi.stackexchange.com/a/1322/1841
    – statox
    May 14, 2016 at 9:32
  • I corrected the Windows thing. Thank you for the link! May 14, 2016 at 9:49

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.