Thomas Möbius
Thomas Möbius

Reputation: 1782

Apply different key mappings when within or outside a comment

I have the following key mappings in my ~/.vimrc

" Format paragaph
au FileType markdown nnoremap <buffer> <space> gwip

" Send current line to tmux usind SendToTmux
au FileType python,sh,zsh nnoremap <buffer> <space> yy:call SendToTmux(@")<cr>j

I would like vim to apply / use the markdown key mapping, whenever I am in a line or a paragraph of comments.

Upvotes: 0

Views: 60

Answers (1)

Luc Hermitte
Luc Hermitte

Reputation: 32966

You'll have to analyse the current context with synIDattr(synID(line('.'),col('.')-1,1),'name') like for instance:

:nnoremap <buffer> <expr> <space>  (synIDattr(synID(line('.'),col('.')-1,1),'name') =~? 'comment\\|doxygen') ? ':echo 1<cr>' : ':echo 2<cr>' 

Upvotes: 2

Related Questions