file management: emacs dired to replace Finder in mac os x (and other OS)

I hate Finder in Mac OS X. I mean, it looks nice and all, but it is not customizable. I really liked the KDE window management back when I used Kubuntu because I can select/de-select files with the keyboard, cut/paste/copy files with the keyboard, have shortcuts to different locations, etc. Finder has some keyboard shortcuts, but not at all flexible. You can copy and paste files, but you can’t cut and paste files. Selecting/de-selecting files is not as flexible (eg, skipping a file). I can’t go to the sidebar window where all my bookmarked locations reside using the keyboard. Finder, and in general Mac OS X, is not so flexible. This bugs me a lot. I wanted to get stumpwm working in OS X so that things are more keyboard-oriented. However, it doesnt work with all the native mac apps. The fix is with dired mode in emacs. I spend most of my time in emacs already. With dired, I can even manage files inside emacs (I can do so in the shell, but this makes browsing and executing commands on files a breeze). Use “C-x f” and navigate to a location rather than a file. Learn all the keyboard shortcuts for delete, rename, execute shell command, etc. You can even cut/copy/paste with wuxch-dired-copy-paste. The functions below also allow me to open files the Mac way, with the “open” command. This is cool because files will be opened with the native Mac default application.

<pre class="src src-sh">;; http://www.emacswiki.org/emacs/DiredPlus

;; copy and paste files in dired ;; needed additional files, such as dired+, dired-details+, find-dired+, and find-dired- (require ‘wuxch-dired) (require ‘wuxch-dired-copy-paste) ;;http://stackoverflow.com/questions/1824696/function-to-call-same-shell-command-in-dired ;; (defun dired-do-shell-mac-open-vqn () ;; (interactive) ;; (dired-do-async-shell-command ;; “open” current-prefix-arg ;; (dired-get-marked-files t current-prefix-arg))) (defun dired-do-shell-mac-open-vqn () (interactive) (save-window-excursion (dired-do-async-shell-command “open” current-prefix-arg (dired-get-marked-files t current-prefix-arg))))

(define-key dired-mode-map (kbd “s-o”) ‘dired-do-shell-mac-open-vqn)

I can use Apple-o/Windows-o to open files with the default mac program for the marked files.I also use emacs’ bookmark system (“C-x r b” and “C-x r m”) to bookmark locations.Note that all this is not specific to Mac OS X. Dired’s been around forever – I just never picked it up really. Now with my “open” trick and the bookmark system, this should really replace my use of Finder in Mac OS X.

Update 11/10/2010 – On Linux

I’m back on Linux now. A few updates on launching files with the default application. Add the following to the emacs init file:

(defun dired-do-shell-launch-file-default ()
 (interactive)
 (save-window-excursion
 (dired-do-async-shell-command
 "$HOME/Documents/bin/open.sh" current-prefix-arg ;; linux;; multiple files
 ;; "nohup xdg-open" current-prefix-arg ;; linux can open multiple files, but one at a time
 ;; "see" current-prefix-arg ;; linux;; can open at most 1 file (being opened)
 ;; "open" current-prefix-arg ;; mac os x
 (dired-get-marked-files t current-prefix-arg))))
(define-key dired-mode-map (kbd "s-o") 'dired-do-shell-launch-file-default)

Need the script open.sh (thank you fledermaus from #emacs):

#! /bin/bash

for file in "$@"
do
nohup xdg-open "$file" &
done
sleep 1

Now, to unmount devices in emacs:

;; unmount disk in dired
;;http://loopkid.net/articles/2008/06/27/force-unmount-on-mac-os-x
(defun dired-do-shell-unmount-device ()
 (interactive)
 (save-window-excursion
 (dired-do-async-shell-command
 "umount" current-prefix-arg ;; linux
 ;; "diskutil unmount" current-prefix-arg ;; mac os x
 (dired-get-marked-files t current-prefix-arg))))
(define-key dired-mode-map (kbd "s-u") 'dired-do-shell-unmount-device)

If an error is returned, make sure the device is not being used anywhere, including opened in nautilus or in some terminal.

The ability to launch files with the default application (look into xdg-open, gnome-open, and see), unmnount devices, and being able to copy/paste files in emacs help makes dired the perfect file manager. I will use this from now on over Nautilus. It’ll compliment using stumpwm.

global environment variables (like PATH) in mac os x via environment.plist and /etc/launchd.conf

so, through my journey of getting emacs 23 and conkeror to work on mac os x, i’ve discovered that PATH is not defined for native mac gui apps. details of the journey can be found in this post of mine’s.

now, to sum it up, to get PATH or other env variables working in mac gui apps, you need to create or modify your ~/.MacOSX/environment.plist file. i usually define my PATH in .bashrc, so i do something like

In terminal, type:

<pre class="src src-sh">defaults write ${<span style="color: #eedd82;">HOME</span>}/.MacOSX/environment PATH <span style="color: #ffa07a;">"${PATH}"</span>

then restart (log out) computer to take effect.

To update the path of environment.plist at every login, do (credit here):

<pre class="src src-sh"><span style="color: #00ffff;">if</span> [ -x /usr/libexec/path_helper ]; <span style="color: #00ffff;">then</span>

eval /usr/libexec/path_helper -s defaults write $HOME/.MacOSX/environment PATH “$PATH” fi

in /etc/profile

logout and log back in. however, apps launched using spotlight still do not see the env vars. they are seen only when launched using Finder. so, to fix that, put something like

 <pre class="src src-sh">setenv PATH /opt/local/bin:/opt/local/sbin:/sw/bin:/sw/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin:/usr/X11/bin:/usr/X11R6/bin:/Users/vinh/Documents/bin/:/opt/local/bin:/opt/local/sbin:/usr/texbin

in your /etc/launchd.conf file. a restart is required. this second method works for all apps start in mac.

these files should be edited each time u add to your PATH in .bashrc.

see my original post to follow all the credit.

Emacs App (v 23) for Mac OS X & environment.plist

Ever since i bought my macbook i’ve been using carbon emacs (v 22) as my emacs of choice since it has everything emacs and works well with OS X (I didn’t want to use Aquamacs because I’m more of a unix person than Mac).

Recently, I discovered Emacs App and wanted to migrate over since

  1. it is emacs v 23, and
  2. it uses Cocoa, a newer technology than carbon and is the standard on Mac OS X these days.

I wanted to use Vincent Goulet’s modified binary version of Emacs App, however, ESS gives me weird characters. Thus, I decided to just compile Emacs App myself.

  1. Downloaded emacs app source via CVS.
  2. ./configure –with-ns
  3. make
  4. make install

Step 3 will make Emacs.app in ./nextstep/. Now, install ESS (ESS: download and put in ~/elisp, edit .emacs for load) and AucTeX. AucTeX:

  1. ./configure –with-emacs=~/Downloads/emacs/emacs/nextstep/Emacs.app/Contents/MacOS/Emacs –with-lispdir=/usr/local/share/emacs/site-lisp –with-texmf-dir=/usr/local/texlive/texmf-local/
  2. make
  3. sudo make install

ESS works. However, AucTeX doesn’t because it doesn’t see the PATH to latex/pdflatex. This is because Emacs.app acts like a Mac program, so it doesn’t have access to it as defined in my .bashrc. To do so, we must edit/make ~/.MacOSX/environment.plist , a file that is analogous to .bashrc but for Mac GUI programs.

FIX (http://www.emacswiki.org/emacs/CarbonEmacsPackage [unix and mac path] ) In terminal, type: defaults write ${HOME}/.MacOSX/environment PATH “${PATH}”

then restart (log out) computer to take effect.

To update the path of environment.plist at every login, do (credit here): #/etc/profile begin

if [ -x /usr/libexec/path_helper ]; then eval /usr/libexec/path_helper -s defaults write $HOME/.MacOSX/environment PATH “$PATH” fi

#/etc/profile end

in /etc/profile

UPDATE 2: it appears launching emacs 23.1 through spotlight will not pick up the ENV variables. launching through Finder or the shell is fine. Don’t know why. this is weird!

UPDATE 3: did some googling about environement variables, and this, this, and this site taught me how to fix the PATH issue when launching using spotlight. So i just need to put the following in my etc/launchd.conf file: setenv PATH /opt/local/bin:/opt/local/sbin:/sw/bin:/sw/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin:/usr/X11/bin:/usr/X11R6/bin:/Users/vinh/Documents/bin:/opt/local/bin:/opt/local/sbin:/usr/texbin

restart computer and things should be working GREAT!

i usually update my PATH in .bashrc, so whenever i update it, just echo $PATH and update the launchd.conf file.

another way to fix all this is to set the PATH (exec-path) inside .emacs, like here.