counsel-find-file后输入test/, spacemacs能够正确提示是否创建目录,但使用我自己的配置会提示Use M-x make-directory RET RET to create the directory and its parents ,并跳转到一个空的buffer



(use-package ivy
  :ensure t
  :defer t
  :diminish ivy-mode
    (with-eval-after-load 'recentf
      (setq ivy-use-virtual-buffers t))
    (setq ivy-height 12
          ivy-do-completion-in-region nil
          ivy-wrap t
          ivy-extra-directories nil
          ivy-fixed-height-minibuffer t
          ;; Don't use ^ as initial input
          ivy-initial-inputs-alist nil
          ;; highlight til EOL
          ivy-format-function #'ivy-format-function-line
          ;; disable magic slash on non-match
          ;; ~ to /home/user
          ;; ivy-magic-tilde nil
          ivy-magic-slash-non-match-action nil)
    ;; (setq ivy-re-builders-alist
    ;;       '((t . ivy--regex-fuzzy)))
    ;; (setq confirm-nonexistent-file-or-buffer t)
    (setq ivy-re-builders-alist
          '((t   . ivy--regex-ignore-order)))
    (evil-make-overriding-map ivy-occur-mode-map 'normal)

(use-package counsel
  :ensure t
  :defer t
  :diminish counsel-mode
  :config (counsel-mode 1)
  :bind (("M-x" . counsel-M-x)
         ("C-x C-m" . counsel-M-x)
         ("M-y" . counsel-yank-pop)
         :map ivy-minibuffer-map
         ("C-j" . ivy-next-line)
         ("C-k" . ivy-previous-line)
         ("<tab>" . ivy-alt-done)
         ("TAB" . ivy-alt-done)
         ([escape] . minibuffer-keyboard-quit)
         :map counsel-find-file-map
         ("C-h" . counsel-up-directory)
         ("C-<return>" . ivy-immediate-done)))

Peek 2017-12-29 19-55

我没有用过 Spacemacscounsel-find-file 但是在 find-file的时候创建目录,Emacs 提示 Use M-x make-directory RET RET to create the directory and its parents 是在提示你目标目录是不存在的,如果你想要创建这个目录,只需要在那个空的 buffer C-x C-s 保存,即可以创建这个目录. 这么看来,是Spacemacs 做了优化. BTW, 我创建目录不是像你这样使用 counsel-find-file,我是 C-x d + 来创建目录的.

1 个赞



(defadvice find-file (before make-directory-maybe (filename &optional wildcards) activate)
  "Create parent directory if not exists while visiting file."
  (unless (file-exists-p filename)
    (let ((dir (file-name-directory filename)))
      (unless (file-exists-p dir)
	(if (y-or-n-p (format "directory %s does not exist,do you want you create it?" dir))
	      (make-directory dir)

不知道有没有比 (keyboard-quit) 更优雅的打断函数的方法呢,欢迎其他同学指教.