Implementing pwdw pwdc, a summary of manipulating directory name strings in Emacs lisp

1 Summary

  1. this article summaries several technical tricks for implementing two functions
    • pwdw: copy the dir name of the current buffer (in Windows format) to the kill-ring (clip-board)
    • pwdc: copy the dir name of the current buffer (in cygwin format) to the kill-ring (clip-board)
  2. These two functions are lisp alternatives to my shell functions pwdw and pwdc, for passing the current working directory from cygwin to windows and other programs.
  3. Emacs-lisp code:
    ;; pwdw and pwdc
    (defun pwdw ()
      "Copy the dir name of the current buffer (in Windows format) to the kill-ring (clip-board)."
      (interactive)
      (kill-new (replace-regexp-in-string "/" "\\\\"
                                          (file-name-directory (expand-file-name (buffer-file-name)))))
      )
    (defun pwdc ()
      "Copy the dir name of the current buffer (in cygwin format) to the kill-ring (clip-board)."
      (interactive)
      (kill-new
       (replace-regexp-in-string "\\([a-zA-Z]\\):" "/cygdrive/\\1"
                                 (file-name-directory (expand-file-name (buffer-file-name)))))
      )
    
  4. shell code (compatible with bash and zsh)
    # note: you need -d to convert unix line-ending to dos line-ending for Windows
    alias pwdw='cygpath -w $PWD | tee >(putclip -d)'
    alias pwdc='echo -En $PWD | putclip'
    

2 Technical tricky points in these functions

  1. about path and file names
  2. replace-regexp-in-string: replacing / with \ or c:/ with cygdrive/c
  3. kill-new: to make the text in the kill ring:
  4. about regex in Emacs

Author: Da Zhang

Created: 2016-02-12 Fri 16:54

Emacs 25.0.90.3 (Org mode 8.3.3)

 

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s