Skip navigation

Now that I’m getting fond of using a heredoc to insert mysql scripts into the bash shell, have put this into my emacs initialization file. The same shortcut (Control+Shift+f11) will conveniently write different parameters depending in which shell I’m sitting in.

UPDATE: seems that I spoke too soon. Asking about the present working directory, formerly “(let ((a (shell-command-to-string “pwd | tr -d ‘\\n'”)))”, didn’t actually work when having opened shells of different servers via Tramp.
The correction below (less elegant) depends on the shell buffer being created/renamed with an identifiable name -which could be the hostname or whatever consistent nomenclature we choose-.
Anyway, until figuring a better way, this does the job of inserting the right parameters into each shell:

(defun my-heredoc-sql-invocation-from-shell ()
"Insert the appropiate parameters to run a heredoc mysql query depending on which shell I'm in"
  (let ((a (buffer-name (window-buffer (minibuffer-selected-window))))
        (b nil))
    (cond ((string-match "serverA" a) 
           (setq b "mysql -uroot mainDbToHitAt_A--password=`cat /etc/security/mysqlpassword` -t -vv <<\\!"))
          ((string-match "serverB" a) 
           (setq b "mysql -ualpha mainDbToHitAt_B --password=`cat /etc/security/mysqlpassword` -t -vv <<\\!"))
          ((string-match "serverC" a) 
           (setq b "mysql -uroot mainDbToHitAt_C  -t <<\\!")))
    (insert b)))

;; key shortcut to bind it to
(global-set-key (kbd "C-S-<f11>")  'my-heredoc-sql-invocation-from-shell)



  1. There are a couple options for improving this code.

    #1 – Instead of setting ‘b in cond and inserting it, add a default clause in cond and insert what it evaluates to:

    (insert (cond ((string-match “serverA” a) “mysql …”)
    (t nil)))

    This is much more idomatic Lisp/FP code.

    #2 – The ‘default-directory variable should contain your PWD, so you don’t need to shell out to get that information. I’m not entirely sure what you’re after, but if it’s just the PWD, this function will extract the local part of that for either local or TRAMP paths:

    (defun local-path (path)
    “Return the local portion of a path.

    If PATH is local, return it unaltered.
    If PATH is remote, return the remote diretory portion of the path.”
    (if (tramp-tramp-file-p path)
    (elt (tramp-dissect-file-name path) 3)

    (local-path “/home/ieure”) ;; -> “/home/ieure”
    (local-path “/ssh:remote-host:/home/ieure”) ;; -> “/home/ieure”

    #3 – Use an alist to store your options instead of using a cond. Ex:

    (setq mysql-per-host-options
    ‘((“serverA” . “mysql -uroot mainDbToHitAt_A–password=`cat /etc/security/mysqlpassword` -t -vv <<\\!")
    ("serverB" . "…")))

    Then the meat of your function becomes: (insert (cdr (assoc b mysql-host-options)))

    • Ian, Much thanks for reviewing the code and your suggestions for improvements in points #1 and #3
      About the second point, you are totally right: I looked into default-directory and that’s the variable that allows me to recognize where the different shells are opened (actually created) with tramp.
      I did not implement it yet but I saw that will come in handy by running the following quick check:

      (defun my-tramp-hostname-check ()
      (if (string-match "" default-directory)
      (message "gotcha")))

      In regards to you local-path function, how do you feed the path parameter exactly? I see the example in which you just typed it in (“/ssh:remote-host:/home/ieure”), you would call ‘default-directory’ previously somehow?
      Thanks again for your kind response.

        • Ian Eure
        • Posted December 7, 2011 at 7:17 pm
        • Permalink

        default-directory is a variable, not a function. So you would just call (local-path default-directory) to get the CWD.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: