2

I often use UltiSnips for other file types, but for HTML I'd like to use Emmet-vim.

However, I'd like to use the same trigger for both. For this I have

let g:user_emmet_expandabbr_key='<C-j>'
let g:UltiSnipsExpandTrigger='<C-j>'

in my .vimrc.

Unfortunately, it seems that by assigning the same trigger to both makes Emmet-vim not work (it works if I comment the UltiSnips line).

Is there a way to use <C-j> for both?

Thank you!

2 Answers 2

2

Following the suggestion of @MartinTournoij

let g:UltiSnipsExpandTrigger='<C-F12>'
let g:user_emmet_expandabbr_key='<C-S-F12>'

function! IsEmmet(filetype)
  echom a:filetype

  if a:filetype == 'html'
    return v:true
  endif

  if a:filetype == 'css'
    return v:true
  endif

  return v:false
endf

imap <expr> <C-s> IsEmmet(&filetype) ? "\<C-S-F12>" : "\<C-F12>"
2
  • 2
    Nit: I prefer v:true and v:false to 1 and 0 when working with booleans
    – D. Ben Knoble
    Jul 21, 2022 at 13:31
  • You can also use a <Plug> mapping that doesn't interfere with existing keystrokes.
    – filbranden
    Jul 21, 2022 at 22:17
0

I propose you the following:

let g:UltiSnipsExpandTrigger='<C-F11>'
let g:user_emmet_expandabbr_key='<C-F12>'

function! SetUltiSnipsMap()
  imap <C-j> <C-F11>
endf

function! SetEmmetMap()
  imap <C-j> <C-F12>
endf

augroup snippettrigger
  autocmd!
  autocmd BufWinEnter *.* call SetUltiSnipsMap()
  autocmd WinEnter *.* call SetUltiSnipsMap()
  autocmd BufWinEnter *.html call SetEmmetMap()
  autocmd WinEnter *.html call SetEmmetMap()
augroup END

The idea is to make vim the value of the global variable when you enter in a buffer or when you switch to a buffer of type .html.

The first two lines are called to restore the default first.

6
  • 1
    while this looks like exactly what I want, it doesn't seem to work. indeed I can check that the variables are defined as in your script depending on whether I'm in an html file or not. but neither ultisnips in non-html files, nor emmet in html files work with <C-j>. maybe I need to reload the plugin somewhere?
    – ElPC
    Jul 20, 2022 at 16:33
  • I would look in the code of the plugin to see how the plugin map the shortcut and add this code to the autocar. Jul 20, 2022 at 19:56
  • 1
    @ElPC The plugins will setup the mappings when loaded, and not for every buffer, so that's why this doesn't work. What you can do is set the mappings to something you don't use (<F13> and <F14> might work), and then switch the mapping of <C-j> to that similar to what this answer is doing (e.g. nnoremap <C-j> <F13>). Jul 21, 2022 at 2:33
  • Thanks @MartinTournoij, I have adapted the solution to your suggestion. I have tested on Windows 10 and it works fine. Jul 21, 2022 at 5:05
  • 1
    This should work as well I think (didn't test): imap <expr> <C-j> &filetype->split('\.')->index('html') == -1 ? "\<C-F11>" : "\<C-F12>" Jul 21, 2022 at 5:21

Your Answer

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

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