all
Now I'm editing c sources with emacs under c-mode. How ever auto-fill-mode doesn't seem to work at all. Here how I enabled and tried to use it.
M-x auto-fill-mode (enable auto-fill-mode)
Typed in a line longer than auto-fill size(which 80 characters for now) --> didn't break the line
Tried to auto-filled by issuing M-q
However above attempt didn't work out at all.
Could anybody point out that what have I done wrong?
Thanks for your help in advance.
When you use auto-fill-mode in c-mode, the default behavior is to wrap text only when writing text, as in a comment. You can override this by customizing the value of c-ignore-auto-fill. Note that emacs will wrap and indent your code as text, which is probably not what you want.
A better solution is probably to bind space to a function like this:
(defun insert-space-or-newline-and-indent ()
(interactive)
(if (>= (current-column) fill-column)
(newline-and-indent)
(insert-char ? )))
I have an issue using Emacs 24.1.1 on Mac OS X. I'm editing Jade and CoffeeScript files, so I've turned on whitespace-mode for those file types.
What I'm seeing is that lines longer than 70 characters are highlighted with the whitespace-line font face, regardless of the setting of whitespace-line-column.
In this shot, it is clear that I've customized whitespace-line-column to track fill-column, and I've set fill-column to 120, but much shorter lines are being highlighted.
I've glanced over the code for the Jade mode and don't see anything that would explain the behavior, but I have only a passing understanding of Emacs Lisp.
Thanks in advance for any pointers!
You have to set whitespace-line-column before you activate whitespace-mode. That is, if you want to change its value it does not take effect unless you turn whitespace-mode off and on again. Ironically, that variable is not available for M-x customize until you have activated the mode once :-(
However, you can customize the global value of this variable by putting the following line in your .emacs file:
(setq whitespace-line-column 120)
Since your .emacs is evaluated when you start Emacs, the setting will take effect before you invoke whitespace-mode for the first time and should thus do what you want. If you don't want to set the value globally, but only for Jade files, put the following in your .emacs file instead:
(set (make-local-variable 'whitespace-line-column) 80)
(add-hook 'after-change-major-mode-hook
'(lambda () (when (eq major-mode 'jade-mode)
(setq whitespace-line-column 120))))
If you never want long lines to be highlighted specially at all, there is a third option you might want to consider. You could customize the variable whitespace-style (by typing M-x customize-variable ENTER whitespace-style ENTER) and in the value list remove the entries:
lines
lines-tail
(if any). This should turn off highlighting of long lines globally independent of the value of whitespace-line-column (again, only after you de- and re-activate whitespace mode).
I am trying to use setnu.el to give me line numbers in emacs, which as you might imagine I want in pretty much every mode. It seemed like the preffered way of doing this in Emacs is to use
(add-hook 'text-mode-hook 'turn-on-setnu-mode)
but this isn't working for me. Using
(add-hook 'emacs-lisp-mode-hook 'turn-on-setnu-mode)
works just fine when I am editing emacs lisp files, but I want line numbers in all my text viewing and don't want to have a special case for each kind of file in my init.d file. Help would be much appreciated.
Linum seems to be distributed with emacs >=22.
Try:
(require 'linum)
Then toggle the display of line numbers with
M-x linum-mode
http://web.student.tuwien.ac.at/~e0225855/linum/linum.html
I enable them by pressing: M-x linum-mode. How can I "translate" that into my.emacs file in order to enable it automatically every time I open EMACS?
Drop (global-linum-mode t) into your .emacs if you want it for every buffer. Otherwise, you can add a function to the appropriate hook of modes you're interested in to enable it for just that mode.
You should really read though the manual like I suggested in the last question of yours that I answered. ;)
Since Emacs 26, the new way is to use (global-) display-line-numbers-mode:
(global-display-line-numbers-mode 1)
One more solution is to use a linum-mode
linum-mode works fast on large files, so in order to enable it for your mode put this into your configuration:
(add-hook 'js2-mode-hook
(lambda ()
(linum-mode 1)))
linum-mode is a part of Emacs after version 22
More documentation about this mode is here
You can also put (line-number-mode 1) into your .emacs file. This way you can also have it be mode specific:
(defun my-c-mode-common-hook ()
(line-number-mode 1))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
That way, it will only put the line numbers in if it's a C/C++ file.
You might want to consider this code in your .emacs file:
;; always show line numbers
(global-linum-mode 1)
;; insert a space if running in text mode
(if window-system
(progn)
(setq linum-format "%d ")
)
It adds a space between the line number and the editable lines in text mode, as it is done in graphics mode.
Sometimes I need to read log files that have ^M (control-M) in the line endings. I can do a global replace to get rid of them, but then something more is logged to the log file and, of course, they all come back.
Setting Unix-style or dos-style end-of-line encoding doesn't seem to make much difference (but Unix-style is my default). I'm using the undecided-(unix|dos) coding system.
I'm on Windows, reading log files created by log4net (although log4net obviously isn't the only source of this annoyance).
(defun remove-dos-eol ()
"Do not show ^M in files containing mixed UNIX and DOS line endings."
(interactive)
(setq buffer-display-table (make-display-table))
(aset buffer-display-table ?\^M []))
Solution by Johan Bockgård. I found it here.
Modern versions of emacs know how to handle both UNIX and DOS line endings, so when ^M shows up in the file, it means that there's a mixture of both in the file. When there is such a mixture, emacs defaults to UNIX mode, so the ^Ms are visible. The real fix is to fix the program creating the file so that it uses consistent line-endings.
What about?
C-x RET c dos RET C-x C-f FILENAME RET
I made a file that has two lines, with the second having a carriage return. Emacs would open the file in Unix coding, and switching coding system does nothing. However, the universal-coding-system-argument above works.
I believe you can change the line coding system the file is using to the Unix format with
C-x RET f UNIX RET
If you do that, the mode line should change to add the word "(Unix)", and all those ^M's should go away.
If you'd like to view the log files and simply hide the ^M's rather than actually replace them you can use Drew Adam's highlight extension to do so.
You can either write elisp code or make a keyboard macro to do the following
select the whole buffer
hlt-highlight-regexp-region
C-q C-M
hlt-hide-default-face
This will first highlight the ^M's and then hide them. If you want them back use `hlt-show-default-face'
Edric's answer should get more attention. Johan Bockgård's solution does address the poster's complaint, insofar as it makes the ^M's invisible, but that just masks the underlying problem, and encourages further mixing of Unix and DOS line-endings.
The proper solution would be to do a global M-x replace-regexp to turn all line endings to DOS ones (or Unix, as the case may be). Then close and reopen the file (not sure if M-x revert-buffer would be enough) and the ^M's will either all be invisible, or all be gone.
You can change the display-table entry of the Control-M (^M) character, to make it displayable as whitespace or even disappear totally (vacuous). See the code in library pp-c-l.el (Pretty Control-L) for inspiration. It displays ^L chars in an arbitrary way.
Edited: Oops, I just noticed that #binOr already mentioned this method.
Put this in your .emacs:
(defun dos2unix ()
"Replace DOS eolns CR LF with Unix eolns CR"
(interactive)
(goto-char (point-min))
(while (search-forward "\r" nil t) (replace-match "")))
Now you can simply call dos2unix and remove all the ^M characters.
If you encounter ^Ms in received mail in Gnus, you can use W c (wash CRs), or
(setq gnus-treat-strip-cr t)
what about using dos2unix, unix2dos (now tofrodos)?
sudeepdino008's answer did not work for me (I could not comment on his answer, so I had to add my own answer.).
I was able to fix it using this code:
(defun dos2unix ()
"Replace DOS eolns CR LF with Unix eolns CR"
(interactive)
(goto-char (point-min))
(while (search-forward (string ?\C-m) nil t) (replace-match "")))
Like binOr said add this to your %APPDATA%.emacs.d\init.el on windows or where ever is your config.
;; Windows EOL
(defun hide-dos-eol ()
"Hide ^M in files containing mixed UNIX and DOS line endings."
(interactive)
(setq buffer-display-table (make-display-table))
(aset buffer-display-table ?\^M []))
(defun show-dos-eol ()
"Show ^M in files containing mixed UNIX and DOS line endings."
(interactive)
(setq buffer-display-table (make-display-table))
(aset buffer-display-table ?\^M ?\^M))
(add-hook 'text-mode-hook 'hide-dos-eol)