3

Programming in Javascript and similar syntax languages we use a lot of {} and (), more so than [] and the 90. Therefore we end up using SHIFT a lot.

Is there a way to make it so that when I type [ it actually outputs { - SHIFT+[.

Basically I want SHIFT to work opposite on (), [] and & and |, so when I type SHIFT+[ it outputs [ and if I type just [ it outputs {.

The idea is to have one less key stroke to output these special chars as I use than more than the "unshifted" chars.

  • I'm unclear on what you're wanting to do with & and |. Are you wanting them swapped with 7 and ``? Or is your keyboard layout already different than mine, and you're wanting to swap them with different keys? – Ed Grimm Jun 28 at 4:06
  • My keyboard has 7 and & on the same key, and \ and | on the same key. Basically I use more & for and conditions then 7 so I'll swap then as well. The same for \ | as I use more | than \ when programming. I follow the pattern from your answers. Thanks a lot! – user1894919 Jun 28 at 4:25
2

If you're using vim, you want a :noremap variant. To apply this for just the current editing session, and just in insert mode,

:inoremap [ {
:inoremap ] }
:inoremap 9 (
:inoremap 0 )
:inoremap 7 &
:inoremap \\ \|

Note that the last one is different. \ and | are both special characters in vim's editor mode, they need to be escaped. I'm pretty sure in other vi variants, you'll need to escape the \, but I don't know if you'll need to escape the | or not.

Since you still want to be able to insert the characters you just mapped away, map the others back to those. Since we're using the :inoremap command rather than the :imap command, this works fine. (If you tried that with :imap, the following would make an infinite loop.)

:inoremap { [
:inoremap } ]
:inoremap ( 9
:inoremap ) 0
:inoremap & 7
:inoremap \| \\

Because these commands are limiting the change to just insert mode, the first set shouldn't interfere with typing the second set. That said, you can also do them all on one line:

:inoremap [ {|inoremap ] }|inoremap { [|inoremap } ]|inoremap 9 (|inoremap 0 )|inoremap ( 9|inoremap ) 0|inoremap 7 &|inoremap \\ \||inoremap & 7|inoremap \| \\

These commands are suitable for use in a .vimrc file, either to set them globally or as part of an autocommand.

:autocmd BufEnter *.html :inoremap [ {|inoremap ] }|inoremap { [|inoremap } ]|inoremap 9 (|inoremap 0 )|inoremap ( 9|inoremap ) 0|inoremap 7 &|inoremap \\ \||inoremap & 7|inoremap \| \\
  • That is awesome and it worked, thanks a lot! I am a neovim user and I already found that on neovim I have to use :ino instead of :inoremap. – user1894919 Jun 28 at 4:25
  • 2
    @user1894919 :ino is just the abbreviation for :inoremap. I checked the current nightly build of neovim (v0.4.0-1159-g35b959c60) and :inoremap works as expected. – Ralf Jun 28 at 6:16

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.