18

Edit Multiple Files at once in Neovim

 3 years ago
source link: https://jdhao.github.io/2020/10/28/edit_multiple_files_nvim/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Edit Multiple Files at once in Neovim

2020-10-28247 words 2 mins read 140 times read

When I was in a folder and tried to edit multiple files using glob pattern with the following Neovim command:

:edit *.vim

I was surprised to find that Neovim gives the following error message:

E77: Too many file names.

This is because the command :edit only supports editing one file. There are several ways to open multiple files at once.

Use argument list

We can Neovim’s builtin in argument list feature to open multiple files. When we open Neovim from command line with multiple files, argument list will be filled with a list of file names. To replace the argument list with new file names, we can use :args {glob_pattern} or :next {glob_pattern}.

For example, to open all files in current directory with extension .vim, use :args *.vim or :next *.vim. You can then traverse the list of files using : next and :previous command.

For more info, see :h argument-list.

Define your own custom command

The second way is to define a custom :Edit command that can take glob patterns. Something like the following should work:

function! MultipleEdit(p_list)
  for p in a:p_list
    for c in glob(p, 0, 1)
      execute 'edit ' . c
    endfor
  endfor
endfunction

command! -bar -bang -nargs=+ -complete=file Edit call MultipleEdit([<f-args>])

Then we can use :Edit *.vim or :Edit *.vim *.py to open multiple files.

References

Author jdhao

LastMod 2020-11-06

License CC BY-NC-ND 4.0

Reward
Prev Next

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK