Friday, October 1, 2010

Bash Readline Emacs Editing Mode, Basic Keyboard Shortcuts on OS X xterms

Yes, I know these work in more than just OS X, but I wanted to focus on getting it configured correctly within xterm applications running on OS X.

This only works for Emacs Mode. Your shell might be in Vi mode, but you might already know this since it's not the default. The shell editing and command history sections also work when editing in an emacs editor.

  • In Terminal.app, open preferences and go to the
    Settings->Keyboard tab. Check the "Use option as meta key" check box.
  • If you use iTerm, go into Manage Profiles, select the global or xterm keyboard profiles (whichever one your bookmark is using, or just do both) and set "Option Key as" to "+ESC".

Shell Editing:
  • Ctrl + A: Move the cursor to the beginning of the line
  • Ctrl + E: Move the cursor to the end of the line
  • Ctrl + L: Clears the Screen, similar to the clear command
  • Ctrl + H: Same as backspace
  • Ctrl + W: Cut the word before the cursor
  • Opt + D: Cut the word after the cursor
          (don't confuse with Ctrl + D)
  • Ctrl + U: Cut everything before the cursor
  • Ctrl + K: Cut everything after the cursor
  • Ctrl + Y: Paste the last thing to be cut.
  • Ctrl + T: Transpose the last two characters before the cursor
  • Opt + T: Transpose the last two words before the cursor
  • Opt + B: Move back one word
  • Opt + F: Move forward one word
  • Ctrl + _: Undo (also "Ctrl + -" works)

Command History:
  • Ctrl + R: Let’s you search through previously used commands for lines matching a specified search string.
  • Ctrl + G: While in search mode, aborts the current search and restores the original line.
  • Ctrl + J: While in search mode, terminates a search and places the current item from the search on the line.
  • <return>: While in search mode, terminates a search and places the current item from the search on the line and executes it.

For more advanced Readline Emacs Editing Mode shortcuts, you can get a copy of the Readline Emacs Editing Mode Cheat Sheet and work on making them function in your OS X xterms application. The shortcuts listed above are the most useful for me and I hope this helps you out.

Saturday, July 17, 2010

System and Kernel Settings in Mac OS X

After scouring the Internet looking for a way to cleanly increase the number of open files, (ulimit -n), I've finally discovered a nice, clean, and easy way to update the system and kernel settings. To start, I'll explain the problem. I'm trying to set up MySQL on a Mac Pro running OS X 10.5.8 and having 4 cores plus 8GB of RAM. After tuning the MySQL configuration file (/etc/my.cnf), MySQL eventually has a problem with the number of files it can have open, or runs out of locked memory, etc. To fix these issues, some default settings need to be changed in the system:

To modify system limits (ulimit settings), update /etc/launchd.conf with the following:
limit memlock unlimited
limit maxproc 1024 2048
limit maxfiles 32768
Make sure that my.cnf contains the open-files-limit setting under the [mysqld_safe] section to match the maxfiles limit set in launchd.conf:
open-files-limit = 32768

To modify kernel parameters (sysctl settings), update /etc/sysctl.conf with the following:
kernel.sysv.shmmax=402653184
kernel.sysv.shmall=393216
kernel.sysv.shmmni=4096

kern.maxfiles=32768
kern.maxfilesperproc=32768

kern.maxproc=2048
kern.maxprocperuid=1024
NOTE: make sure you use your own values specific to your environment. Here are my notes on setting shmmax and shmall:
# Set the maximum size of shared memory segment to 75% of
# total memory installed:
#
# kernel.shmmax = 402653184 <-- 512MB (536870912)
# kernel.shmmax = 1610612736 <-- 2GB (2147483648)
# kernel.shmmax = 4831838208 <-- 6GB (6442450944)
#
# Set the total amount of shared memory available using the
# following formula:
#
# shmall = AmountOfFreeMemBytes / PAGESIZE
#
# AmountOfFreeMemBytes is the amount of memory to be
# allocated to MySQL (typically use shmmax) divided by the
# PAGESIZE of the memory, 4096 on most systems.
# To get the page size on a host, run the following command:
#
# linux:
# getconf -a | grep -i PAGESIZE
#
# OS X:
# getconf PAGESIZE
#
# kernel.shmall = 98304 <-- calculated for 512MB
# kernel.shmall = 393216 <-- calculated for 2GB
# kernel.shmall = 1179648 <-- calculated for 6GB
In order for these settings to take effect, OS X needs to be rebooted. Once the system comes back up, check the system settings and kernel parameters to make sure the settings are active. MySQL (or whatever service you're using) can be started now.

I hope you found this helpful. Good luck!

Wednesday, May 5, 2010

Setting up Emacs + erlang-mode + distel on Snow Leopard

I've spent a considerable amount of time trying to set up Aquamacs with erlang-mode + distel and wanted to share my success -- I briefly switched to Emacs for OS X after working with one of my Erlang colleagues, Chris Duesing, on getting all of this set up -- but found the recent release of Aquamacs 2.0 to have more useful features for my tastes.

Here's some pre-requisites:
  • Download Aquamacs from http://aquamacs.org/ -or- Emacs for Mac OS X from http://emacsformacosx.com/. This is purely a personal preference, and I have found that I *really* like Aquamacs 2.0 that was recently released -- switched from Aquamacs to Emacs for OS X, then 2.0 came out and switched back.
  • Download, build, and install Erlang. You can use MacPorts to install Erlang, but Homebrew is an incredible replacement to MacPorts so I'm going to recommend that you try it out. Follow this guide to setting up Homebrew which will be used to install Erlang and the the Erlware tools Faxien and Sinan:

    http://simergence.blogspot.com/2010/04/erlang-on-mac-for-lazy.html

    NOTE: Spend some time reading that blog entry which will show you how to build out projects with ease, create proper Erlang releases, and easily install various published Erlang applications including your own.

  • Download, build, and install Distel:

    svn co http://distel.googlecode.com/svn/trunk distel
    cd distel
    make
    make install

NOTE: At the time of this writing, we're using Erlang R13B04. Make sure your paths match the version of Erlang you are targeting.
  • If you have installed using Homebrew, Erlang will be located in the following folder:

    Erlang root: /usr/local/Cellar/erlang/R13B04
    Emacs files: [erlang-root]/lib/erlang/lib/tools-2.6.5.1/emacs
    Erlang bin: [erlang-root]/bin
  • If you have installed using MacPorts, Erlang will be located in the following folder:

    Erlang root: /opt/local/lib/erlang
    Emacs files: [erlang-root]/lib/tools-2.6.5.1/emacs
    Erlang bin: [erlang-root]/bin
  • Distel will be installed in the following folder:

    /usr/local/share/distel
NOTE: We will use the Homebrew locations for the remainder of this example, so make sure your configuration uses to correct locations for your installation.

We will need to pull down a couple of files that are missing from the Erlang installation to get erlang-mode working in Emacs (R14+ has these files, please ignore):

cd /usr/local/Cellar/erlang/R13B04/lib/erlang/lib/tools-2.6.5.1/emacs/
wget "http://github.com/erlang/otp/raw/dev/lib/tools/emacs/erlang-skels.el"
wget "http://github.com/erlang/otp/raw/dev/lib/tools/emacs/erlang-skels-old.el"

Lastly, we need to create a .emacs init file to load erlang-mode when Emacs starts. The .emacs file is located in your home folder. Create the .emacs file with the following contents (there may be wrapping issues):

;;
;; Standard Emacs settings
;;

;; Set my email address.
(setq user-mail-address "jpatton@gmail.com")

;; Set the shell emacs uses.
(setq explicit-shell-file-name "/bin/bash")

;; Use column and line numbering.
(line-number-mode 1)
(column-number-mode 1)

;; Display time in the minibuffer
(display-time)

;; Specify a color theme
(require 'color-theme)
(color-theme-initialize)
(color-theme-euphoria)

;;
;; Erlang mode and Distel settings
;;

;; This is needed for Erlang mode setup
(setq load-path (cons "/usr/local/Cellar/erlang/R13B04/lib/erlang/lib/tools-2.6.5.1/emacs" load-path))
(setq erlang-root-dir "/usr/local/Cellar/erlang/R13B04")
(setq exec-path (cons "/usr/local/Cellar/erlang/R13B04/bin" exec-path))
(require 'erlang-start)

;; Tell Emacs not to wait the usual 60 seconds for an Erlang prompt
(defvar inferior-erlang-prompt-timeout t)

;; This is needed for Distel setup
(let ((distel-dir "/usr/local/share/distel/elisp"))
(unless (member distel-dir load-path)
;; Add distel-dir to the end of load-path
(setq load-path (append load-path (list distel-dir)))))

(require 'distel)
(distel-setup)

;; Some Erlang customizations
(add-hook 'erlang-mode-hook
(lambda ()
;; when starting an Erlang shell in Emacs, default in the node name
(setq inferior-erlang-machine-options '("-sname" "emacs"))
;; add Erlang functions to an imenu menu
(imenu-add-to-menubar "imenu")))

;; A number of the erlang-extended-mode key bindings are useful in the shell too
(defconst distel-shell-keys
'(("\C-\M-i" erl-complete)
("\M-?" erl-complete)
("\M-." erl-find-source-under-point)
("\M-," erl-find-source-unwind)
("\M-*" erl-find-source-unwind)
)
"Additional keys to bind when in Erlang shell.")

(add-hook 'erlang-shell-mode-hook
(lambda ()
;; add some Distel bindings to the Erlang shell
(dolist (spec distel-shell-keys)
(define-key erlang-shell-mode-map (car spec) (cadr spec)))))
Now, you should be able to launch Aquamacs -or- Emacs for Mac OS X and have no errors in the *Messages* buffer. Once you have all of this working, you can read up on how to use erlang-mode in Emacs. Here are some links that I used to put this entry together that have more information about erlang-mode and Distel extensions:


    Also, if you're in the Chicago area, join the Chicago Erlang User's Group (CEUG) and help be a part of our growing community!

    Hopefully, something in the blog post helped you get things working.

    Happy erlanging!