Say I have a bunch of files, ordered alphabetically: 1.txt, 2.txt, 3.txt, etc. (In a typical situation, I have hundreds of these, ordered by date.) If I open file 2, is there a quick way for me to open file 1? (Or, 3?)

Ideally, I'd like to be able to do this with a single keystroke. (Holding modifiers is ok.)

Edit: One thing I forgot to mention was that there are multiple folders that have these files in them. It's common for me to be in a directory (e.g. /, and then need to edit files in /tmp/a and /tmp/b.) Although not a deal-breaker, it would be nice if I could change between /tmp/a/2.txt and /tmp/a/1.txt without having to reload vim.

  • 2
    FWIW I'm using a modified version of this: vpaste.net/5VEYU It's heavily inspired by the unimpaired plugin, with a few tweaks. You can move forward or backward in the filesystem with [f and ]f, and then repeat your last motion with ; or , (depending on the direction you want). Nov 10 '16 at 16:42
  • @user9433424 Thanks for the info! Seems pretty heavy-handed though... Is there not an easier way? Nov 10 '16 at 16:45
  • Vim can open (and handle) hundreds or thousands of files pretty easily: $ vim *.txt. Jumping to 3.txt from 2.txt is thus only a matter of :n<CR>, which you could map to anything if that's still too much typing. The problem is the order in which those files are opened: 1.txt, 10.txt, 100.txt, 1000.txt, 101.txt, 102.txt, etc. but this may or may not affect you depending on your naming scheme.
    – romainl
    Nov 10 '16 at 16:53
  • @romainl what a timing !! Nov 10 '16 at 16:57
  • You may include the dirextory in a variable and ise thay variable to get the list of files and then try tp get the next file by alphabetical order of your choice. Write code for all these in vimscript and map it with a command or a key combo. When you press the key combo or typeghe command,it calls the function thereby executing the statements and opening the expected file.
    – SibiCoder
    Nov 15 '16 at 16:58

if all your files have a similar extension (.txt, .c, .py), place them in a folder and cd then vim *.text, vim *.c, vim *.py

All the files will get added to args list, open the list by :args to see the added files. These files can be navigated using :n for next file and :N for previous. This could then be mapped to F7,F8 as so

map <F7> :N<CR>, map <F8> :n<CR>

for single keystroke operation


My EditSimilar plugin has :EditPrevious and :EditNext, which go alphabetically through files in the current directory. There are also :EditMinus / :EditPlus for numbers in files, and other, generic :EditSubstitute, :EditRoot, as well as :Split..., :VSplit... variants. You can easily define mappings quick mappings to those commands.

The unimpaired.vim plugin has (among many other, largely unrelated) [f / ]f mappings that work like :EditPrevious / :EditNext (but aren't as robust last time I've checked).


Following up on my comment…

I've created 1000 files named 1.txt to 1000.txt with the following command:

$ touch {1..1000}.txt

In Vim, I could use :args *.txt to load each of them in the argument list but the order will be less than optimal:


Instead, we could use :help backtick-expansion:

:args `ls\|sort -g`

Which gives us a more usable order:


From there, you can use argument-related commands to move around:

:n    " next argument
:N    " previous argument

And map them if that's still too much typing:

nnoremap <Right> :n<CR>
nnoremap <Left>  :N<CR>

Since we are at it, why not map our custom :args command, too?

nnoremap <key> :args `ls\|sort -g`<CR>

NOTE: Feeding the argument list with hundreds of files (from plain *.txt to structured *.rb or whatever) is nearly instantaneous and Vim can perfectly handle thousands of buffers so you don't have to fear performance issues.

--- edit ---

Unlike the buffer list, argument lists can be local. This allows you to have different argument lists in different tab pages (or windows, but I find tab pages to be easier to reason about in such a scenario and… that's pretty much their intended use):

$ vim
:arglocal `ls /tmp/a\|sort -g`
:arglocal `ls /tmp/b\|sort -g`

With such a setup, :n and :N will cycle through the first argument list in the first tab page and the second argument list in the second tab page.

  • This is great! Thank you so much for the detailed explanation! I added a bit of extra info to my question -- do you think there's a way to handle this scenario with your solution? Nov 11 '16 at 14:38
  • Yes, it is definitely possible. See my edit.
    – romainl
    Nov 11 '16 at 15:26
  • if ls command supports it, can use ls -v to natural sort of (version) numbers within text
    – Sundeep
    Nov 11 '16 at 16:38
  • Very good point, Sundeep.
    – romainl
    Nov 11 '16 at 17:18

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.