Open list of files in the directory from which to choose the file to edit in vim

0

In the video at Watch how i work rails he first obtains the output of find . -type f and uses this list as a regular vim file to search. Ones he finds this file in the list, he can open it in vim. This looks really useful but he does not specify the alias to do it. Can someone give me the alias for the same?

woodstok

Posted 2013-08-21T14:34:34.567

Reputation: 761

Answers

2

What's going on can be pieced together from the author's dotfiles. Warning! The whole thing is pretty convoluted (and smart).

  1. This is the vim- bash command:

    vim-() {
      vim -s <(printf '\eh')
    }
    

    It opens Vim and simulates a key sequence, <Esc>h.

  2. <Esc>h is mapped to execute a custom command, :FPScratchy which in turn calls an external script, ack_find.

    nmap <ESC>h :FPScratchy "ack_find"<CR>
    
  3. The purpose of that command is to create a "scratch window" containing the file list generated by the external command and do some remappings for navigation: <CR> in normal mode to open the file under the cursor, <CR> in visual mode to add the selected files to the local arglist and <Esc><CR> in normal mode to open the file under the cursor in a split window.

    function! FPScratchy(...)
      call call(function("Scratchy"), a:000)
      setlocal cursorline
    
      nmap <buffer> <CR> 0gf
      vmap <buffer> <CR> :Lines2arglocal<CR>
      nmap <buffer> <ESC><CR> 0<c-w>f<c-w>w
    endfunction
    command -nargs=* FPScratchy :call FPScratchy(<args>)
    
    function! Lines2arglocal() range
      let lines = map(range(a:firstline, a:lastline), 'getline(v:val)')
    
      execute "arglocal " . join(lines, " ")
    endfunction
    command -range Lines2arglocal <line1>,<line2>:call Lines2arglocal()
    
  4. The author changed his find-based method to a slightly more involved ack-based one a few years ago. We have no means to know what he used daily but we can assume it was a simple:

    $ find . -type f
    

romainl

Posted 2013-08-21T14:34:34.567

Reputation: 19 227

2

There are several ways to do that. To get the output of that command into a Vim buffer, open Vim and execute

:r!find . type f

Another way to do that, using vim in a terminal, would be to execute the following at the shell prompt.

$ find . type f | vim -

I usually put the cursor over the desired file name and type

<C-W>f

where <C-W> means Ctrl-W. That will open the file in a new window, which avoids the problem of trying to open a new file in a modified buffer.

Another approach would be to first execute

:set nomodified

Then type

gf

That will open the new file in the buffer/window that contained the list of files.

Still another way would be to again put the cursor over the desired file name and execute

:e! <C-R>f<CR>

where <C-R> means Ctrl-R and <CR> means the Enter key. The <C-R>f combination will expand to the file name under the cursor.

See

:help :r!
:help CTRL-W_f
:help gf
:help 'modified'
:help c_CTRL-R_f

garyjohn

Posted 2013-08-21T14:34:34.567

Reputation: 29 085

Even before that how do i get the file list in vim? – woodstok – 2013-08-21T15:23:32.067

Probably with :r!find . type f. See :help :r!. I'll update the answer. – garyjohn – 2013-08-21T15:26:07.327

@MIkhail: If he starts from the command line and not from within an open Vim session, perhaps he is running find . -type f | vim -. This will grab STDIN and put it into the buffer. – Daniel Andersson – 2013-08-21T15:27:19.227