linux - 如何使用 emacs 键绑定(bind)启动 shell 脚本,将光标下的单词作为变量传递

标签 linux macos bash emacs elisp

通过 emacs 在 osx 上执行下面的脚本,没有用,我收到一条权限被拒绝的消息,这个问题的答案解决了那个问题:https://stackoverflow.com/a/12276562/912475

Tldr:如何设置一个系统,自动将 emacs 中光标下的单词作为变量直接传递到我的 shell 脚本,然后运行该脚本?

我已经创建了一个有点简陋的系统,用于以可靠的方式从纯文本文件“链接”到文件夹。它使用由脚本生成的时间戳,然后设置为剪贴板的“值”。然后将时间戳从剪贴板粘贴到包含相关注释的 txt 文件和文件夹的名称字段中。

为了在阅读 txt 时找到文件夹,我使用了一个带有键绑定(bind)的 emacs 函数,可以将时间戳(作为一个词,所有数字)复制到剪贴板并在聚光灯下搜索(在 osx 上)。我想做的是自动启动一个 shell 脚本,搜索名称以该字符串结尾的目录,然后打开它。我已经有一个执行类似操作的脚本,但我真的不知道如何将 elisp 函数和 shell 脚本结合在一起。我非常感谢适用于 osx 和 linux 的解决方案(我同时使用两者)。可能很容易“移植”适用于任何一个的解决方案以与另一个一起使用。

这是用于复制光标下的单词的 emacs 函数:

;;; function for copying a word under the cursor http://www.emacswiki.org/emacs/CopyWithoutSelection
    (global-set-key (kbd "C-c o")         (quote copy-word))

     (defun copy-word (&optional arg)
      "Copy words at point into kill-ring"
       (interactive "P")
       (copy-thing 'backward-word 'forward-word arg)
       ;;(paste-to-mark arg)
     )

这是查找并打开名称以时间戳结尾的目录的脚本:

#!/bin/bash
PATH=/opt/local/bin:/opt/local/sbin:$PATH #need this to make the gnu coreutils work on osx
file_number="20130812193913"
path_to_open=$(gfind ~/x/ | grep -e $file_number$) # $ means the end of the line, makes it possible to search for directories without finding their content
open "${path_to_open}"

脚本的编辑版本,它接受来自命令行的参数,如下所示:

me$ sh script_path.sh 20130812193913

脚本:

#!/bin/bash
PATH=/opt/local/bin:/opt/local/sbin:$PATH #need this to make the gnu coreutils work on osx
file_number=$1
echo $file_number
path_to_open=$(gfind ~/x/ | grep -e $file_number$) # $ means the end of the line, makes it possible to search for directories without finding their content
open "${path_to_open}"

参见:http://www.bashguru.com/2009/11/how-to-pass-arguments-to-shell-script.html

最佳答案

你可以尝试这样的事情:

(defvar script-name "/foo/bar/my-script")

(defun call-my-script-with-word ()
  (interactive)
  (shell-command
   (concat script-name 
           " "
           (thing-at-point 'word))))
(global-set-key (kbd "C-c o") 'call-my-script-with-word)

关于linux - 如何使用 emacs 键绑定(bind)启动 shell 脚本,将光标下的单词作为变量传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18406429/

相关文章:

c++ - 将 UTC tm* 转换为本地化 tm* 的跨平台方式?

linux - 在Linux中使用cURL POST txt文件和文本

c - Linux 3.0 : "glibc detected" abort - tuning corruption detected feature?

linux - 处于 TIME_WAIT 状态时 SO_REUSEADDR 的行为

macos - macOS 10.12 上两个 Docker 容器之间的通信

c++ - 如何使用 LD_LIBRARY_PATH 和链接使其真正正确?

bash - 在 bash/unix 中获取以前的日期

linux - BASH:如何询问用户输入并存储它以备将来使用?

macos - Torch/Lua 安装后不起作用

Python 清除屏幕