2

I have a directory that is all little shell code snippets for being sourced.

Tried creating an auto-mode-alist, that does not seem to be effective, so tried creating a .dir-locals.el thusly:

((nil . ((mode . sh))))

which does indeed, set the mode of all files to sh-mode.

However, now cannot run dired, get this message:

dired-readin: Wrong type argument: stringp, nil

What should I do to allow dired to run normally in this dir?

Related: How to set major mode for a file in .dir-locals.el?

Or, to rephrase the question, can specific files, not subdirs be specified, so as to only apply to those files? IOW, not dot files?

Addendum

This was the auto-mode-alist attempt:

(defvar home-lib
  `,(concat (substitute-in-file-name "$HOME/lib") "/.*\\'")
  "Regexp for auto-mode-alist to find anything in ~/lib to be a sh-mode file.")

(add-to-list 'auto-mode-alist `(,home-lib . sh-mode))
  • Just what did you try? Show your code or give a recipe of commands used. And do you see the same problem if you start Emacs using emacs -Q (no init file)? – Drew Jun 19 at 19:08
  • @Drew The code is in the question. What else do you expect the question to contain? – Gilles 'SO- stop being evil' Jun 19 at 19:45
  • 2
    It would be interesting to see the attempt at setting auto-mode-alist, as it should be possible to set the mode for all files in this directory based on the path to the files, not just the file names. – InHarmsWay Jun 19 at 23:47
  • @dithotx, your auto-mode-alist code works fine for me. You haven't used regexp-quote on the literal portion of the pattern, so it's not the correct way to build that regexp; but most $HOME dir paths would probably not cause problems in practice. – phils Jun 20 at 22:42
0

Regarding .dir-locals.el, assuming the files are being opened in fundamental-mode, you can simply target that:

((fundamental-mode . ((mode . sh))))

Tried creating an auto-mode-alist, that does not seem to be effective

There's no reason why it shouldn't be effective. e.g.:

(add-to-list 'auto-mode-alist (cons REGEXP 'sh-mode))

Where REGEXP is a regular expression matching the filename.  e.g.:

- Any filename with a particular extension ".foo":
  (rx ".foo" eos)

- Any file in a specific directory:
  (rx bos "/path/to/directory/")

- Only *.c filenames under that directory:
  (rx bos "/path/to/directory/" (zero-or-more not-newline) ".c" eos)

- Match some sub-path anywhere in a filename:
  (rx "/sub/path/foo")

- A specific individual file:
  (rx bos "/path/to/file" eos)
  • Thanks, @phils. First answer here was the ticket. – dithotx Jun 20 at 20:39
  • Thanks again @phils, both answers worked. The problem with the auto-mode-alist may have had something to do with there being multiple instances, and the subsequent one was an accretion over several years. Reworked it with the rx and regexp-opt and that fixed the issue, so the .dirs-local.el was no longer required. – dithotx Jun 22 at 0:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.