8

EDIT: It turns out that the second edit to my .emacs file actually works. (See the comments below this entry.)

I tried a couple of addition to the .emacs to make all txt files opened in emacs use orgmode. They did not work. How can I make it happen?

;;SET EMACS AS DEFAULT MAJOR MODE TO FOR ALL FILES WITH AN UNSPECIFIED MODE
(setq default-major-mode 'org-mode)

;;OPEN ALL TXT FILES IN ORGMODE
(add-to-list 'auto-mode-alist '("\\.txt$" . org-mode))

Additionally:

It would be even better to open only txt files in a certain directory orgmode. Any hint as to how that could be done would also be appreciated.

N.N.
  • 8,336
  • 12
  • 54
  • 94
  • 1
    Your code should open txt files in org-mode. At least, it works for me. Check your .emacs file if you dont modify a mode for txt files later. – Oleg Pavliv Feb 25 '12 at 07:56
  • I don't it's a very short .emacs cause I'm new to it. But I just realize I now get this message every time I start emacs: "Warning (initialization): An error occurred while loading `/Users/Klsrd/.emacs': End of file during parsing: /Users/Klsrd/.emacs To ensure normal operation, you should investigate and remove the cause of the error in your initialization file. Start Emacs with the `--debug-init' option to view a complete error backtrace." Could that be the cause? –  Feb 25 '12 at 08:02
  • 1
    There is something wrong with your .emacs. Remove everything from it and put just (add-to-list 'auto-mode-alist '("\\.txt$" . org-mode)) to see if it works – Oleg Pavliv Feb 25 '12 at 08:08

4 Answers4

5

Another way to do this is using directory-local variables. This is nice because you can put a file in any directory where you want this behavior to engage, and it works recursively in any subdirectories.

Create a file called .dir-locals.el in the desired directory.

Here are the contents:

((nil (eval . (if (string-match ".txt$" (buffer-file-name))(org-mode)))))

Read this like so: for any major-mode (nil), evaluate the following form:

(if ....  (org-mode))
craig
  • 156
  • 2
  • 5
4

The regex in auto-mode-alist could be something more complex, like "^/path/to/.*\\.txt$"

tripleee
  • 175,061
  • 34
  • 275
  • 318
2

You can implement a hook which verifies the file directory and modifies the buffer mode:

(add-hook 'find-file-hooks 
          (lambda ()
            (let ((file (buffer-file-name)))
              (when (and file (equal (file-name-directory file) "c:/temp/"))
                (org-mode)))))

As an alternative you can add the mode line in the beginning of your text file. In this case emacs will set the specified mode.

; -*- mode: org;-*-
* header 1
** header 2
N.N.
  • 8,336
  • 12
  • 54
  • 94
Oleg Pavliv
  • 20,462
  • 7
  • 59
  • 75
0

I glued together some code from Oleg Pavliv's answer here, and from yibe's at elisp - File extension hook in Emacs - Stack Overflow

(defun use-org-mode-for-dot-txt-files-in-owncloud ()
  (when (and (string-match owncloud buffer-file-name)
             (string-match "\\.txt\\'" buffer-file-name))
    (org-mode)))
(add-hook 'find-file-hook 'use-org-mode-for-dot-txt-files-in-owncloud)

This way, though ownCloud Web and phone apps are currently friendly only with .txt files, from my PC I can use Emacs' Org-mode for them.

(If I set all .txt files to use Org-mode, it breaks todotxt-mode.)

(Note that owncloud is a string variable equal to my ownCloud path.)

Community
  • 1
  • 1
Brady Trainor
  • 2,026
  • 20
  • 18