要点
find [搜索路径] [要跳过搜索的东西] -prune -o [其它筛选条件] [要执行的操作] 例 find ./ -path "*/.git" -prune -o -type f,d -name "*.*" -print -path 后面要接文件路径/路径的模式串path ... -prune 是联合使用的, 作用是跳过这些文件-type f,d 表示结果保留普通文件f 和目录d -name 模式串 是对指定文件名进行搜索-print 是对find找到的满足条件的文件 实施的动作: 打印文件路径- 命令中的双引号在
elisp 中要进行转义才能用 default-directory 是该进程的pwd ( locate-dominating-file 起始目录 目标文件 ) : 从起始目录向上级目录的方向查找包含目标文件的路径( shell-command-to-string shell命令) 将shell命令的运行结果以字符串的形式返回( split-string 待分割串 分隔符 ) 返回切分后形成的字符串**list **( ivy-read 提示符 候选list ) 获取用户输入或在候选中的一项(find-file 文件路径) 打开文件(file-exists-p 路径) 判断文件路径是否合法
(require 'ivy)
(defun my-find-files()
(interactive)
(let* ((cmd "find ./ -path \"*/.git\" -prune -o -type f,d -name \"*.*\" -print ")
(default-directory (locate-dominating-file "." ".git" ) )
(output (shell-command-to-string cmd))
(lines (split-string output "[\n\r]+" ))
)
(message "cmd: %s" cmd)
(setq selected-line (ivy-read (format "choose the file[%s]: " default-directory ) lines))
(message ">> %s" selected-line)
(when (and selected-line (file-exists-p selected-line ))
(find-file selected-line)
)
)
)
(defun my-find-files-helper(start-dir)
(interactive)
(let* ((cmd "find ./ -type f,d -path \"*/.git\" -prune -o -print -name \"*.*\" ")
(default-directory start-dir )
(output (shell-command-to-string cmd))
(lines (split-string output "[\n\r]+" ))
)
(message "cmd: %s" cmd)
(setq selected-line (ivy-read (format "choose the file[%s]: " default-directory ) lines))
(message ">> %s" selected-line)
(when (and selected-line (file-exists-p selected-line ))
(find-file selected-line)
)
)
)
(defun my-find-files-in-project()
(interactive)
(my-find-files-helper (locate-dominating-file "." ".git" ))
)
(defun my-find-files-in-level(&optional level)
(interactive "P")
(unless level
(setq level 0))
(setq i 0)
(setq start-dir default-directory )
(while (< i level)
(setq start-dir (file-name-directory (directory-file-name start-dir) ))
(setq i (+ i 1) )
)
(my-find-files-helper start-dir)
)
|