Text processing to concatenate lines (ie remove end of line char) - emacs

I have a list of lines like this:
a+
b+
c+
d+
e+
f+
... you get the idea...
I want to end up with a+b+c+d+e etc
I was trying with emacs but couldn't work out how to do such a thing. anyone any ideas?
One thing that does work is
c-m-% [paste in selected after + on one line to beginning of next row] [nothing]
There must be something to insert for carriage return?

How about simply replacing EOLs by nothing?
M-%C-q C-jRETRET
Explanation:
M-% : query-replace
C-q : quote the following character
C-j : end-of-line character
first RET : validate the search string
second RET : validate the (empty) replacement string

Do you have a buffer with those lines in it? In that case, you could create a simple macro:
F3 ;; record macro
C-e ;; end of line
C-d ;; delete newline
F4 ;; save macro
Then either press F4 repeatedly until you're done, or do C-0 F4 to do it all in one swoop.

Have you tried just `M-q' ? The spacing is different, and it will use several lines if you have many of those thingies, but otherwise, it seems like a funny alternative.

M-x
replace-regexp
RET
C-q C-j
RET
RET

Related

Emacs mode/method for logic symbol placement in text?

I would like to put actual logic symbols into my emacs buffers, e.g., the logic symbol "∀" or "∃" or "⇒", directly into my (fundamental) text or .org or whatever buffer. I found xmsi-math-symbols-input.el at ErgoEmacs, but I'm wondering if this is "best practice." Maybe the best practice is to just right Tex/Latex copy, especially if I'm doing org-mode?
You can just use the corresponding Unicode characters normally in Emacs. Bind any that you want to any keys you want. For example:
(global-set-key [f2] "∀")
(global-set-key [f3] "∃")
(global-set-key [f4] "⇒")
To get the string with the char, you can use C-x 8 RET and type the name or code point of the Unicode char. In other words, C-x 8 RET lets you insert any Unicode character.
For example, the Unicode code point for ∀ is 2200. C-x 8 RET 2200 RET inserts a ∀ character.
And the Unicode name of ∀ is FOR ALL. C-x 8 RET for all RET also inserts a ∀ character.
The reason you might want to bind a particular character to a key is for convenience - C-x 8 RET is very general, and generally slow.
At least in org-mode, it is possible to place special symbols in an .org buffer just as their raw latex markup, e.g.:
\forall
becomes the UTF-8
∀
when you do C-c C-x \
... but this isn't a general solution.
The way I do it is with the TeX input method. I.e. use C-u C-\ TeX RET after which typing \forall will insert the ∀ char.
[ Well, in reality , I have TeX set as my default coding-system, so I really only need to hit C-\ to enable this input method. ]

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.

Emacs - delete to first non blank character

I'm looking for key binding / function which delete from current cursor position to first non blank character
example:
function f() {|
test();
| - cursor position
I want delete everything to "t" letter
M-z t t (substitute t as needed :).
Lots of good options have been presented. In Emacs 24 (currently in pretest), you can specify M-- to just-one-space or M-SPC to do exactly what you ask.
In 'c-mode' there is 'M-x c-hungry-delete-forward', which is also bound to C-c C-d. So, you can create that binding in whatever programming mode you're using.
(define-key <whatever>-mode-map (kbd "C-c C-d" 'c-hungry-delete-forward)
Alternatively, you can grab the package 'hungry-delete' and use that to override the deletion commands to delete all the whitespace (as opposed to a single space).
To do this, I use a custom macro :
(fset 'jline
[?\C- ?\C-\M-n ?\C-a ?\C-w return])
(just put this small macro in your .emacs config)
Of course, you can rename the macro like you want.
So, the following state :
function f() {|
test();
}
Becomes :
function f() {
|test();
}
Use M-x delete-blank-lines (which is also bound to C-x C-o).
From the help-docs (C-h k C-x C-o):
On blank line, delete all surrounding blank lines, leaving just one.
On isolated blank line, delete that one.
On nonblank line, delete any immediately following blank lines.

Running a macro till the end of text file in Emacs

I have a text file with some sample content as shown here:
Sno = 1p
Sno = 2p
Sno = 3p
What i want is to remove the p from each of the columns.
With this intention i write a macro:
M-x //go to buffer
C-x (//start the macro
C-s = // search for equalto sign
RET C-f C-f // reach to te alphabet 'p'
DEL // Delete
C-n C-x )//go to new line and Close the macro definition
C-x e
Pressing e twice will remove p, but in case i want to do the same stuff till the end of file, how can i do it i can't keep pressing e if i have 20000 such lines. What should be done??
Please donot suggest regex, as this is a sample example, not the actual case.
Please donot suggest any elisp, i am comfortable with remembering shortcutf for emacs.
M-0 C-x e to repeat last macro until an error happens (after the final line, either C-s = or C-n will be an error).
You may hear an annoying beep.
You can use the "apply-macro-to-region-lines" function to apply the last defined keyboard macro to all lines in a region. So, the steps you follow are:
Define your keyboard macro (which you had already done but I've added another C-f to the 3rd line):
C-x (
C-s =
RET C-f C-f C-f
DEL
C-n C-x )
Select the whole buffer with the "mark-whole-buffer" command (by default, it's bound to C-x h). Alternatively, if you just want to change a certain number of lines, just select the lines you want changed.
Run the "apply-macro-to-region-lines" function (by default, it's bound to C-x C-k r).
All the p's will be removed.
I usually give a name to the macro after defining it with M-x name-last-kbd-macro, so I can call it with M-x conveniently. And then I call it with a numeric prefix. E.g.
M-1000 M-x macroname
The files I work on usually don't have 1000 places where the macro can act, so 1000 is large enough and it will stop automatically at the end of the file. You can use, say, 1000000 as a prefix if you have larger files, and if it's still not enough then you can call it again with 1000000 as prefix.
you may try: M-20000 C-x e so as to do the stuff for 20000 times, after you define the macro.

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.