Emacs: Set tab indent for just one file on the fly

前端 未结 6 1509
无人及你
无人及你 2021-02-01 17:02

I work on an open source project where the creator sets his tab-indents to 2 spaces.

I\'d like to just enable it on the fly for the one file I work on and not other fil

相关标签:
6条回答
  • 2021-02-01 17:53

    You could also use file local variables to automate omrib's solution for that one file, by adding this to it:

    // Local Variables:
    // js-indent-level: 2
    // indent-tabs-mode: nil
    // End:
    
    0 讨论(0)
  • 2021-02-01 18:03

    Create a file ".dir-locals.el" in the project's directory and fill it like this:

    ((nil . ((tab-width . 2))))
    

    This will take care of setting tab-width automatically and you don't have to modify the actual file (which is likely version-controlled.)

    See the manual for more information about the format. I believe this requires Emacs 23.

    0 讨论(0)
  • 2021-02-01 18:03

    As indicated by others, one issue with the File Local Variables approach is that you need to modify the file, and that's not ideal if you need to keep those declarations out of version control.

    If you want the variables to apply to all files under a given directory, then Directory Local Variables is obviously the way to go, and you can implement that with either a .dir-locals.el file, or by calling (dir-locals-set-directory-class):

    • http://www.emacswiki.org/emacs/DirectoryVariables
    • http://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html

    I prefer the directory class approach myself, and I was thinking that it's a shame that there isn't an analogous approach for file local variables, but I found that the directory class code actually works perfectly with files, and the only issue is that dir-locals-set-directory-class calls file-name-as-directory on its argument, which prevents it from being matched, due to the trailing slash.

    The following therefore is a way to configure directory local variables for a single file, without modifying the file itself, or affecting other files under the same parent directory.

    (defun my-file-locals-set-directory-class (file class &optional mtime)
      "Enable 'directory local' classes for individual files,
    by allowing non-directories in `dir-locals-directory-cache'.
    Adapted from `dir-locals-set-directory-class'."
      (setq file (expand-file-name file))
      (unless (assq class dir-locals-class-alist)
        (error "No such class `%s'" (symbol-name class)))
      (push (list file class mtime) dir-locals-directory-cache))
    
    (dir-locals-set-class-variables
     'my-javascript-class
     '((nil . ((js-indent-level . 2)
               (indent-tabs-mode . nil)))))
    
    (my-file-locals-set-directory-class
     "path/to/the/file.js" 'my-javascript-class)
    
    0 讨论(0)
  • 2021-02-01 18:06

    You can make the variable js-indent-level local to the buffer using:

    M-x make-variable-buffer-local <RET> js-indent-level <RET>

    Then you can set that variable in the buffer using:

    M-x set-variable <RET> js-indent-level <RET> 2

    0 讨论(0)
  • 2021-02-01 18:06

    The easiest way to do this for a single buffer is to use M-x set-variable.

    1. Type M-x set-variable and press enter
    2. When prompted for the variable to set, set tab-width then press enter
    3. You'll be prompted with the line Set tab-width (buffer-local) to value:. Put the value you want, then hit enter

    The buffer should instantly be updated with the new value.

    0 讨论(0)
  • 2021-02-01 18:08

    I use a snippet of code in my init.el that tries to auto-detect files that use 2-space indents, and switch Emacs's indentation for that file to 2 spaces when it sees such files:

    (add-hook 'js-mode-hook
              (lambda ()
                (when (string-match-p "^  [A-Za-z]" (buffer-string))
                  (make-variable-buffer-local 'js-indent-level)
                  (set-variable 'js-indent-level 2))))
    
    0 讨论(0)
提交回复
热议问题