Sane PATH variable in Emacs on Mac OS X

16 08 2011

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.





Match and replace TAB

20 01 2011

I am using the terminal inside emacs to modify and clean textfiles (colon separated) for import into MySQL – mostly with SED, GREP, AWK.
Replacing TAB in a textfile with SED was not as straightforward as I thought. So Google came up with an excellent post on macworld.com and here especially one post suggesting perl as a solution. It turned out perl behaves inside the pipe the same way as SED/AWK.

Replacing TAB ‘\t‘ with colon ‘,‘ in a textfile ‘SomeFile.txt‘ can be done with:

perl -pi -e 's/\t/,/g' SomeFile.txt

or using the pipe, which can be combined with grep, sed, awk, tr, head, tail, etc…

cat SomeFile.txt | perl -pi -e 's/\t/,/g'

*Very* similar to the sed invocation, except for the flags. “-pi” for “*print* every line out after making the changes, while editing *inplace*. That is, no backup file is kept. In reality, a temp file is written, and if all is well (ie the operation succeeds), the temp file is shifted “onto” the original file, giving the appearance of an in-place edit.





Emacs as MySQL frontend

18 01 2011

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

30 09 2009

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.





Fullscreen toggle with Emacs Shortcuts

16 08 2009

After having the unnecessary scroll-, tool- and foo-bars removed it is nice to toggle emacs fullscreen with a keyboard shortcut.

The following code pasted into the .emacs file will do the job under Ubuntu, using wmctrl. It works with a lot of desktop managers (Xfce, fluxbox, Gnome at I tried at least). If it is not installed
sudo aptitude install wmctrl

The full screen mode is based on a code snippet I found online. The comments in the .emacs file gave credit to the emacswiki and Steven Poole for the code.

First define a lisp function calling wmctrl:
(defun djcb-full-screen-toggle ()
(interactive)
(shell-command "wmctrl -r :ACTIVE: -b toggle,fullscreen"))

and then a keybinding to the function

(DefGlobKey "H-f" 'djcb-full-screen-toggle)

In this example toggle fullscreen is bound to H-f which means pressing the Hyper key and f. You have to customize it to your needs.





Using the Directory Editor in Emacs

5 08 2009

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.




Emacs – Display REAL Full Screen

27 07 2009

The main advantage of the Emacs editor is the possiblity to control everything (literally) with lightning fast keyboard shortcuts. This holds true after the default shortcuts got replaced by the sane keybindings of Andrew Polonsky – otherwise you end up lightning fast crippled.

Anyway. Having proper keybindings and being used to them, everything which is not text content is no longer usefull and just minimized the invaluable screenspace (espacially on a 12” X61 without an external display).

So away with them:
(menu-bar-mode -1)
(toggle-scroll-bar -1)
(tool-bar-mode -1)

include those lines into the configuration file in your homefolder (in Linux: /home/YourLoginName/.emacs) and
menu-bar, tool-bar and scroll-bar disappear.

Update: The only thing to do then is to toggle emacs to fullscreen and the whole screen is emacs (Hat tip: Nisheet).