CR/LF in Emacs? - emacs

How can I detect CR and/or LF in Emacs? Since I want to i-search CRLF, I prefer simpler way (e.g. no or less regex). Only relevant web page I've found is this but c-q c-j didn't work.
Thanks!
Environment) GNU Emacs 22.3.1 (i386-apple-darwin9.8.0, Carbon Version 1.6.0), but I also want to know how to do this on linux (Ubuntu).

C-s to start i-search, then C-q C-j should work. Control-Q quotes the next character as a literal, and control-J is a literal linefeed ("LF" or newline).
If you're dealing with a file that has carriage returns (CRLF line endings), Emacs will automatically use a DOS buffer file coding system and convert them to just linefeeds when loaded, and reconvert them to CRLFs when saved. If you really want carriage returns in your buffer, run M-x revert-buffer-with-coding-system unix. Then carriage returns will be displayed in your buffer as ^M. In this case, you can search for them with C-s to start i-search, then C-q C-j C-q C-m.

Related

How to replace a character with a newline in Spacemacs?

I tried to follow this to replace a character with newline in Spacemacs but that key combination replaced it with a space instead of newline.
How to replace a character with a newline in Emacs?
How can this be accomplished in Spacemacs?
It is supposed to work exactly the same with Spacemacs (C-q C-j will replace with a newline).
You can check if your keys are correctly mapped with the describe-key command (C-h k):
C-q runs the command quoted-insert
and
C-j runs the command electric-newline-and-maybe-indent (result with my Spacemacs)

Does the C-m key combination in emacs generate "\r\n" or just "\n"

The emacs docs for C-m key (ie. C-h k C-m) displays the following
newline is an interactive compiled Lisp function in `simple.el'.
(newline &optional ARG)
Insert a newline, and move to left margin of the new line if it's blank.
If `use-hard-newlines' is non-nil, the newline is marked with the
text-property `hard'.
With ARG, insert that many newlines.
Call `auto-fill-function' if the current column number is greater
than the value of `fill-column' and ARG is nil.
But it does not say newline is in the context of dos ("\r\n") or unix ("\n").
Internally, Emacs stores a Unix newline, i.e. "\n", in its buffers, similar to what most C programs do when reading text files. If there is a "\r" in an Emacs buffer, it will be displayed as ^M.
On the other hand, Emacs remembers the newline convention used for each opened file. If you open a file with "\r\n" line breaks, modify it, and save it, then Emacs will convert all newlines back to "\r\n".

Creating new buffer with space in buffer name under Emacs 24.3

I'm using GNU Emacs 24.3 under Windows. At work I have to conform to specific file-naming standards, some of which include spaces in the file name.
How do I create a file with a space in the file name in Emacs? I can easily open them for editing, but can't find any info on how to create one. (i.e., create the file C:\temp\foo bar.txt)?
Update
In response to the question in the comments under the accepted answer: I'm using ido-mode, and this is the cause of the specific issue I was experiencing at the time.
I don't have the Windows version to test, but with the Linux version, there is no particular issue. I can open a file with spaces in it and save it with these two commands:
C-x C-f ~/tmp/this is a test.txt
C-x C-s
Note that C-x C-f does not require an existing file. When you do C-x C-s, it automatically creates it.
I hope for you that this is the same behaviour with the Windows version...
In Emacs you create a file the same way you open it for editing: C-x C-f c:/temp/foo bar.txt (no need for backslashes in Emacs).
And in Emacs 24.3 you can just type spaces like any other file-name chars. (In old Emacs versions you needed to quote SPC chars by preceding them with C-q: C-q SPC inserts a space char.)
(Actually, that just creates a new buffer for the new file (the buffer "visits" the file). The file itself is created when you save the buffer: C-x C-s.)

Insert complete lines with Emacs + Evil

In Vim, I often move lines by deleting them (either with dd or visual line mode), moving my cursor to the new position, then p to put them in:
first
second
third
And if my cursor is on the line second, I can use ddp to move it down:
first
third
second
But with Emacs + Evil mode, putting the line back doesn't work as expected: if, for example, my cursor is on the i in third when I hit p, I end up with:
first
thisecondrd
How can I make Emacs + Evil mode insert new lines when putting entire yanked lines?
I use C-a to go to the beginning of the line (^ in evil-mode, probably) before yanking, if I want that behaviour. If you do this often, you can probably come up with your own thing for yank, although you have to figure out during the kill part if you're doing that. (Or you can check if the yanked thing has newlines, I guess?)
There's a transpose-lines command, by the way (C-x C-t in regular Emacs binding - someone suggested binding this to xtl - https://github.com/syl20bnr/spacemacs/blob/master/my-keybindings.el).
If I find my cursor on a line that I want to move, my natural response is to first delete the line into the kill ring with either C-a C-k C-k or C-a C-space C-n C-w (either of which can also grab several-line sequences by duplicating either the C-k or C-n or prefixing the C-n with a numeric argument) and then travel to the beginning of the line where I want to paste and doing a C-y yank.
Note that Emacs considers a file to be a steam of characters, in which newline or carriage return is not special. Unlike in vi, you can C-f forward right over a newline exactly as though it is a normal character; backspace over it; or include it in a deleted and yanked buffer. It is exactly like any other character. Perhaps Emacs is for people who think of files as sequences of characters — some of which happen to be newlines — and vi is for people who think of their file as lines, that are magically separated by who-knows-what but it certainly is not like any other character.
If the main use case you are trying to address is moving lines up or down (as opposed to the more general question of how to "make Emacs + Evil mode insert new lines when putting entire yanked lines"), I suggest you try out move-text.
It is a very small add-on package that provides two commands (move-text-up and move-text-down) for moving lines up and down, respectively. You can be anywhere on a line and call these; there is no need to kill or yank anything, and they work for regions as well.
For example, calling move-line-down in this situation (point right after second):
first line
second| line
third line
will produce
first line
third line
second| line
As you would expect, moving the current line (or region) up or down n lines works by calling the appropriate command with a numeric prefix.
The commands are bound to M-up and M-down by default but you should be able to rebind them to key sequences of your liking via
(define-key evil-normal-state-map "mu" 'move-line-up)
(define-key evil-normal-state-map "md" 'move-line-down)
move-text is package-installable from MELPA.

How can I replace a character with a newline in Emacs?

I am trying to replace a character - say ; - with a new line using replace-string and/or replace-regexp in Emacs.
I have tried the following commands:
M-x replace-string RET ; RET \n
This will replace ; with two characters: \n.
M-x replace-regex RET ; RET \n
This results in the following error (shown in the minibuffer):
Invalid use of `' in replacement text.
What's wrong with using replace-string for this task? Is there another way to do it?
M-x replace-string RET ; RET C-q C-j.
C-q for quoted-insert,
C-j is a newline.
There are four ways I've found to put a newline into the minibuffer.
C-o
C-q C-j
C-q 12 (12 is the octal value of newline)
C-x o to the main window, kill a newline with C-k, then C-x o back to the minibuffer, yank it with C-y
Don't forget that you can always cut and paste into the minibuffer.
So you can just copy a newline character (or any string) from your buffer, then yank it when prompted for the replacement text.
More explicitly:
To replace the semicolon character (;) with a newline, follow these exact steps.
locate the cursor at the upper left of buffer the containing text you want to change
Type m-x replace-string and hit Return
The mini-buffer will display something like this:
Replace string (default ^ -> ):
Type in the character you want to replace. In this case, ;
and hit Return
The mini-buffer will display something like this:
string ; with:
Now execute C-q C-j
All instances of semicolon will be replaced a newline (from the cursor location to the end of the buffer will now appear)
There is a bit more to it than the original explanation says.
Switch to text mode:
M-x text-mode
Highlight the block to indent.
Indent: Ctrl + M </kbd>
Switch back to whatever mode...
Inline just:
C-M-S-% (if the binding keys are still the default) and then
replace-string ^J.