Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a list of files

file1.ext
file2.ext
...

How do I create a file with file2.ext name if my cursor is inside file2.ext. Kind of like gf only for creating new files (this is a hypothetical situation, just thought might be helpful someday).

share|improve this question

migrated from stackoverflow.com Apr 30 '11 at 5:32

This question came from our site for professional and enthusiast programmers.

2 Answers 2

up vote 12 down vote accepted

The first thing that comes to my mind is to use touch command passing to it the filename under the cursor.

map <silent> <leader>cf :!touch <c-r><c-p><cr><cr>

But there is a Vim-internal solution that should be portable across platforms. The function writefile writes a list to a file line by line. When the input list is empty, it creates an empty file. (See :help writefile for details.)

map <silent> <leader>cf :call writefile([], expand("<cfile>"), "t")<cr>

Note that filename extraction could be adjusted by using different expand patterns (see :help expand()).

By the way, if you would like not to create a file, but to open one for editing, you could define gf-like mapping

map <leader>gf :e <cfile><cr>

where :e could be replaced with :tabe or with something similar.

share|improve this answer
1  
+1 for teaching <cfile> instead of <C-r><C-f> –  sehe Apr 29 '11 at 8:39

I took the excellent answer by ib above and expanded it as follows. My goal was to use vim to create new markdown files as needed for a wiki (in this case a Gollum wiki)

I first tried:

map <silent> <leader>cf :call writefile([], expand("<cfile>"), "t")<cr>` 

the above does work as stated in the answer. However, at first I thought it was not working because I did not actually see the file opening in vim. Using the second bit of code below will open a new file - this is more what I was looking for. So I combined them and tried:

map <leader>cf :e <cfile><cr>

but that does not work for a wiki because when you try to create a new file in the wiki using syntax like [[the-new-file]] the wiki syntax does not allow for the extension of the file in the brackets. However, Vim needs to know the extension when creating a new file for this to work. In this case I used:

    map <leader>cf :e <cfile>.md<cr>

so I could create new markdown files. There are ways to further customize this (for example by not hardcoding the extension) but the above works fine for my needs. If I ever need another extension (for example to save a .wiki file) I will probably just take the simple route and make another map like:

    map <leader>cwf :e <cfile>.wiki<cr>

As a side benefit you can use the same command to open the already existing markdown file (the standard gf command will not work here because the file extension is missing).

share|improve this answer
    
gf works if set suffixesadd=.wiki. An improved version of your cf would be one that respects suffixesadd, at least in the case where it contains only one item. –  hjdivad Oct 28 '13 at 20:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.