0

The question is actually rather short, don't be afraid. :-)

I'm Russian, and so to tweak Vim in a way it will understand its commands even if a Russian language is currently active, I have the following part in my vimrc:

set keymap=russian-jcukenwin " jcukenwin for Windows and Linux,
                             " jcukenmac for macOS

set iminsert=0
set imsearch=0

set laststatus=2

" To change the color of statusline when keyboard layout is changed
function MyKeyMapHighlight()
  if &iminsert == 0
    highlight tabLine    ctermfg=darkBlue guifg=darkBlue guibg=darkGrey
    highlight StatusLine ctermfg=darkBlue guifg=darkBlue guibg=Gray30
  else
    highlight tabLine    ctermfg=darkBlue guifg=darkBlue guibg=darkGrey
    highlight StatusLine ctermfg=darkRed  guifg=darkRed  guibg=Gray30
  endif
endfunction

call MyKeyMapHighlight()
autocmd WinEnter * :call MyKeyMapHighlight()

" To switch languages by using Control-Space instead of C-^ (C-6 on Windows)
cmap <silent> <C-Space> <C-^>
imap <silent> <C-Space> <C-^>X<Esc>:call MyKeyMapHighlight()<CR>a<C-H>
nmap <silent> <C-Space> a<C-^><Esc>:call MyKeyMapHighlight()<CR>
vmap <silent> <C-Space> <Esc>a<C-^><Esc>:call MyKeyMapHighlight()<CR>gv

The only part which interests me currently is mappings. I'm trying to understand where to put and where to not put spaces when we define mappings. For example, if I delete spaces after <C-Space>, it doesn't work.

" Doesn't work
cmap <silent> <C-Space><C-^>
imap <silent> <C-Space><C-^>X<Esc>:call MyKeyMapHighlight()<CR>a<C-H>
nmap <silent> <C-Space>a<C-^><Esc>:call MyKeyMapHighlight()<CR>
vmap <silent> <C-Space><Esc>a<C-^><Esc>:call MyKeyMapHighlight()<CR>gv
  1. Why the second version doesn't work?
  2. Are there explanations/rules/best practices where to put and not put spaces when we define mappings in Vim?

1 Answer 1

1

Why the second version doesn't work?

Because there is nothing mapped to <C-Space><C-^>.

Space separates a sequence of keys and a mapped value.

Are there explanations/rules/best practices where to put and not put spaces when we define mappings in Vim?

:h :map is concise indeed. :map {lhs} {rhs} should tell you that there should be a space between {lhs} and {rhs}. So your first mapping {lhs} is <C-Space> and {rhs} is <C-^>.

Mappings could have arguments (in your case it is <silent>) that should be put before {lhs}. See :h :map-arguments.

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.