Sublime Forum

Prevent Multiline input on input panel


How do I prevent the user enters multi line input? I could bind ctrl+enter to a command to prevent adding another line. Is there another way?

The scope inside input panel is plain text - is it possible to change that?



What do you need to do this ? Understanding that might help



The window.show_input_panel() method returns the view that represents the editable area of the panel, so you can indeed change the syntax to something else, say to a syntax that is otherwise plain text but with a scope that is distinct enough that you can target a key binding to not allow the key that would do it.

For example, you can try this in the console:

test = window.show_input_panel('caption', '', None, None, None); test.assign_syntax('Packages/Python/Python.sublime-syntax')

When the panel opens, it will be using the Python syntax (though note that you need to have both lines together in one input separated by a semicolon if you do this interactively from the console, or the console will close when the panel opens and you can’t enter the second line).

That said, someone can paste whatever they want into the control, so you would also have to do something like intercept the paste command if it’s going to your view and block it there. You also can’t stop the user from making their own binding on a key to type characters you don’t want.

So, all else being equal, the best way to prevent multiline input is probably to sanitize the input when you receive it, and reject it if it doesn’t conform to what you want.