I have a large text file. Each sentence is on a separate line and I ant to join them all together in emacs. DOes anyone know how to do this. Somebody has told me I can use M-^ but this only joins one line to the one above one at a time and I have hundreds of lines. Help!
You can replace newlines with an empty string. Move the cursor to the beginning of your file and execute this sequence
M-x replace-string C-q C-j RET RET
You can replace with query replace, pasting the newline.
mark/copy a newline
start at beginning of buffer
Alt-%
paste the newline and do enter. (for me the buffer says Query replace ^J with: )
type space + enter
type ! for accept all or y/n for accept/reject per match
Related
I'm trying to make a macro that does some replacements and in the end, deletes the first and the last lines in the document, but I've hit a dead end. Any advice?
A single regular expression replace can do this.
To delete the first and last lines: Select Dot matches newline, select Wrap around and set the Find what to be
\`[^\r\n]*[\r\n]+(.*[\r\n]+)[^\r\n]+[\r\n]*\'
Set the replace with to be
\1
My testing has sometimes required Find next to be pressed twice. The first find, for some unknown reason, only matches from somewhere in the middle of the buffer to the end.
The regular expression is interpreted as follows:
\` Start of buffer
[^\r\n]* First line, zero or more characters
[\r\n]+ Line separator
(.*[\r\n]+) Central part of buffer
[^\r\n]+ Line separator before the last line
[\r\n]* Optional line separators at end of buffer
\' End of buffer
The question does not state how to handle the second line of the file being empty. The above with [\r\n]+ will delete any blank lines after the first line. Similarly, the handing of empty lines near end of buffer may not be exactly as wanted. Changing both [\r\n]+ to \R might be a stricter interpretation of what is wanted.
For the general case of modifying the first and last lines set the Find what to be:
\`([^\r\n]*)([\r\n]+.*[\r\n]+)([^\r\n]+)([\r\n]*)\'
Set the replace with to be
First \1 First\r\n\r\n\2\r\n\r\nLast \3 Last\4
Tested with Notepad++ version 6.7.8.2
While the macro is recording press these keys...
Ctrl+Home
Shift+Down
Backspace
Ctrl+End
Shift+Home
Backspace
Backspace
That will delete the first and last lines of the document.
The commands M-a (backward-sentence) and M-e (forward-sentence) move to the beginning and end of the current sentence, respectively. I would like to end a sentence if there is a newline. So, I would like emacs to treat the following text as two lines (even though there are no periods). With the default config, emacs treats it as a single sentence.
This is line one
This is line two
Why do I need this?
I use visual-line-mode. So my sentences never contain a newline character. I am using the sentence hilight mode which relies on emacs "sentence end". This creates problem in cases where there are no periods. For example program source listing. (The whole program is treated as a single line by emacs.)
Adding \n to the end of the regexp for sentence-end should accomplish the goal you seek -- it is done by adding a delimiter that looks like this \\| and then the \n.
Here is the default regexp for sentence-highlight-mode:
(setq sentence-end "[^.].[.?!]+\\([]\"')}]*\\|<[^>]+>\\)\\($\\| $\\|\t\\| \\)[ \t\n]*")
Here is the revised regexp for sentence-highlight-mode:
(setq sentence-end "[^.].[.?!]+\\([]\"')}]*\\|<[^>]+>\\)\\($\\| $\\|\t\\| \\)[ \t\n]*\\|\n")
How can I write a "," (comma) character in the SLIME emacs window? The , key brings up the SLIME command prompt automatically.
Thanks, a Lisp beginner
, only triggers REPL shortcut selection when input at the beginning of a line. In all other cases, you can input a comma by typing ,.
In the case of Common Lisp, since as long as you don't modify the reader, , can only occur within a quasi-quoted expression, this should not be a significant restriction.
If it really is a problem, refer to Deokhwan Kim's answer.
You can insert , by C-q, (Control-q and then comma). C-q is bound to quoted-insert, which can be generally used whenever you want Emacs to read a next input character and insert it instead of invoking a command bound to the input character.
With emacs/org-mode, I use [[link][display]] format to show and link whatever necessary. The problem is that sometimes, the automatic formatter kicks in, and break the line to have an ugly link as follows.
The solution can be (1) remove the leading blanks of the second line, and (2) join the first and second string.
How can I do that with emacs or org-mode? If org-mode has a solution to this problem, it would be better, as I don't need to delete [ or ] to make it editable.
Your first solution is available as delete-indentation:
M-^ runs the command delete-indentation, which is an interactive compiled Lisp function in `simple.el'.
It is bound to M-^.
(delete-indentation &optional ARG)
Join this line to previous and fix up whitespace at join.
If there is a fill prefix, delete it from the beginning of this line.
With argument, join this line to following line.
With point on the second line, M-^ will remove the leading whitespace on the second line, the trailing whitespace on the first line, and join the two lines together.
I have 2 columns, separated by comma. How can I swap those columns with Emacs?
I have the following:
column 1,column2
x1,x2
y1,y2
f1,f2
and I want it like this:
column2,column 1
x2,x1
y2,y1
f2,f1
Use M-x query-replace-regexp and then:
\(.+\),\(.+\)
as replace regexp and
\2,\1
for replacement.
In Emacs, you need to escape grouping parentheses with \. So, above regexp would be usually written as
(.+),(.+)
which means that you want everything before comma in first group and everything after comma in second group.
\2,\1
means: write second group, then comma, then first group.
While you can apply techniques given by other people, you can also use the org-mode tables.
Once you convert the data into org-mode table, it is very easy to swap the columns by simple keystrokes. You can have M-x org-mode, select the region then do M-x org-table-convert-region, and then M- on the right most column. I am not sure, how to export the data as CSV, but that should be very easy for you with replace-regexp. This can be helpful: http://www.gnu.org/software/emacs/manual/html_node/org/Tables.html#Tables
Similar to the answer given by #darioo, type the following into the top of your buffer:
(query-replace-regexp "\\(.*?\\),\\(.*\\)" "\\2,\\1")
Then, put your cursor at the end of this line and press ctrl-x, ctrl-e.
You will have an interactive search-and-replace for which you press the space bar to make the change, and press ctrl-g to quit. If you press ! (exclamation mark) then the search will cease being interactive and take place on all matching text.
If you want to reverse the changes then press M-x (usually ESC followed by x) and type undo and press enter.
Emacs has a rectangular selection mode, see for example: http://emacs-fu.blogspot.com/2008/12/working-with-rectangular-selections.html
Even better, if you enable cua-mode, entering Ctrl-Enter will put you in rectangle selection mode that is very easy to use.
http://trey-jackson.blogspot.com/2008/10/emacs-tip-26-cua-mode-specifically.html
Use a macro !
Go to the first line of the buffer
Start recording a macro (F3)
Move to the beginning of the line (^a)
Search for comma (^s ,)
Transpose (M-t)
Move cursor down one line
Stop recording macro (F4)
Select the rest of the lines and:
M-x apply-macro-to-region-lines
UPDATE: This doesn't work properly if you have multiple words in a column. Looking for a more general solution...