Emacs Power: remote servers and shell commands

Emacs file and directory browsing

Emacs has Dired, a great method for browsing directories; especially in combination with ido-mode, I prefer it to Windows Explorer, OS X Finder, Gnome Nautilus, or anything else I’ve used over the decades to browse file systems. You can quickly begin entering paths, and Dired helps you with directory and file name completion.

As you browse directories, emacs lets you bookmark any path you may visit again. A very cool feature of emacs is the wide variety of shell-based modes: shell, term, interactive SQL, version control, recursive grep, and more. When you are looking at a Dired buffer and invoke any of those commands, the shell is started in the directory you are looking at.

TRAMP: powerful remote file server access

An emacs user will eventually discover that Dired becomes even more powerful with the built-in power of TRAMP (Transparent Remote (file) Access, Multiple Protocol). This extends directory path syntax to include FTP, SSH, Rsync and other protocols for accessing remote files.

An example of accessing a remote file on my development server smeagol from my laptop:

/ssh:gregj@smeagol:work/client1/web/index.php

A directory on an FTP server is accessed the same way:

/ftp:ftpuser@ftp.example.com:

Even sudo is considered a ‘protocol’; so to gain root access without leaving the comfort of your emacs session, use

/sudo::/etc/hosts

Emacs even helps you browse remote servers, providing the same name completion you get on your local directories.

The amazing combo of TRAMP and shell commands

As wonderful as all this is, we are still in the realm of “mere” GUI editors that can browse remote servers. But we are dealing with emacs, the superset of all editors, so we expect even more.

Imagine we are looking at a directory on a remote server at /ssh:myuser@remote.com:web/public_html, and decide to type

M-x shell

What happens? Why, emacs looks at our current directory, sees that it is a TRAMP remote path, and just does the Right Thing™: in this case, invokes ssh, sets the directory on the remote server to ~myuser/web/public_html, and sets us at the shell prompt.

Similarly, if we invoke version control (vc-dir, for example), or recursive grep (rgrep), or most other shell-based commands, emacs will open a secure shell first, then run the vc command (svn, git, etc) or grep on the remote server!! So for example, if I innocently invoke rgrep at the root of a remote WordPress installation, the grep command looks through all the files from the remote server. If on the other hand I were accessing the server using something like SSHFS, all those files would be transferred to my local machine first and then searched!

Some examples

I’ll save my favorite uses of this magic called Dired with TRAMP for last. I often need to access a development system or a live WordPress installation remotely. To access the SQL client, I found I often had to browse for the WordPress config file, open it, search for the database access info, open a SQL session using M-x sql-mysql and fill in all the prompts to authenticate to the MySQL server. It’s great that emacs with TRAMP starts a shell on the remote machine, and initiates the mysql client on that machine. But emacs allows you to do damn near anything you can imagine, so I realized I could write a function that does the following:

  1. Looks for wp-config.php in the current directory
  2. If not found, moves up a directory until it either finds the file or reaches the root of the filesystem
  3. If found, opens the config file and parses the database authentication parameters
  4. Feeds those parameters to the sql-mysql function and
  5. plops me into the MySQL prompt all logged in and ready to go!

Another example: my iPod Touch has an SSH server running on it (don’t ask me how it got there). I have discovered that many apps use SQLite to store their data. I have been losing weight lately, and have been using the excellent Lose It! app to track my meals and exercise. The app gives me nice weekly summaries of my caloric intake, but does not give a weekly summary of my aerobic exercise. Here’s how I get that information now:

  1. I have a nice bookmark to the Lose It! application directory at
    /scpc:mobile@172.16.17.118:/var/mobile/Applications/C6503545-700B-4395-9C8B-FE5B75CF6CD8/,
    so I hit the Return key on that bookmark and wait for Dired to show me the files there.
  2. Browse to the Documents directory, wherein is stored the database for my personal history
  3. Invoke M-x sql-sqlite and enter the database file UserDatabaseV1.sql (using dabbrev as a shortcut)
  4. Wait for the SQLite prompt to appear, and run a lovely little SQL query using a YASnippets shortcut:

SELECT date('2001-01-01', '+' || Date || ' day', '-1 day', 'weekday 1', '-7 day') AS Weekdate, strftime('%W',date('2001-01-01', '+' || Date || ' day', '-1 day')) AS Week,   ExerciseName, ExerciseCategoryId, SUM(Minutes), SUM(CaloriesBurned)   FROM ExerciseLogEntries  GROUP BY Week;

Summary

I hope that this post gives an idea of the power of TRAMP on emacs. It should at least explain the occasional ecstatic post you may see from your geekier tweeps.

As a bonus, here’s what the above examples look like in use: it’s unbelievable how fast emacs makes you after a quick 15 years of study.

And here’s my emacs lisp code that opens up a SQL prompt for any WordPress installation. To use, eval the code and invoke M-x gjg/sql-mysql-wordpress

(defun gjg/parse-wp-config-db (wpconfig-path)
  "Read in and parse the DB settings from a WordPress config file; binds 'global' vars for use by sql-mode"
  (save-excursion ;; will restore current buffer and default dir afterwards
      (set-buffer (get-buffer-create (generate-new-buffer-name " wp-config.php")))
  (insert-file-contents wpconfig-path)
  ;; in regex: subexpr 1 is variable name, subexpr 3 is value: DB_{HOST,NAME,PASSWORD,USER}
      (while (search-forward-regexp "define\s*(\s*['\"]\\(DB_\\(HOST\\|NAME\\|PASSWORD\\|USER\\)\\)['\"]\s*,\s*['\"]\\([^'\"]*\\)['\"]\s*)" (point-max) 42   )
  (cond
  ((equal "DB_HOST" (match-string-no-properties 1))
  (setq sql-server (match-string-no-properties 3)))
  ((equal "DB_NAME" (match-string-no-properties 1))
  (setq sql-database (match-string-no-properties 3)))
  ((equal "DB_PASSWORD" (match-string-no-properties 1))
  (setq sql-password (match-string-no-properties 3)))
  ((equal "DB_USER" (match-string-no-properties 1))
  (setq sql-user (match-string-no-properties 3)))))
  (kill-buffer )))

  (defun gjg/sql-mysql-wordpress ()
  "Find WordPress config file in current tree, log into WP database if found."
  (interactive)
  (let ((mypath (locate-dominating-file default-directory "wp-config.php")))
  (if mypath
  (progn
  (gjg/parse-wp-config-db (concat mypath "wp-config.php"))
  (pop-to-buffer (sql-connect-mysql))
  (setq sql-interactive-product 'mysql)
  (setq sql-buffer (current-buffer))
  (sql-interactive-mode)
  (let* ((match (string-match (nth 0 tramp-file-name-structure) mypath))
  (myformat (if (eq nil match)
  (format " WordPress: local; %s; dbhost %s "
  mypath
  sql-server
  )
  (format " WordPress: Remote %s@%s %s; dbhost %s "
  (match-string (nth 2 tramp-file-name-structure) mypath)
  (match-string (nth 3 tramp-file-name-structure) mypath)
  (match-string (nth 4 tramp-file-name-structure) mypath)
  sql-server))))
  (setq header-line-format myformat))
  )
  (message "Did not find wp-config.php in current path"))
  ))

LocalWords: Dired emacs SQL Rsync smeagol pre rgrep WordPress MySQL iPod
LocalWords: SQLite

This entry was posted in Development, emacs, wordpress and tagged , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Posted October 5, 2009 at 9:27 pm | Permalink

    Great post! Just out of curiosity what versions of emacs/ido/etc are you running this on. I’m seeing some different behavior in mine when I try to get a shell on the remote server from within dired.

  2. Posted October 6, 2009 at 3:52 pm | Permalink

    Thanks, Jason.

    Let’s see, I’m running primarily on Ubuntu 9.10 (karmic alpha-6); GNU Emacs 23.1.50.1; ido 1.57-ish that was included in this release of Emacs.

    I have noticed that some system process modes do not heed the TRAMP file syntax. Flymake-mode comes to mind as one. I think the solution is for the mode to call ’start-file-process instead of ’start-process. I’m guessing that more modes are using ’start-file-process, and perhaps they did not do so in earlier versions.

2 Trackbacks

  1. [...] Emacs Power: remote servers and shell commands [...]

  2. [...] !emacs power: remote servers and shell commands http://gregorygrubbs.com/wordpress/emacs-power-remote-servers-and-shell-commands/ [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*
  • randy moss combine results
  • tea party zombies download
  • search 78search 800 numbers
  • randy moss bio
  • dis systems
  • archos
  • randy moss yahoo stats
  • roses
  • dis quand reviendras-tu
  • xanadu bengals
  • zara phillips and the queen
  • 4pm cspancspan area 51cspan 90.1
  • freida pinto miral
  • search lsu.edu
  • mtv oddities
  • seagate
  • battleship aurora
  • greg olsen university of miami
  • mtv kings of leon
  • chad ochocinco sisterchad ochocinco twitter
  • tolerance
  • search engines cookiessearch engines definition
  • bonus
  • gears
  • hartmann
  • dis 0 0.9
  • vince young yahoo stats
  • radar
  • beers
  • search 2.0
  • homeless
  • dis boards cruise
  • search engines us
  • chad ochocinco johnson
  • bengals qb situation
  • gregg olsen books
  • zara phillips dating
  • vince young uncle rico gif
  • freida pinto green dress
  • bea luna
  • chicago bears media relations
  • framed
  • la ink 2011 season 5
  • deed
  • battleship vittorio veneto
  • di's hallmark
  • meant
  • chicago bears 61
  • vince young depression
  • cspan hosts
  • ratchet
  • new england patriots gillette stadium
  • chicago bears 08 record
  • chicago bears bleacher report
  • battleship yamato wreck
  • chicago bears training camp
  • la ink corey
  • chicago bears 1985
  • search engines watch
  • search engines questions
  • pendant
  • cspan facebook
  • connecticut renaissance faire
  • battleship egg hunt
  • serenity
  • kilo
  • bangles eternal flame mp3bengals forum
  • prairie
  • afternoon
  • greg olsen vikingsgreg olsen wife
  • springhill
  • tea party birthday
  • vince young quiz
  • bea zuberbühler
  • battleship ipad
  • betty
  • search engines of the world
  • bea 0b0 105
  • dis x
  • chicago bears tattoos
  • greg olsen vancouver
  • hp support 530
  • chicago bears expo
  • quran
  • legend
  • search engines 9
  • 60 search engines virus
  • zara phillips baby
  • freida pinto zac posen
  • connecticut education
  • battleship yamato 2010
  • search vim
  • hp support error 1005
  • pads
  • connecticut post
  • mtv executivesmtv fantasy factory
  • connecticut secretary of state
  • search 990 finder
  • rooster
  • homeowners
  • wein
  • chicago bears 96
  • vince young yahoo stats
  • worst
  • c span yesterdayc span zelaya
  • randy moss jail
  • new england patriots 98.5
  • hp support quick test pro
  • zara phillips baby
  • la ink games online
  • chicago bears pictures
  • chad ochocinco bears
  • zara phillips wedding hat
  • chad ochocinco vs skip bayless
  • chad ochocinco and cheryl burke
  • new england patriots jake locker
  • tijuana
  • hp support englandhp support forum
  • bengals arrests
  • c span video contest
  • mtv dougie
  • new england patriots 80
  • tea party zombies download
  • vince young redskins
  • cspan presidents
  • palma
  • bear gryllsbea hive dance studio
  • zara phillips kids
  • battleship galactica
  • randy moss 98 vikings
  • beagle
  • extending
  • hp support id
  • connecticut 5th district
  • vince young football camp
  • search tumblr
  • la ink cast
  • search 32
  • battleship wilmington nc
  • randy moss 07 08 highlights
  • hose
  • dis pater
  • crimper
  • la ink 03x05
  • mandolin
  • hp support 6500a plus
  • battleship bismarck wreck
  • search engines before google
  • cspan washington correspondents dinner 2011
  • reign
  • chad ochocinco quotes video
  • bea goldfishberg
  • dependency
  • spherical
  • coronado
  • randy moss vikings 2011
  • battleship lexington
  • mtv true life
  • vince young endorsementsvince young foundation
  • hiding
  • search and seizure
  • search engines usage statistics 2010
  • connecticut lottery
  • dressing
  • licensed
  • mtv overdrive
  • tea party nj
  • hp support chat
  • hp support greece
  • connecticut juvenile training schoolconnecticut kids
  • hp support error 1005
  • dis lyrics
  • bea 71 series staples
  • odessey
  • bea test
  • mtv 5 cover
  • new england patriots needs
  • la ink 04x01
  • vince young 99 yard video
  • rotation
  • search operatorssearch people
  • battleship hacked
  • bea verdi
  • zara phillips fascinator