2

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?

Jakub
  • 393
  • 1
  • 4
  • 18

2 Answers2

2

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

Personally I would just create a new command.

Community
  • 1
  • 1
Peter Rincker
  • 43,539
  • 9
  • 74
  • 101
  • You showed approaches for my issue. I read about 3rd and 4th now and might read later. I'll stay with 1st approach for now and if it will be annoying I'll switch to the others. Thank you. – Jakub Feb 08 '16 at 23:02
  • I use the command solution combined with as found here: https://stackoverflow.com/a/3213800/428399 – Jakub Feb 09 '16 at 08:24
0

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.

Ben
  • 8,725
  • 1
  • 30
  • 48
  • Interesting, thank you. I decided to use the command solution combined with as found here: stackoverflow.com/a/3213800/428399 – Jakub Feb 09 '16 at 08:25