2

I would like to create a shortcut for the spell checker, since it requires a relatively long command. I inserted the following map in the .vimrc file:

inoremap <F5> <C-O>:set spell spelllang=en_gb<CR>

But nothing happens when I press the F5 key. What would be the right map?

4
  • 1
    Are you pressing F5 while in insert mode? The mapping you wrote is for insert mode only... Maybe you want a similar mapping for normal mode as well?
    – filbranden
    Jul 8 '20 at 13:53
  • 1
    Slightly tangentially, it's more usual to set 'spell' and/or 'spelllang' more permanently, for instance in a filetype plugin (or even just in your _vimrc), which means you'd generally not need to manually switch it on at all.
    – Rich
    Jul 8 '20 at 13:59
  • Also "nothing happens", depending on the content of your buffer it might be normal that nothing happens. Have you tried running running the commands "manually" (i.e. without a mapping)? What are you expecting to happen?
    – statox
    Jul 8 '20 at 14:01
  • @filbranden That seems to be the case. I modified the inoremap to noremap (did not know about this. But now all sorts of weird things happen. F5 and F6 seem to have their own functions in Normal mode. In any case, if you file an answer I am glad to accept it. Jul 8 '20 at 14:16
3

It seems you were trying to use this mapping by pressing F5 from Normal mode, while the mapping you created is only valid for Insert mode.

You can duplicate this mapping so that it also works in Normal mode. If you do so, you should omit the <C-O> part, since that's only needed to go back to Normal mode, for a single command, while you're in Insert mode. (See :help i_CTRL-O for details on that command.)

(If you keep the <C-O> in the Normal mode mapping, it will execute the Normal mode CTRL-O command, which jumps back to the last cursor position in your jump list.)

inoremap <F5> <C-O>:set spell spelllang=en_gb<CR>
nnoremap <F5> :set spell spelllang=en_gb<CR>

Or you could just reuse the normal mode mapping from the Insert mode one:

nnoremap <F5> :set spell spelllang=en_gb<CR>
imap <F5> <C-O><F5>

In that case, the Insert mode mapping needs to be a recursive mapping (use imap rather than inoremap), in order to be able to use the other Normal mode mapping you created.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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