2

Inspired by a nice solution on a Stack Overflow, the following was put in my vimrc:

command! Bro :vnew +setl\ buftype=nofile | 0put =v:oldfiles 
  \| nnoremap <buffer> <CR> :e <C-r>=getline('.')<CR><CR>

There are two problems with it I'd like to fix:

  1. I want to view the list from the top (most recent) rather than the bottom, as is currently the case. I've tried adding 1G to the script, but to no avail.
  2. I would rather the list be open in the current window, so I don't have to come back to close it later. Using enew instead of vnew produces an E488 error, and e puts the list of files in current file.

Thanks.

Update. Since the purpose of this exercise is to make the file list searchable, it's probably a good idea to append :noh<CR> to the nnoremap so that once you've searched, found your file, and opened it by pressing Enter, search terms are not highlighted in the file.

  • 3
    Just a note, 1G doesn't work here because you are basically in command-line mode (or ex-mode for purists) and 1G is a normal mode command. – Tumbler41 Jun 8 '16 at 22:42
  • FWIW, my answer to the linked question is a lot cleaner than that hack. – romainl Jun 9 '16 at 4:17
  • @Tumbler41 How dumb of me! I thought 1G was a command-line mode, too. Somehow, although I never did :1G, I thought it would work until I tried just now. – bongbang Jun 9 '16 at 16:06
6

This should work:

command! Bro :enew | setl buftype=nofile |  0put =v:oldfiles
  \| nnoremap <buffer> <CR> gf | 1

:enew doesn't support +cmd, unlike :vnew, so just split the :setl out to a new, chained, command.

I changed the mapping to use gf, which IMHO, is more native. The last command, 1, should move the cursor to the first line. Unlike the :e … getline() version, gf will fail if the file doesn't exist - the help in fact suggests using that if you always want to edit a new file if it doesn't exit and suggests a simpler variant:

To make gf always work like that: 
        :map gf :e <cfile><CR>

So you could also do nnoremap <buffer> <CR> :e <cfile><CR>. But I suppose this isn't really a problem - files in the history are more likely to exist than not. :)

| improve this answer | |
  • Thank you! You're right that gf is more native, but it doesn't work when there's a space the path (e.g. Window's Program Files). – bongbang Jun 8 '16 at 23:31
  • 1
    @bongbang that's part of the beauty of gf - what it includes can be customized using isfname (:set isfname^=\ ), you can optionally add suffixes to check with suffixesadd, and if a file can't be found, it uses includeexpr which can be used to modify the filename how you want to check again. Though I suppose Ingo's version is far simpler. – muru Jun 8 '16 at 23:51

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.