1

I am using two different keyboard layouts (depending on the computer I use) on the same server (one being AZERTY, the other QWERTY).

I would like to have "conditional" mapping options in my .vimrc file, depending on my layout, something like :

if (keyboard_layout == QWERTY)
    nnoremap : ;
    nnoremap ; :
elseif (keyboard_layout == AZERTY)
    inoremap :w <Esc>:w<Return>
endif

Is it possible ? Or is there a "workaround" not too heavy ?

(sorry for the syntax, I haven't coded much .vimrc files yet)

Edit

User @statox gave a very complete answer based on another question asked, I could not test it because I am not 'root' user on the server I'm working on.

I found another way through defining a function, that I posted as answer here.

6
  • 3
    Depending on your system you probably have an environment variable or a configuration file which contains your current layout. You could read it from your .vimrc and create a condition on it. The problem is that it might be a problem to make this code portable since (as far as I know) not all the distributions keep this information in the same place.
    – statox
    Commented Apr 18, 2016 at 15:27
  • 1
    For the users coming to this question I think OP solved his problem by asking this other question
    – statox
    Commented Apr 19, 2016 at 11:40
  • I tried to, but in the end it didn't solve my problem : the $LANG variable does not contain the keyboard layout. In the end I built a function I have to call if I want to change my mappings.
    – Feffe
    Commented Apr 19, 2016 at 12:53
  • What system do you use? Sometimes it is in a configuration file instead of a variable.
    – statox
    Commented Apr 19, 2016 at 12:55
  • @statox : I use a CentOS Linux or a MacOS X, but I always ssh to the server on which I'm working
    – Feffe
    Commented Apr 19, 2016 at 12:55

2 Answers 2

2

What I would recommend is the following:

  • First on your local machine set an environment variable manually containing your layout:

    export LAYOUT=AZERTY
    
  • Then configure ssh to receive this environment variable (Thanks to this question ):

    To do so use the ~/.ssh/config file and the SendEnv command. A very basic example would be:

    Host server
        HostName dev.example.com
        SendEnc LAYOUT
    

    you can then ssh to your server with ssh server. (There a lot of documentation of ssh config, you can do a lot of stuff with that).

  • You also have to configure the server to accept this new variable: In /etc/ssh/sshd_config add the following line (as root):

    AcceptEnv LAYOUT
    
  • Finnaly on the server side you can use the answer to this question to read your variable and make your mappings accordingly:

    if $LAYOUT == 'AZERTY'
    …
    endif
    
1
  • Thanks for the detailed proposition -- one problem though : I am not (and will never be) root on the server I'm using. Hence I wrote a function in my .vimrc Kbdmodes(lang), where 'lang' can be 'qwerty', 'azerty'. My problem is now to call it "automatically" when vim starts... I'll probably ask a new question if I can't figure it out.
    – Feffe
    Commented Apr 19, 2016 at 13:18
1

I did not use any "environment" variable, but I coded a function that I call in the .vimrc file :

" The function in my .vimrc file :
function! Kbdmodes(lang)
   if a:lang=='en'
      imap <akey> <anotherkey>
   elseif a:lang=='fr'
      imap <yetanotherkey> <iamthefourthkey>
   endif
endfunction

let lang='en'
call Kbdmodes(lang)

So far it seems to work. By default, upon starting, vim loads my 'qwerty' (english) bindings ; then if I do :call Kbdmodes('fr') in normal mode, it switches the bindings to my 'azerty' (french) ones.

I hope this can be useful to anybody !

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.