I'm trying to write a simple mapping that will pre-populate the vim ex command line but leave the user there to execute the command on their own.

The use-case is for the user to type <leader>/foo which would then open the command line with :e **/foo and the user could either further refine, press ctrl-d to see possible matches, etc

  • For those who come to this question searching for how to preserve a trailing whitespace in the mapping, the answer is here. – Enlico Aug 21 '20 at 12:28

That kind of mapping is very, very common:

nnoremap <leader>/ :edit **/*


Might I suggest the following slightly smarter alternative?

set path=.,**
nnoremap <leader>/ :find *

The 'path' option tells Vim to look for files matching the argument given to :find in the directory of the file associated with the current buffer and any subdirectory of the working directory.


:help :find
:help 'path'
  • Thanks, that worked. Why would :find be superior to :edit in this case? Is it because you can pre-filter out matches with "path" while :edit will just search for everything? – Jonathan.Brink May 31 '15 at 14:26
  • 1
    Yes. With :edit **/foo, your are somewhat restricted to files located below the working directory. With :find, you are restricted to the directories specified in 'path', which may or may not be under the working directory. – romainl May 31 '15 at 14:30
  • A quick question about this, why is a * necessary at the end of the find command? I tried using that, and when I hit tab after typing a few characters in, it shows the results, then removes the star. – EvergreenTree May 31 '15 at 16:39
  • 1
    @EvergreenTree, :find bar only finds files whose name starts with bar but :find *bar also finds files containing bar anywhere in their name. Note that this rule also applies to :edit and all related commands. – romainl May 31 '15 at 16:55
  • Ah, that makes sense. – EvergreenTree May 31 '15 at 21:05

You may also want to check :h :map-<expr>. While admittedly in this case it would be overkill, it enables mappings of the following type:

function! PopulateFooMap(c)
  let dict = { "r":"bar", "z":"baz" }
  return ":e **/" . get(dict, a:c, a:c)

" very powerful
nnoremap <expr> <leader>/ PopulateFooMap(nr2char(getchar()))

" but this would still take precedence
nnoremap <leader>/f :e **/foo

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.