2

So, this may not be a clear question because of the title. But here's the actual explanation:

I have this custom function, I want this function to be able to be called from the command line, this is done (:FunctionName arg), but now I need to make this function react to a certain mapping.

So when the user presses <leader>cs it will prompt the user for the arg part, mapping this is still kind of unclear to me, but also how to achieve this functionality. This is kind of what the Surround script does, where it lets you input the old character and the new character to replace it with.

I need this for my first script that I'm making BTW, which allows you to change the file's syntax in a manner similar to Sublime Text's way.

Thanks for all your help!

1 Answer 1

3

The simplest way is to remove the concluding <CR> from the mapping, so that it just enters command-line mode and fills the command line with your custom command:

:nnoremap <Leader>cs :FunctionName<Space>

You can then enter the arg and trigger the command with Enter.


Alternatively, you can query for user input via input() (and single characters with getchar(); obvious, isn't it?!), like this:

function! FunctionNameWithQuery()
    let arg = input('arg: ')
    execute 'FunctionName' arg
endfunction
nnoremap <Leader>cs :call FunctionNameWithQuery()<CR>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I'll try this soon enough, and give you feedback. :)
I just tried it out, and it worked, thanks! BTW, here's my new script, how about giving it a try. :) vim.org/scripts/script.php?script_id=4324
Glad I could help. Some additional hint: If you add -complete=syntax to your plugin's command definition, you can even auto-complete the syntax names with <Tab>.
I see what you mean. Thanks for the tip, that's the first thing I'll do. :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.