Friday, July 24, 2015

Vim: Rebind Caps Lock And Escape

In this article I discuss how I managed to bind Escape to my Caps Lock key and Caps Lock to Escape, not only for my OpenBox environment, but also for my Virtual Terminal/Console.

I recently decided to finally give Vi/Vim a try. Previously, I thought it was too arcane and tried Emacs instead. I was an avid Emacs user and it became my editor and IDE of choice. So why did I decide to switch?
My decision came down to three (3) things:
1. Why not?
2. Striving to be more productive (i.e. lazier...i.e. less keystrokes)
3. Vi is installed by default on Unix, so an advanced editor is always ready. So are ee, ed and a whole range of others, but I like the challenge and wanted to see what the rage was all about. Yes Emacs came with Mac OS X by default albeit an older version, so I still had to install the latest. On FreeBSD? pkg install emacs is your friend.
While using Emacs on OS X, I could easily remap the keybindings for Caps Lock to Control, in fact, it was a builtin feature of the OS. It's a bit more work to get it done on FreeBSD, so here goes.

OpenBox setup:

This was taken from the vim wikia:

  1. vi ~/.vimkeyswapper – you can name the file anything you want, I just wanted the easy reminder everytime I looked at it. This was easier to understand at a glance over '.xmodmaprc'.
  2. Edit the file to include the following contents:
    ! Swap CAPS LOCK and ESC
    remove Lock = Caps_Lock
    keysym Escape = Caps_Lock
    keysym Caps_Lock = Escape
    add Lock = Caps_Lock
  3. Edit either your ~/.config/openbox/autostart if you are using a login manager or your ~/.xinitrc if, like me you only need to start X when you really need to
    xmodmap ~/.vimkeyswapper
  4. Restart X, or reboot just to make sure it takes effect.

Console setup:

Alright, be careful here. We're going into the file system, which requires root privileges. Ironically, I found the original solution here.
  1. We need to decide which keyboard mapping we are using. So you'll need to install misc/kbdscan if you don't already have it installed.
    $ cd /usr/ports/misc/kbdscan && sudo make install clean
  2. Please note, you have to be in the console to be using this, so you can't be logged into X and use your terminals. So ALT+F1 or ALT+F(n) to whichever console you like and login.
    $ kbdscan
    Press and release Caps Lock and make a note of which number appears, for me it was 58
    Press and release Esc, the number I got was 1.
  3. Okay, you may have to do some digging around to find which files match the numbers you got.
    $ cd /usr/share/syscons/keymaps && ls
    Based on your locale, language or keyboard setup, it may be different for you. The file that matched my bindings were us.iso.kbd. [1]
    $ sudo cp us.iso.kbd us.vim.kbd
  4. Edit the file, so it should look something like this:
    # $FreeBSD: releng/10.1/share/syscons/keymaps/us.iso.kbd 74119 2001-03-11 23:41:19Z ache $
    # alt
    # scan cntrl alt alt cntrl lock
    # code base shift cntrl shift alt shift cntrl shift state
    # ------------------------------------------------------------------
    000 nop nop nop nop nop nop nop nop O
    001 clock clock clock clock clock clock debug clock O
    ...
    058 esc esc esc esc esc esc esc esc O
    [2]


    I don't know if it made a difference or not, but I realigned the text before saving the file.
  5. Add this to your /etc/rc.conf file: keymap="us.vim.kbd"
  6. Reboot: # reboot OR $ shutdown -r now
  7. And there you have it, if done correctly, your Caps Lock will now be your new Esc key. Happy coding![3]
[1] – On a side note, they already have us.emacs.kbd, no fair to us vim users :-)
[2] – Ellipses denotes more content, I just did this to highlight the relevant changes. Please don't remove anything from this file.
[3] – I am not sure if this affects console only or the X environment as well, making the xmodmap command unneccessary.

Tuesday, July 21, 2015

FreeBSD 10.1: Cabal Update Version Issues

Sometimes there is an issue with % cabal update, where after updating and testing the update with % cabal --version, you will still see the previous version. I had this issue when I was running Mac OS X 10.9 and 10.10 but they were easily resolved by just pointing to the correct PATH, e.g: export PATH="$HOME/Library/Haskell/ghc-7.8.3-x86_64/lib/cabal-install-1.20.0.3/bin:$PATH"

On FreeBSD, it's also an easy fix, again by ensuring you set your $PATH correctly, but this time ensuring it's ~/.cabal/bin:/usr/local/bin: . An issue I found with this, is that during some cabal installs, I'd see an error message gcc -not found even though whereis gcc reports /usr/local/bin/gcc48. To resolve this, I set my $PATH thus: /usr/local/bin:~/.cabal/bin:, which lead to 'cabal --version' not displaying the correct version number.

On a side note, I could just use the --with-gcc=/usr/local/bin/gcc48 flag, but I'm too lazy to type all that and too forgetful to remember.

This is how I chose to resolve it.

% sudo mv /usr/local/bin/cabal /usr/local/bin/cabal-1.18.0.5
% sudo ln -s ~/.cabal/bin/cabal /usr/local/bin/cabal

Effectively, I renamed the original cabal to include it's version number, in case I may ever need it later, then I created a symbolic link which pointed to my user copy of cabal, which will always be the updated version. So, I can now do cabal updates and cabal installs knowing that gcc will always be found and I'll always be using the most up to date cabal version.

Monday, July 13, 2015

Installing Adeskbar on FreeBSD

Why?

On not so powerful hardware, squeezing every last ounce of juice from your limited resources are vital especially when you're a developer. Time is very important, and so waiting for a program to compile or watching your IDE slow to a crawl just trying to auto-complete the name of a function, or even watching your mouse cursor that you're used to blinking be a constant pixel on your screen, makes your soul scream as precious seconds waste away before your eyes.

So when I got a chance to finally install FreeBSD on my own hardware, I jumped on it. Unfortunately, said hardware isn't as powerful as I want it to be right now for my daily development, it's an Acer E 15 Start [1], but it's mine. So as much as I could, my X environment has to be as minimal as possible.

I know there are tons of Desktop Environments (DE) and Window Managers (WM) to choose from, I thought XFCE4 was lighweight enough as I had installed that on a previous PC-BSD setup I had, but that was on much better hardware, so it was blazingly fast, but on my current hardware, the Application Launcher menu took ~3 seconds to load on average [2], yes, the menu to launch my applications took a while to appear before I could launch my applications. Now what's the big deal with 3 seconds? I'm dual booting with Windows 8.1 [3] and Windows 8.1 on the same hardware is surpisingly and pleasantly responsive, enviously so, and I can't have my main OS FreeBSD be shown up like that. So I did a lot of reading and did a lot of testing and although I thought LXDE would have been faster, and at least not let me feel bad about jumping from XFCE4, which honestly, I do enjoy and would install it on anyones computer, LXDE for some reason just wasn't the same. BUT it was faster, and making this possible was OpenBox. OpenBox? What is that, I've never heard of it before...I did more reading and realized something. What if I just ran OpenBox by itself, how much more responsive could my desktop be? 

# cd /usr/ports/x11/lxde-meta && make rmconfig-recursive deinstall
# cd /usr/ports/x11-wm/openbox && make config-recursive install clean

I now had OpenBox and a gray background...Simple, minimal, divine. In the first time using an OS, I realised, everything else was unneccessary. However, after shaking myself out of it, I soon realised, there were somethings that were necessary for me to be and feel productive, especially if I wanted to replace Windows 8.1 and get over the feeling of missing the MacBook Pro I had been using for over three (3) years. So I moved on to see what others were doing with their FreeBSD + OpenBox setup and stumbled across this forum explaining how to have a minimal FreeBSD desktop. How I wish I saw this first, and how much more I wanted to do a full reinstall of FreeBSD, but after 2 weeks of struggling, and not wanting to waste any more time, I decided to make the best of it, so I opted for Tint2 and Wbar.

Wbar - minimal, lightwieght, easy to configure. I quickly found out it's limitation, and it was the one thing I needed, it doesn't support SVG, and doesn't plan to anytime soon, unless imlib2 decides to. I found this out from here.

I thought cairo-dock aka GLX-dock would have been the answer, and would give me that OS X feel, but after # make config-recursive install clean I realized how wrong I was. One hour of installation, and to top it off, my hardware doesn't support OpenGL, which means space on my HDD is being taken up for eye-candy that I can't see or utilize. I tried making the best of it and it worked after a fashion, but the bulkiness and underutilizatoin of it was getting to me. So back to google. I found this wonderful article which confirmed a lot for me and also introduced me to Adeskbar, which was created by David Art. The caveat, Adeskbar isn't ported to FreeBSD, which means a manual install. Google turned up nothing helpful on how to get it done, so I went through the README and following are my steps to get Adeskbar up and running in my environment, I had to have it.

What?

These are the steps I had initially taken.

1. Downloaded adeskbar tar.bz2 from official site
2. $ tar yxf adeskbar-0.4.3.tar.bz2
3. $ mv adeskbar-0.4.3 ~/adeskbar-0.4.3 && cd adeskbar-0.4.3
4. $ sudo ./install.sh
5. $ adeskbar
6. annnddd...Nothing.
7. $ cat install.sh (Reveals where the files were moved, and adeskbar.desktop was renamed to applications...hmm, ok)
8. $ sudo mv /usr/share/applications /usr/local/share/applications/adeskbar.desktop
9. $ sudo ln -s /usr/local/share/applications/adeskbar.desktop /usr/share/applications
10. copied default.conf to ~/.config/adeskbar/
11. $ adeskbar default …. repeat step 6
12. $ cat ~/.config/adeskbar/output.log
> /usr/bin/adeskbar: /usr/share/adeskbar/main.py: not found
13. $ ls /usr/bin/adeskbar
> main.py (listed) ??? I found this article Still Stupid (http://stillstup.blogspot.com/2013/03/new-adeskbarinstall.html)
14. $ whereis python : /usr/local/bin/python
15. $ sudo emacs main.py (updated path based on whereis: #!/usr/local/bin/python)
16. Repeat step 5, 6 and 11.
17. This time /usr/share/adeskbar/ui.py complains about not finding gmenu. I have gnome-menus installed.

After all of that, I realised I would never get gmenu installed. Soo, I found and downloaded adeskbar-5.0, changed all the paths in install.sh, adeskbar.sh and uninstall.sh to reflect /usr/local/share instead of /usr/share.

Then:
1. $ adeskbar &
2. and …. IT'S ALIVE
3. edited ~/.config/openbox/autostart to include (sleep 4 && adeskbar) &

Tweaks...

Okay, it works but not quite. I had to do a few more things. First I followed the advice in the comments section and did
$ ln -s /usr/local/etc/xdg/menus/gnome-applications.menu /usr/local/etc/xdg/menus/applications.menu
I also installed py27-dbus, not sure if I needed to or not.

You can use ~/.config/adeskbar/output.log to guide you, but based on my errors, this is what I did.

1. NameError: name 'pyinotify' is not defined.
  • Pyinotify is listed as optional, and I couldn't find it in the pkg or ports repos, so
  • Go to /usr/local/share/adeskbar/plugins/menu.py or wherever you installed it on your system and comment the entire class definition for class EventHandler(pyinotify.ProcessEvent)
  • Restart adeskbar, for me I right-clicked adeskbar, chose quit and ran $ adeskbar default &
2. IOError: [Errno 2] No such file or directory /etc/xdg/menus/applications.menu
  • Easy fix, based on output.log, control left adeskar at this point: File "/usr/local/share/adeskbar/adesk/core.py", line 216
  • Go to line 216 and update the path to the symbolic link created earlier /usr/local/etc/xdg/menus/
3. File "/usr/local/share/adeskbar/adesk/check.py", line 76, in check_modules
check['VTE'][0] = False
  • Line 76 isn't the problem, it's actually at line 19. if you look closely you'll see 'VTE ', notice the extra space. Edit the line to remove the space so it looks like 'VTE'.
  • Restart Adeskbar, right-click and choose Preferences and it should finally appear

1. Intel Celeron Processor N2840 @ 2.14GHz / Intel HD Graphics / 4 GB RAM / 223Gb Storage
2. I counted in my head 1-1000-2-1000-3-1000, not scientific but I didn't care enough to really measure it down to the milliseconds
3. It took me longer than 3 days to figure out how to dual boot Windows 8.1 / FreeBSD on a GPT + UEFI setup and I still don't know how to