dPol
dPol

Reputation: 475

vim: define new command that works only outside c++ comments

When I am coding in c++, I want vim to expand ( into ()<++> and place the cursor in the parenthesis. I do this by putting the following line in one of the files loaded at startup:

inoremap (      ()<++><Left><Left><Left><Left><Left>

However, I would like this binding to be disabled in comments, like

// Inline comment where ( shouldn't become ()<++>

or

/* Comment block where ( shouldn't become ()<++>
*/

How can I do it?

Upvotes: 2

Views: 75

Answers (1)

Luc Hermitte
Luc Hermitte

Reputation: 32936

Install lh-brackets it already detects the context to not expand anywhere. It also adds a placeholder after the closing brackets. If you directly install lh-cpp, the control statements will also be context aware.

(I must admit your request is quite surprising as nobody seems to use placeholders any more. And yet lh-brackets is the plugin that (re-)introduced this concept (idea stolen from Stephen Riehm's original bracketing macros). Then mu-template used a similar bracketing philosophy, and finally latex-suite did as well. BTW, lh-brackets should be compatible with an installed version of latex-suite)

NB: for the ones that absolutely want to define the mappings themselves, look at the Map*Context() functions from lh-brackets. The first version recognizes comments and string contexts to not expand the key. The last version (Map4TheseContext()) permits to specify how the key should be expanded for a list of possible contexts.
The idea is to test the context with synIDattr(synID(line('.'),col('.')-1,1),'name'), then to interpret possible special character sequences like <esc>.

Upvotes: 3

Related Questions