Sane PATH variable in Emacs on Mac OS X

On Mac OS X the system PATH variable is not recognized by emacs. This means that one can not simply type

mysql

in the emacs shell to get into the database. The emacs shell complains about “binary not found”.

Indeed

echo $PATH

reveals that emacs just looks into /bin, /usr/bin, /sbin and /usr/sbin.

To set the $PATH variable inside emacs one can append the following lines to the .emacs file (found on github, hattip Alex Payne):

; sane path
(setq path "/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/mysql/bin")
(setenv "PATH" path)

Next time Emacs starts one can go to the shell and

mysql

presents the database prompt.

Advertisement

Emacs as MySQL frontend

After working quite a time with some minor MySQL databases in the (Emacs-) Terminal I just looked up the preinstalled SQL related emacs functions. Just entered M-x sql TAB and indeed the autocompletion showed a function sql-mysql, as expected…

I gave it a try with
M-x sql-mysql
and after prompting for database, servername, username and password Emacs connected to the database and presented the MySQL shell. So I bound sql-mysql to some keyboard shortcut, BUT entering the whole connection parameters each and every time was not acceptable.

Atomized.org has a really nice post on Enhancing Emacs’ SQL Mode (you can have a look, but you cannot read the post before pasting the content to a text editor). There I found some excellent functions which would provide a solution:

(setq sql-connection-alist
'((pool-a
(sql-product 'mysql)
(sql-server "1.2.3.4")
(sql-user "me")
(sql-password "mypassword")
(sql-database "thedb")
(sql-port 3306))
(pool-b
(sql-product 'mysql)
(sql-server "1.2.3.4")
(sql-user "me")
(sql-password "mypassword")
(sql-database "thedb")
(sql-port 3307))))

(defun sql-connect-preset (name)
"Connect to a predefined SQL connection listed in `sql-connection-alist'"
(eval `(let ,(cdr (assoc name sql-connection-alist))
(flet ((sql-get-login (&rest what)))
(sql-product-interactive sql-product)))))

(defun sql-pool-a ()
(interactive)
(sql-connect-preset 'pool-a))

Now, you can just run sql-pool-a and get connected right away. Because the buffers have good names, you can easily fire up many connections.

I included it in my .emacs file and appended
(DefGlobKey "s-a" 'sql-pool-a)
and with a keystroke the database promt appears.

Thx, atomized.org, but what’s that webpage giving you an epileptic fit looking at it?!. Excellent page 🙂

Sending Email from Emacs

Since Emacs is my main workhorse it comes handy to send plain text email straight from an Emacs buffer. Like for example this blog post directly from Emacs to the Rforge blog.

Emacs needs some configuration to do this, which I found on GNU.org’s Emacs Speaks SMTP:

First you need a helper application which enables Emacs to use a crypted connection to an IMAP server like e.g. starttls:

sudo aptitude install starttls

Then include the following into your ~/.emacs file to setup the outgoing mail server (SMTP):

;; Emacs Speaks SMTP (gnu.org) using the default mail agent ;; If you use the default mail user agent.
(setq send-mail-function 'smtpmail-send-it)
;; Send mail using SMTP via gmail.
(setq smtpmail-smtp-server "smtp.gmail.com")
;; Send mail using SMTP on the mail submission port 587.
(setq smtpmail-smtp-service 587)
;; Authenticate using this username and password against smtp.gmail.com. (setq smtpmail-auth-credentials
'(("smtp.gmail.com" 587 "captain.cheese" nil)))
;; Use STARTTLS against the server.
(setq smtpmail-starttls-credentials
'(("smtp.gmail.com" 587 "captain.cheese" nil)))

when you are not using gmail, you have to find out which SMTP server and port your provider uses and captain.cheese has to be changed to your user account. Note that nil can be changed to your passphrase, which I do not recommend since the .emacs file is plain text so it is possible to read your passphrase…

The standard Emacs keys to open an empty email formular is Control-x m, which I found to awkward, so I changed it to Hyper-c (this requires having CapsLock redefined as “Hyper”-key, which is another story and highly recommendable): (DefGlobKey "H-c" 'compose-mail)
(DefGlobKey "H-M-c" 'mail-send-and-exit)

Now H-c opens an email and after writing it H-M-c sends it.

Using the Directory Editor in Emacs

Emacs autocompletes path names when opening a file. Just hit TAB. When I forget the correct name of the file I am looking for I just stop at some place in the path and hit ENTER.

A list of files and folders in the directory show up and Emacs says (Dired by name). I just recently found out that Emacs offers full control over the listed files and directories in this so called Dired (directory editor) mode.

A look at the manual showed some straighforward commands, which work while Emacs is Dired. Move to the line listing the file/folder and hit

  • D to delete this file or folder
  • d to flagg this file/folder for deletion
  • u to unflagg this file/folder to prevent deletion
  • x to delete the files flagged for deletion
  • R Rename the specified file
  • C Copy the specified file(s). Emacs asks for the directory (and filename) where to copy to.