• Remapping keys on macOS

    I ramble a bit here - you may want to skip to how to remap keys on macOS.

    I attended WWDC for the first time ever (making lemonade in the COVID-19 situation), and after watching the keynote, got pretty excited about macOS Big Sur.

    I wanted to install the macOS Big Sur developer beta but not at the expense of making my main development MacBook Pro virtually unusable. One of the things I thought I really needed working was Karabiner-Elements, which I use to remap keys, specifically:

    • Caps Lock to Esc
    • Esc to `

    This particular remapping is really because I use ` (backtick) as my tmux prefix key. I picked this up from an ex-colleague after watching him fly while switching tmux panes. And I use either a compact 60% or 65% keyboard which meant I didn't have a dedicated ` key - I'd have to hit Fn-Esc which is even more terrible than the default of Ctrl-B.

    Photo of Escape key

    Unsurprisingly, Karabiner-Elements was not working in Big Sur. Not satisfied with that, I quickly found that macOS has supported native key remapping since macOS Sierra via the hidutil command-line tool. If you're not beyond scrappily writing some JSON and XML, this just works:

    1. Decide what keys you want to remap. In my case, I wanted to remap Caps Lock to Esc.
    2. Apple's docs say:
      The keys take a hexadecimal value that consists of 0x700000000 or’d with the desired keyboard usage value
      Pretty cryptic, but what it really means is you just have to add 0x700000000 to your key's "usage ID" in this table (techically, this is a bitwise OR, which is not addition, but the usage IDs are small enough for it to not matter).
    3. Caps Lock's usage ID is 0x39. Adding 0x700000000 to that gives 0x700000039.
    4. Esc's usage ID is 0x29 - we'll use 0x700000029.
    5. Caps Lock (0x700000039) is the source, so we will specify "HIDKeyboardModifierMappingSrc": 0x700000039.
    6. Esc (0x700000029) is the destination, so we will specify "HIDKeyboardModifierMappingDst": 0x700000029.
    7. Repeat for each pair of keys you want to remap.
    8. Try it out in the command-line:
      hidutil property --set '{"UserKeyMapping":[
        {
          "HIDKeyboardModifierMappingSrc": 0x700000039,
          "HIDKeyboardModifierMappingDst": 0x700000029
        },
        {
          "HIDKeyboardModifierMappingSrc": 0x700000029,
          "HIDKeyboardModifierMappingDst": 0x700000035
        }
      ]}'
    9. This remapping will be undone whenever you reboot your Mac, so one way to persist the remapping is to run the hidutil command every time your Mac boots. For that, we can rely on Launch Agents.
    10. Create a file named ~/Library/LaunchAgents/com.example.KeyRemapping.plist: This file basically tells macOS to run the hidutil tool when it starts up.
    11. You're all set!

    A vast improvement would be to generate the key remappings from an HTML form #ideas-are-free.

    I really love Karabiner-Elements, but my use of it has become less complex of late. There's also something to be said about running less software. And that brings me a tiny step closer to trying out Big Sur - yes, I can't take the plunge yet! You can evaluate whether you are ready after taking a look at this list of apps that are not working on macOS.

  • Installing Powerline on OS X + homebrew

    I've always wanted to get that fancy Powerline status bar and prompt. Chevrons and git branch icons in the terminal just sounds so... defying:

    Powerline screenshot

    I'd failed to get it working properly before because of a combination of outdated Powerline docs and confusion between OS X's system python/vim and homebrew's python/vim. Some of my former colleagues detailed the pains and hours lost getting Powerline setup, so I gave up.

    Anyway, I got a new Mac for work recently and decided to try again, and it was easier than I thought. You (including you, future Chu Yeow) can use this guide if you're using homebrew to install vim and python (and everything else like zsh and tmux).

    Installing Powerline on OS X

    1. Install python with homebrew: brew install python
    2. Install vim with homebrew: brew install vim --env-std --override-system-vim. You must install vim after python so that it'll compile with homebrew's python.
    3. Install powerline with pip: pip install https://github.com/Lokaltog/powerline/tarball/develop. Powerline should get installed to /usr/local/lib/python2.7/site-packages/powerline.
    4. open /usr/local/lib/python2.7/site-packages/powerline - you should see the files required for integration with zsh, vim, tmux, etc. in the bindings directory. All you need to do now is to include these bindings in your zsh, vim and tmux config files - it's that simple!
    5. Add Powerline to vim by adding these lines to ~/.vimrc:
      source /usr/local/lib/python2.7/site-packages/powerline/bindings/vim/plugin/powerline.vim
      set laststatus=2
    6. Add Powerline to tmux by adding this to ~/.tmux.conf:
      source /usr/local/lib/python2.7/site-packages/powerline/bindings/tmux/powerline.conf
    7. Add Powerline to zsh by adding this to ~/.zshrc:
      source /usr/local/lib/python2.7/site-packages/powerline/bindings/zsh/powerline.zsh

      If you use oh-my-zsh, be sure to source the Powerline bindings after oh-my-zsh's.

    8. Grab a patched font from https://github.com/Lokaltog/powerline-fonts (I use Inconsolata), and install it (just download, double-click the font file, and Install). If you don't use any of these fonts, you'll have to patch your favorite font yourself (have fun with that!)

    That's all you need! Go crazy and start tweaking your Powerline colorscheme.

  • Summer 2011 anime (and some from Spring 2011)

    Some of you might know that I'm a rabid anime fan. I don't talk much about it on this blog (actually, I don't talk much on this blog anymore, seeing as I haven't blogged this year). Anyway, yeah, one of my biggest passions and failings is my love for anime and I can't stop myself from collecting those cute Nendoroids too.

    This summer 2011 season, I'm watching 4 new series and 2 older series that started in spring 2011, Hanasaku Iroha and Nichijou:

    Currently watching anime, Summer 2011

    Top row: Usagi Drop, Mawaru Penguindrum, Ikoku Meiro no Croisée.
    Bottom row: No.6, Hanasaku Iroha, Nichijou.

    I'm a sucker for the slice of life genre (5 out of 6 series above are "slice of life"). Usagi Drop in particular is very promising - looking forward to how Daikichi and Rin's story unfolds. Mawaru Penguindrum is probably the winner in terms of "interestingness" though - after all, it has penguins. After the brilliant Ano Hana from spring 2011, Usagi Drop is looking to be my mainstay for this season.

    If you're an anime fan, please leave a comment on what you're watching or ping me on twitter (I'm @chuyeow) - I'd love to know what you guys like to watch when you're not coding.

  • Firefox nightly builds add a combined Stop/Go/Refresh button to the Location Bar

    In an effort to streamline Firefox's UI, the latest Firefox nightly builds have moved the Stop/Refresh button into the right corner of the Location Bar. I like this move even if the Firefox developers are simply copying design ideas from other browsers (in this case, I believe Safari was first).

    When you're typing in the Location Bar, a green Go button is shown:

    That switches to a red Stop button when the page is loading:

    The thin line you see in the location bar is a progress indicator that indicates how much of the current web page has loaded - I don't like the way it looks and I think it'll probably change in future.

    When the page has fully loaded, it changes to a Refresh button:

    This leaves a minimalist and compact toolbar:

    I should actually remove the Home button - I don't ever use it.

    To try out the new stuff coming in Firefox, download a Firefox nightly build now.

  • Rendering plists from your Rails app

    Jeena emailed me recently about his Rails plugin, plistifier, that allows you to render plists (property lists).

    It allows you to render your ActiveRecord objects as plists (among other things):

    def show
      @post = Post.find(params[:id])
    
      respond_to do |format|
        format.xml   { render :xml => @post }
        format.plist { render :plist => @post }
      end
    end

    Jeena was kind enough to let me know he "stole" my old Jsonifier plugin (github) code in plistifier.

subscribe via RSS