2

Let's say I'm editing this text in vim:

People are helpful.

and I am in normal mode, with my cursor currently on the h letter of the helpful word.

How could I built a custom command that when run would automatically open in the current directory the file helpful.md (if such a file does not exists, then nothing would happen or a new empty file would be opened, whatever is the easiest to implement).

5

Using gf, you can go to the file under the cursor.

Combined with set suffixesadd+=.md, you can achieve your desired behavior. (suffixesadd asks gf and many other friends to try adding any of the suffixes in its list to the file while searching).

Note that you need to have 'path' correctly set (it's default is helpful for C programmers; tpope's apathy plugin sets it for many other filetypes). For this specific case, you probably just need :set path+=..

:help gf also mentions:

            If you want to edit the file in a new window use
            |CTRL-W_CTRL-F|.
            If you do want to edit a new file, use: >
                :e <cfile>
<           To make gf always work like that: >
                :map gf :e <cfile><CR>
<           If the name is a hypertext link, that looks like
            "type://machine/path", you need the |netrw| plugin.
            For Unix the '~' character is expanded, like in
            "~user/file".  Environment variables are expanded too
            |expand-env|.

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.