On GitHub
why does the code look like this?
It looks fine in all the code editors out there
Ctrl+M characters present in a file in linux, when originally the file came from Windows environment.
You may need to remove Ctrl+M characters, when you import a text file from MS-DOS (or MS-Windows), and forget to transfer it in ASCII or text mode. Here are several ways to do it; pick the one you are most comfortable with.
The easiest way is probably to use the stream editor sed to remove the ^M characters. Type this command: % sed -e "s/^M//" filename > newfilename
To enter ^M, type CTRL-V, then CTRL-M. That is, hold down the CTRL key then press V and M in succession.
You can also do it in vi: % vi filename
Inside vi [in ESC mode] type: :%s/^M//g
To enter ^M, type CTRL-V, then CTRL-M. That is, hold down the CTRL key then press V and M in succession.
You can also do it inside Emacs. To do so, follow these steps:
Go to the beginning of the document
Type: M-x replace-string RET C-q C-m RET RET
where "RET" means and C-q and C-m mean .
Courtesy: https://its.ucsc.edu/unix-timeshare/tutorials/clean-ctrl-m.html
Related
Recently, my Dired listing in Emacs starting appearing with 015 at the end of each line:
I'm not sure what brought it on. I had been making some changes with my Spacemacs layers but since then I've gone to a completely out-of-the-box Spacemacs configuration and the 015s are still there. It makes Dired pretty much useless because if I try to select a file or drill into a directory it doesn't recognize it. Any ideas or suggestions would be greatly appreciated!
Those are Control M characters. Emacs writes them as either ^M (one char, not two) or \015 (again, one char, not 4).
This Emacs Wiki page tells you about this: EndOfLineTips.
This is some of what it says:
If you see ^M in your file, you may have opened a file with DOS-style line endings (carriage return + line feed) while Emacs assumes it has Unix-style line endings (line feed only). (The carriage-return character, sometimes abbreviated as CR, is ^M. The line-feed character, sometimes abbreviated as LF, is ^J.)
You can reopen the file with the correct line ending with a command like C-x C-m r dos.
C-x C-m r is bound to revert-buffer-with-coding-system. Use C-h k or C-h f to see more about it.
See also (C-h v) variable buffer-file-coding-system.
Also: use i line endings in the Emacs manual, to go to node Coding Systems. That tells all you need to know about this.
This question and its answers might also help. And see the UNIX/Linux command dos2unix.
In the graphical mode, RET does the same as C-m, namely newline.
However, when I use M-a or M-e, they seem to distinguish between them. The newline produced by RET, they skip all to a double-RET point, and with the newline produced by C-m, they move the cursor line by line. Why so?
Description (C-h k) for both keys shows the same.
If it matters, it is Emacs23.3.1 on Ubuntu 12.04.
The answer could be dependent on the mode you're in as well as in the key codes generated by your operating system. When I hit Ctrl-h k Ctrl-m in the *scratch* buffer on my Windows box, I'll get the info for (newline) with the information that it is bound to RET. Theoretically, in some other mode, C-m and RET could be set to different functions. This requires, of course, that the OS doesn't generate the same key code.
What Emacs writes as RET (or as <RET>) is, yes, the same character as C-m - it is the ASCII control character 13. This is so regardless of what keyboard etc. you have.
However, the logical key (aka "key sequence") that Emacs sees when you hit the phisical (keyboard) key named Enter or Return might be different from RET (aka C-m). To see what key Emacs recognizes when you hit that keyboard key, use C-h k and hit the key. If Emacs says RET then this is the typical case, and yes, hitting that key is then identical to pressing Control and hitting m (assuming the key labeled Control does what it does typically).
In the graphical mode, the answer is yes (unless keys are redefined, of course).
Careful examination of my file showed that the effect I observed was a coincidence. It was exactly in those paragraphs which I C-m'ed that all lines would end in a dot (it was a poem).
Actually, C-h k key description says the same thing for both keys. Thanks to schaueho's answer for pushing me in this direction.
I have a text file which I am only able to look that there is an underscore between some words only using emcas editor but not other editors such as vi. I do not know how to use emacs but I wanted to replace these underscores "_" by space in the emacs editor automated fashion. How can I do that ?
I believe that those underscore aren't really underscore, but non breaking space (U+00A0 unicode char), that Emacs show as underscore with a different color. You probably don't need to replace them, but if this is really needed, just use M-x replace-string and kill and yank one of those non-breaking space in the string to be replaced.
Hit the M-x key-combination (that is, hold meta key - alt on windows - and hit x) type replace-string and hit enter. You can then type [underscore] enter [space] enter.
In Emacs notation:
M-x replace-string RET _ RET " "
Should the previous answer not solve it: Remember that as a coding system error. Check with C-x = if it's char 95.
If not, check variables coding-system-for-read, coding-system-for-write, buffer-file-coding-system
Finally, get emacs core developers at help-gnu-emacs#gnu.org
I have a .txt file named COPYING which is edited on windows.
It contains Windows-style line breaks :
$ file COPYING
COPYING: ASCII English text, with CRLF line terminators
I tried to convert it to Unix style using dos2unix. Below is the output :
$ dos2unix COPYING
dos2unix: Skipping binary file COPYING
I was surprised to find that the dos2unix program reports it as a binary file. Then using some other editor (not Emacs) I found that the file contains a control character. I am interested in finding all the invisible characters in the file using Emacs.
By googling, I have found the following solution which uses tr :
tr -cd '\11\12\40-\176' < file_name
How can I do the same in an Emacs way? I tried the Hexl mode. The Hexl mode shows text and their corresponding ASCII values in a single buffer which is great. How do I find the characters which have ASCII values other than 11-12, 40-176 (i.e tab, space, and visible characters)? I tried to create a regular expression for that search, but it is quite complicated.
To see invisible characters, you can try whitespace-mode. Spaces and tabs will be displayed with a symbol in a different face. If the coding system is automatically being detected as dos (showing (DOS) on the status bar), carriage returns at the end of a line will be hidden as well. Run revert-buffer-with-coding-system to switch it to Unix or binary (e.g. C-x RET r unix) and they'll always show up as ^M. The binary coding system will display any non-ASCII characters as control characters as well.
Emacs won't hide any character by default. Press Ctrl+Meta+%, or Esc then Ctrl+% if the former is too hard on your fingers, or M-x replace-regexp RET if you prefer. Then, for the regular expression, enter
[^#-^H^K-^_^?]
However, where I wrote ^H, type Ctrl+Q then Ctrl+H, to enter a “control-H” character literally, and similarly for the others. You can press Ctrl+Q then Ctrl+Space for ^#, and usually Ctrl+Q then Backspace for ^?. Replace all occurrences of this regular expression by the empty string.
Since you have the file open in Emacs, you can change its line endings while you're at it. Press C-x RET f (Ctrl+X Return F) and enter us-ascii-unix as the new desired encoding for the file.
Check out M-x set-buffer-file-coding-system. From the documentation:
(set-buffer-file-coding-system CODING-SYSTEM &optional FORCE NOMODIFY)
Set the file coding-system of the current buffer to CODING-SYSTEM.
This means that when you save the buffer, it will be converted
according to CODING-SYSTEM. For a list of possible values of
CODING-SYSTEM, use M-x list-coding-systems.
So, going from DOS to UNIX, M-x set-buffer-file-coding-system unix.
In GNU screen, I want to change the default command binding to Alt-s (by tweaking .screenrc) instead of the default C-a, the reason is I use emacs hence GNU screen binds the C-a key, sending "C-a" to the emacs becomes tedious (as #Nils said, to send "C-a" I should type "C-a a"), as well as "C-a" in bash shell, and I could change the escape to C- but some of them are already mapped in emacs and other combinations are not as easy as ALT-s . If anyone has already done a ALT key mapping, please do let me know.
It is possible to work around :escape command limitations using registers and :bindkey command. Just put this in .screenrc:
# reset escape key to the default
escape ^Aa
# auxiliary register
register S ^A
# Alt + x produces ^A and acts as an escape key
bindkey "^[x" process S
## Alt + space produces ^A and acts as an escape key
# bindkey "^[ " process S
See http://adb.cba.pl/gnu-screen-tips-page-my.html#howto-alt-key-as-escape
From my reading of man screen it seems like the only meta character that screen can use for the command binding is CTRL:
escape xy
Set the command character to x and the character generating a literal
command character (by triggering the "meta" command) to y (similar to
the -e option). Each argument is either a single character, a two-character
sequence of the form "^x" (meaning "C-x"), a backslash followed by an octal
number (specifying the ASCII code of the character), or a backslash followed
by a second character, such as "\^" or "\\". The default is "^Aa".
If there is some mapping that you don't use in emacs, even if it's inconvenient, like C-|, then you could use your terminal input manager to remap ALT-X to that, letting you use the ALT binding instead. That would be a little hackish though.
I'm an Emacs and screen user as well. Although I rarely use Emacs in a terminal -- and as such in a screen session -- I didn't want to give up C-a for the shell either (which uses Emacs key bindings). My solution was to use C-j as the prefix key for screen, which I was willing to sacrifice. In Emacs programming modes it is bound to (newline-and-indent) which I bound to RET as well, so I really don't miss it.
By the way: I know this is an advise rather than an answer, but I felt this would be valuable enough to post nevertheless.
To make Alt+X the default prefix for commands and free C-a, add the following lines to .screenrc:
escape ^||
bindkey "^[x" command
As a side effect C-| will be command prefix too. If you need this keys to be free too, then fix "escape ^||" accordingly.
Screen doesn't have any shorthand syntax for alt bindings, but you can give it the octal code directly. For instance on my machine, Alt-x has the hex code F8, or 370 octal, so putting
escape \370x
in my screenrc changed the escape code to alt-X
Tested and works with screen 4.00.03 on Linux.
You may have to change the escape, since I think this may depend on things like your language and codeset, etc: how I found out what my escape code was was to type
$ echo -n ^QM-x | perl -ne 'printf "%lo\n", ord($_)'
^Q is the quoted-insert command for readline (it inserts what you type directly without trying to interpret it) and M-x was a literal Alt-X.
Fellow emacs user here.
The best solution I've found is a ~/.screenrc file with the following:
# C-a :source .screenrc
escape ^gg
Live updated here: https://gist.github.com/1058111
See also: http://ubuntuforums.org/showthread.php?t=498675
Something I have had for years in my .screenrc:
escape ^Zz
which is now hardwired in muscle memory for me.
Somehow I ended up having to share a screen with someone else's config, and now I keep stopping processes all the time (bash ^Z)... Not funny...