Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mapping the shift key in vim

I'm having an issue when mapping the Shift key in VIM. I want Ctrl+L to behave differently than Ctrl+Shift+L

so I have this

" for insert mode remap <c-l> to:
" Insert a hash rocket  for ruby
" Insert a -> for php
" for coffee the shift key decides

function! SmartHash(...)
  let shift = a:0 > 0
  let ruby = &ft == 'ruby'
  let php = &ft == 'php'
  let coffee = &ft == 'coffee'

  if php
    return "\->"
  end

  if coffee
    return shift ? "\ =>\n" : "\ ->\n"
  end

  if ruby
    return "\ => "
  end

  return ""

endfunction

imap <c-l> <c-r>=SmartHash()<cr>
imap <C-S-L> <c-r>=SmartHash(1)<cr>

...but it's just triggering the second mapping whether I have the Shift key pressed or not.

Updated based on accepted answer

" for insert mode remap <c-l> to:
" Insert a hash rocket  for ruby
" Insert a -> for php and coffeescript
" double tapping does alternate symbol for php and coffescript
function! SmartHash(...)
  let alt = a:0 > 0
  let ruby = &ft == 'ruby'
  let php = &ft == 'php'
  let coffee = &ft == 'coffee'

  if php || coffee
    return alt ? "\ =>\n" : "\ ->\n"
  end

  if ruby
    return "\ => "
  end

  return ""

endfunction

imap <c-l> <c-r>=SmartHash()<cr>
imap <c-l><c-l> <c-r>=SmartHash(1)<cr>
like image 506
Christian Schlensker Avatar asked Feb 20 '23 16:02

Christian Schlensker


1 Answers

Due to the way that the keyboard input is handled internally, this unfortunately isn't possible today, even in GVIM. This is a known pain point, and the subject of various discussions on vim_dev and the #vim IRC channel.

Some people (foremost Paul LeoNerd Evans) want to fix that (even for console Vim in terminals that support this), and have floated various proposals, cp. http://groups.google.com/group/vim_dev/browse_thread/thread/626e83fa4588b32a/bfbcb22f37a8a1f8

But as of today, no patches or volunteers have yet come forward, though many have expressed a desire to have this in a future Vim 8 major release.

I usually work around this by starting my alternate mapping with g, e.g. <C-l> and g<C-l>, but that won't work in insert mode. In your case, with a little bit of additional logic, you could make a second <C-l> at the same position change the previously inserted -> to =>, so that you get the first alternative via <C-l>, and the second via <C-l><C-l>.

like image 130
Ingo Karkat Avatar answered Feb 28 '23 02:02

Ingo Karkat