ฉันต้องการสร้างไฟล์ใหม่ในโหมดไดเรด มีคำสั่ง "สร้างไฟล์ใหม่" ในโหมด dired หรือไม่ ตัวอย่างเช่น เมื่อฉันพิมพ์ "c" ในโหมด dired มันจะสร้าง "untitled.txt" มันง่ายมาก แต่ฉันหามันไม่เจอ
จะสร้างไฟล์ใหม่จากโหมด dired ได้อย่างไร?
คำตอบ (6)
เพียงกด C-x C-f สิ่งนี้จะแจ้งให้ระบุชื่อไฟล์โดยใช้ไดเร็กทอรีปัจจุบันของบัฟเฟอร์ปัจจุบันเป็นไดเร็กทอรีที่จะใส่เข้าไป สำหรับบัฟเฟอร์ที่เสียหาย ไดเร็กทอรีปัจจุบันเป็นเพียงไดเร็กทอรีที่คุณกำลังดูอยู่
File does not exist, create buffer?
ฉันเลือก y
ฉันได้รับบัฟเฟอร์ใหม่พร้อมชื่อไฟล์ที่ฉันป้อน อย่างไรก็ตาม หากฉันปล่อยบัฟเฟอร์ว่างไว้ เมื่อฉันทำ C-x C-w มันจะแจ้งให้ฉันป้อนชื่อไฟล์อีกครั้ง และฉันก็กลับมาที่จุดเริ่มต้นอีกครั้ง กล่าวอีกนัยหนึ่ง วิธีนี้บังคับให้ฉันป้อนชื่อไฟล์สองครั้ง สมมติว่าฉันเพียงต้องการสร้างไฟล์แต่ไม่ได้ป้อนข้อความใดๆ มีวิธีแก้ไขปัญหานี้หรือไม่
- person incandescentman; 20.08.2015
touch filename
ใช้งานได้เฉพาะกับระบบที่ไม่ใช่ Windows และเฉพาะในกรณีที่คุณยังไม่มีเชลล์เปิดอยู่ใน Emacs
- person Robin Green; 20.08.2015
หากคุณต้องการให้ c
ในโหมด Dired ทำสิ่งที่ C-x C-f
ทำ คำตอบคือไม่สำคัญ:
(define-key dired-mode-map "c" 'find-file)
หรือถ้าคุณต้องการให้มีชื่อ untitled.txt
ให้:
(define-key dired-mode-map "c"
(lambda () (interactive) (find-file "untitled.txt")))
(with-eval-after-load 'dired ...)
- person Jay; 29.09.2016
ขอขอบคุณทุกคน ในที่สุดฉันก็แก้ปัญหาด้วยตัวเอง นี่คือคือคำตอบของฉัน การพิมพ์ "c" ในโหมด dired จะทำให้คุณสร้างไฟล์ที่ไม่มีชื่อใหม่ จากนั้นกด Enter จะสร้างไฟล์ที่ไม่มีชื่อใหม่ ใช่มันเป็นรหัสที่ละเอียดมาก อาจมีคนแก้ไขได้
(eval-after-load 'dired
'(progn
(define-key dired-mode-map (kbd "c") 'my-dired-create-file)
(defun create-new-file (file-list)
(defun exsitp-untitled-x (file-list cnt)
(while (and (car file-list) (not (string= (car file-list) (concat "untitled" (number-to-string cnt) ".txt"))))
(setq file-list (cdr file-list)))
(car file-list))
(defun exsitp-untitled (file-list)
(while (and (car file-list) (not (string= (car file-list) "untitled.txt")))
(setq file-list (cdr file-list)))
(car file-list))
(if (not (exsitp-untitled file-list))
"untitled.txt"
(let ((cnt 2))
(while (exsitp-untitled-x file-list cnt)
(setq cnt (1+ cnt)))
(concat "untitled" (number-to-string cnt) ".txt")
)
)
)
(defun my-dired-create-file (file)
(interactive
(list (read-file-name "Create file: " (concat (dired-current-directory) (create-new-file (directory-files (dired-current-directory))))))
)
(write-region "" nil (expand-file-name file) t)
(dired-add-file file)
(revert-buffer)
(dired-goto-file (expand-file-name file))
)
)
)
- กด C-x C-f
- ป้อนชื่อไฟล์
- กด C-j
หลังจากขั้นตอนข้างต้น emacs จะสร้างบัฟเฟอร์ว่างตามชื่อที่คุณป้อน แต่ emacs ไม่รองรับการสร้างไฟล์เปล่าตามค่าเริ่มต้น คุณสามารถอ้างถึง ฉันจะสร้างไฟล์ว่างใน emacs ได้อย่างไร สำหรับแนวคิดเพิ่มเติม
รายการต่อไปนี้มีสอง (2) ตัวเลือก หนึ่งในนั้นกำหนดให้ touch
อยู่ใน $PATH
หรืออาจใช้เส้นทางสัมบูรณ์ก็ได้ touch
โดยปกติจะมีอยู่ในระบบยูนิกซ์ เช่น OSX เป็นต้น ฟังก์ชันนี้จะกำหนดหมายเลขไฟล์/บัฟเฟอร์อย่างต่อเนื่องโดยอัตโนมัติ เช่น untitled.txt; untitled1.txt; untitled2.txt ฯลฯ
(define-key dired-mode-map (kbd "c") 'dired-new-file)
(defun dired-new-file ()
(interactive)
(let* (
(n 0)
lawlist-filename
(dired-buffer-name (buffer-name)))
(catch 'done
(while t
(setq lawlist-filename (concat "untitled"
(if (= n 0) "" (int-to-string n))
".txt"))
(setq n (1+ n))
(if (not (file-exists-p lawlist-filename))
(throw 'done nil)) ))
(message "[b]uffer + file (maybe) | [f]ile + buffer (maybe)")
(let ((file-or-buffer (read-char-exclusive)))
(cond
((eq file-or-buffer ?b)
(switch-to-buffer (get-buffer-create lawlist-filename))
(text-mode)
(or (y-or-n-p (format "Save Buffer `%s'? "lawlist-filename))
(error "Done."))
(write-file lawlist-filename)
(with-current-buffer dired-buffer-name
(revert-buffer)))
((eq file-or-buffer ?f)
(start-process "touch-file" nil "touch" lawlist-filename)
(revert-buffer)
(or (y-or-n-p (format "Open `%s'? "lawlist-filename))
(error "Done."))
(find-file lawlist-filename)
(text-mode))
(t (message "You have exited the function.")) )) ))
หากคุณต้องการป้อนชื่อไฟล์ ฉันคิดว่านี่ซ้ำกับ:
ฉันจะสร้างไฟล์ว่างใน emacs ได้อย่างไร
หากคุณไม่ต้องการป้อนชื่อไฟล์ คุณยังสามารถใช้คำตอบบางคำตอบเหล่านั้นได้ และปรับเปลี่ยนคำตอบอื่นๆ ได้อย่างง่ายดายด้วยการเขียนโค้ดชื่อแบบฮาร์ดโค้ด แทนที่จะต้องถามแบบโต้ตอบ
untitled.txt
อยู่แล้ว? เชื่อฉันเถอะว่าปัญหานี้ได้รับการแก้ไขแล้ว และC-x C-f
เป็นวิธีที่ง่ายที่สุด - person abo-abo   schedule 30.11.2013touch
อะนาล็อกที่เลวร้าย ยอมรับมัน - person Dodgie   schedule 30.01.2017