7

I have a text file where I keep my personal notes. They're structured in a certain way, so I can operate on those notes with vim commands. I would like to map a keyboard shortcut to a specific set of commands, but that mapping should be available only in this file. So I tried writing a modeline at the end of my file, like this:

# vim: nmap \a A [a]<ESC>

Unfortunately, that didn't work: when opening the file, vim says

Error detected while processing modelines:
line  273:
E518: Unknown option: nmap

How do I set a mapping that only works in this file? Preferably without writing to .vimrc or another file. Also, my file doesn't have a filetype, it's just text, and the file doesn't have any extension.

2
  • 4
    You don't, modelines are run in a restricted sandbox. You can however set a filetype in a modeline, then add your map (with the <buffer> qualifier if you want, cf. :h :map-<buffer>) to a corresponding ftplugin file. The filetype can be a made-up name, it doesn't have to be a well-known type. Aug 10, 2016 at 10:51
  • @SatoKatsura I was hoping it worked without a filetype, but defining one for my file inspires me to "think of the possibilities". Thank you. Please move your comment to an answer, I'll accept it.
    – CamilB
    Aug 10, 2016 at 11:01

1 Answer 1

8

Moving to answers, as requested.

Modelines are run in a restricted sandbox, there is only a limited number of things they can do, and defining keyboard maps is not among those.

You can however set a filetype in a modeline, then define a map in a corresponding ftplugin file. If you add the <buffer> qualifier the map will be defined only for the buffers of that filetype (cf. :h :map-<buffer>).

Also, the filetype doesn't have to be a "well-known" name, a made-up name would work just as well as a well-known one.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.