Jakub February 2016

vim cmap lhs only at the beginning

I have a mapping :cnoremap ch call ShowHistoryMatching

The problem is that the ch characters expand to the right sentence in any case they are typed, no matter if at the beginning or later in the cmap input.

The problem is when I try to search for words in vim using / or ? e.g. for /cache - it will be expanded using the mapping above.

How can I set the mapping ch to be extended only when it occurs at the beginning of the command?

Answers


Peter Rincker February 2016

cmap's are notoriously tricky because they often execute in the wrong context. Some better alternatives:

Personally I would just create a new command.


Ben February 2016

You can use the getcmdpos() function to determine if you're at the beginning of the line or somewhere else. This technique can replace a built-in command using an abbreviation or you can adapt it for use in a mapping, possibly with an <expr> mapping.

Post Status

Asked in February 2016
Viewed 2,428 times
Voted 5
Answered 2 times

Search




Leave an answer