I'm trying to open a file from terminal and ensure that line-wrap will be off. I'm trying the following code, but it won't work

emacs --no-window-system --eval '(setq truncate-lines 1)' file

I don't want to set it as default or to change init. Just to open this specific file without wraping lines.


truncate-lines is buffer-local. To set the default for all buffers, use (setq-default truncate-lines t). Major-modes and minor-modes can reset this variable and it will be necessary to use a major-mode or minor-mode hook in that case, or directory-local variables, or file-local variables.

EXAMPLE [assumes we want to load a file with an extension of el, which will default to the emacs-lisp-mode]:

/path/to/emacs --no-window-system --eval "(add-hook 'emacs-lisp-mode-hook (lambda nil (setq truncate-lines t)))" /path/to/file.el

When the file loads, check the value with M-x describe-variable RET truncate-lines RET or C-h v.

EDIT:  In the commentary below, the O.P. has indicted that certain files may be opened in fundamental-mode because a particular file extension may not be associated with a major-mode. While it is possible to use file-local variables or directory-local variables, the O.P. has expressed a preference to handle this entirely on the command-line. One way to accomplish that is as follows:

/path/to/emacs --no-window-system --eval "(progn (find-file \"/path/to/file.csv\") (setq truncate-lines t))"

NOTE: find-file (with its underlying functions) set the buffer of the newly opened file as the current-buffer. When (setq truncate-lines t) is called subsequent to (find-file ...), it is applied to said current-buffer. If the target buffer did not have focus for whatever the reason (e.g., find-file-noselect), then it would be necessary to use something like (with-current-buffer ...) to ensure that truncate-lines is applied to the correct buffer.

| improve this answer | |
  • [The O.P. has indicated briefly in a comment that was withdrawn that he/she wishes to open a file with a csv extension. My comment is in relation thereto.] A csv file extension is not associated with a recognized major-mode using Emacs without any configuration whatsoever. Would the O.P. prefer a file with a csv extension to be in any particular major-mode when the file gets opened in Emacs? Or, do you prefer fundamental-mode? If you prefer fundamental-mode, then you could use /path/to/emacs --no-window-system --eval "(setq-default truncate-lines t)" /path/to/file.csv – lawlist Jan 20 '18 at 20:27
  • The file opens in Fundamental mode, so I cannot use hooks. I'm trying to figure out some way of doing it. If you know, please help. – rvbarreto Jan 20 '18 at 20:33
  • If you don't mind using (setq-default ...), then the example in the previous comment does the job. Otherwise, consider using file-local or directory-local variables ... – lawlist Jan 20 '18 at 20:34
  • The problem is that the code will run in a server by a lot of different users and I don't want my code to be invasive and change their emacs defaults – rvbarreto Jan 20 '18 at 20:35
  • How about wrapping your find-file function inside the function that sets the truncate-lines variable? /path/to/emacs --no-window-system --eval "(progn (find-file \"/path/to/file.csv\") (setq truncate-lines t))" This example assumes that the target buffer is set by find-file so that truncate-lines is set locally in that buffer which has focus. Depending upon the complexity of the function being run, we could use (with-current-buffer ....) – lawlist Jan 20 '18 at 20:42

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.