安利个功能,在 iTerm2 下 cd 到当前文件所在路径

;; mac上打开iterm2,并cd到当前编辑的文件所在目录
;; 需要点菜单栏里 安装shell integration 
;;;###autoload
(defun cd-iterm2()
  (interactive)
  (let ((cmd (format "
tell application \"iTerm\"
	activate
	if (count of windows) = 0 then
		set w to (create window with default profile)
	else
		set w to current window
	end if

	tell w
		set targetSession to null

		activate current session
		tell current session of w
			if is at shell prompt then
				set targetSession to current session of w
			end if
		end tell
		if targetSession is null then
			repeat with aTab in tabs
				if targetSession is null then
					tell aTab
						select
						repeat with aSession in sessions
							if targetSession is null then
								tell aSession
									select
									if is at shell prompt then
										set targetSession to aSession
									end if
								end tell
							end if
						end repeat
					end tell
				end if
			end repeat
		end if
		if targetSession is null then
			create tab with default profile
			-- delay 0.1
			set targetSession to current session of w
		end if

		if targetSession is not null then
			tell targetSession
				select
				set cmd to \"cd \" & quote & \"%s\" & quote & \";clear\"
				write text cmd
			end tell

		end if
	end tell
end tell
" (expand-file-name default-directory))))
    (start-process "cd-iterm2" nil "osascript" "-e" cmd)))
2 个赞

支持Tramp的版本:

(defun mac-iTerm-shell-command (text)
  "Write TEXT into iTerm like user types it with keyboard."
  (interactive
   (list
    (read-shell-command "Run Shell command in iTerm: "
                        (when (use-region-p)
                          (buffer-substring-no-properties
                           (region-beginning)
                           (region-end))))))
  (do-applescript
   (concat
    "tell application \"iTerm\"\n"
    "    activate\n"
    "    create window with default profile\n"
    "    tell current session of current window\n"
    "        write text \"" text "\"\n"
    "    end tell\n"
    "end tell")))
(defun mac-iTerm-shell-command-current (text)
  "Write TEXT into iTerm like user types it with keyboard."
  (interactive
   (list
    (read-shell-command "Run Shell command in iTerm: "
                        (when (use-region-p)
                          (buffer-substring-no-properties
                           (region-beginning)
                           (region-end))))))
  (do-applescript
   (concat
    "tell application \"iTerm\"\n"
    "    activate\n"
    "    tell current session of current window\n"
    "        write text \"" text "\"\n"
    "    end tell\n"
    "end tell")))
(defun mac-iTerm-cd (dir)
  "Switch to iTerm and change directory there to DIR."
  (interactive (list
                ;; Because shell doesn't expand 'dir'
                (expand-file-name
                 (if current-prefix-arg
                     (read-directory-name "cd to: ")
                   default-directory))))
  (if (file-remote-p dir)
      (let* (
             (host (tramp-file-name-host (tramp-dissect-file-name dir)))
             (dir (tramp-file-name-localname (tramp-dissect-file-name dir)))
             (sshcmd (format "ssh %s" host))
             (cdcmd (format "cd %s" dir))
             )
        (mac-iTerm-shell-command sshcmd)
        (mac-iTerm-shell-command-current cdcmd)
        )
    (let ((cmd (format "cd %s" dir)))
      (mac-iTerm-shell-command cmd))
    )
  )
1 个赞

你的这个每次都新建一个窗口 不太好, 我的是尽量避免多开窗口、tab页

下面的是我用的,代码比较少,没处理一些边边角角的情况,比如(终端正在运行别的程序或者 Prompt 后面不干净)

(defun chunyang-mac-iTerm-send-string (string)
  (do-applescript
   (concat
    "tell application \"iTerm\"\n"
    "    activate\n"
    "    tell current session of current window\n"
    "        write text \"" string "\"\n"
    "        end tell\n"
    "end tell")))

(defun chunyang-mac-iTerm-cd (dir)
  "Switch to iTerm and change directory there to DIR."
  (interactive (list
                ;; Because shell doesn't expand 'dir'
                (expand-file-name
                 (if current-prefix-arg
                     (read-directory-name "cd to: ")
                   default-directory))))
  (let ((cmd (format "cd %s" dir)))
    (chunyang-mac-iTerm-send-string cmd)))

不知道从哪抄的_(:з」∠)_

;; Iterm2 Intergration
(defun iterm-focus ()
  (interactive)
  (do-applescript
   " do shell script \"open -a iTerm\"\n"))

(defun iterm-command (cmd)
  "Go to present working dir and focus iterm"
  (interactive)
  (do-applescript
   (concat
    " do shell script \"open -a iTerm\"\n"
    " tell application \"iTerm2\"\n"
    "   tell the current session of current window\n"
    (format "     write text \"%s\" \n" cmd)
    "   end tell\n"
    " end tell\n")))

(defun iterm-goto-filedir-or-home ()
  "Go to present working dir and focus iterm"
  (interactive)
  (iterm-command (concat (replace-regexp-in-string "\\\\" "\\\\\\\\"
                                                   (shell-quote-argument (or default-directory "~")))
                         "; clear")))