1

The code I have so far is this:

command! -nargs=1 Nf call s:NewFile(<q-args>)<CR>

function! s:NewFile(fp)
    :echom a:fp
    :e %:h/a:fp
endfunction

The argument is passed in normally (I know because of the echom statement).

When I run a command such as: :Nf test.py, it echoes test.py as it should, but when creating the file itself, it interprets a:fp as the filename.

How can I get it to interpret test.py as the filename (or more complex filenames like ../foo/bar/test.py)?

1 Answer 1

4

Try it like this:

command! -nargs=1 Nf call s:NewFile(<q-args>)

function! s:NewFile(fp)
    echom a:fp
    execute "e " . expand("%:h") . "/" . a:fp
endfunction

The %:h needs to be handed to expand(), so that the path is extracted from the current file. Then you need to use execute as the parts of the command have to be concatenated together. Then this string is executed.

Also you don't need the <CR> ad the end of the command! .... This is only needed when you call a function (or command) from a mapping.

And the leading colons, are not needed in a script.

3
  • Thanks man! I'm still new to vimscript so your writeup helped me understand more.
    – StarLlama
    Jan 28, 2019 at 17:54
  • 1
    @StarLlama For further Vim-related questions, you might want to join, vi.stackexchange.com.
    – Ralf
    Jan 28, 2019 at 18:08
  • For sure, didn't know about that one but will use for all vim question from now on
    – StarLlama
    Feb 18, 2019 at 18:04

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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