Deleting the first 'x' characters from every line in a region with Emacs - emacs

I want to delete the first x characters for every line in a region.
Is there any key binding available to do this without using regex?

The best way to do this is to use the "rectangle" family of commands. For example, mark the beginning of the region. Go to the end of the region and place the point at column X. Run the command kill-rectangle using C-x r k.
Of course, this is not limited to deleting characters at the beginning of lines.

If the mark is on column 0, put the point on column x and use kill-rectange:
C-x r k runs the command kill-rectangle, which is an interactive
autoloaded Lisp function in `rect.el'.
It is bound to C-x r k.
(kill-rectangle START END &optional FILL)
Delete the region-rectangle and save it as the last killed one.
When called from a program the rectangle's corners are START and END.
You might prefer to use `delete-extract-rectangle' from a program.

One command that I really love for these types of jobs is multiple cursor's edit lines:
http://www.youtube.com/watch?v=jNa3axo40qM
It is overkill compared to kill-rectangle (the best solution to the original problem) but it is an amazing tool in the toolbox. Definitely worth taking a look at it.

Select the required rectangle using rectangle command
M-x rectangle-mark-mode
Then use command
M-x kill-region

Related

How to view and edit only a small part of a text file in Emacs

I would like to accomplish the following behaviour in Emacs and was wondering if there's an existing package that already does what I want.
I want my buffer to display only lines 30 to 60 in file myfile.txt, and simply hide the rest of the file from me.
When I scroll up to the top, I should be at line 30.
When I scroll down to the bottom, I should be at line 60.
If I edit this region, these edits should be reflected in the original file.
The region should grow/shrink as I insert/delete lines.
I should be able to open multiple regions to the same file in different buffers.
This is very close to my ideal workflow. Thanks very much for your help!
The Narrowing,you can Narrow down to between point and mark with C-x n n,then what you edit will be restricted in this region,after that you just Widen to make the entire buffer accessible again C-x n w.Perhaps this is what you are looking for
Try library Narrow Indirect (narrow-indirect.el)
It provides simple commands to create an indirect buffer that is a clone of the current buffer and that is narrowed to a particular portion of it. By default, it helps you distinguish such indirect buffers from non-indirect buffers, by using a buffer-name prefix (default I-) and by using a different face for the buffer name in the mode line.
To complement the other answers, if you are working on an org file specifically, you also have the following commands :
C-x n s (org-narrow-to-subtree) : Narrow buffer to current subtree.
C-x n w (widen) : Widen buffer to remove narrowing.
And if you are using blocks (‘#+BEGIN’ … ‘#+END) :
C-x n b (org-narrow-to-block) : Narrow buffer to current block.

How to format clojure in emacs [duplicate]

I'm new to Clojure and new to Emacs.
Is there an Emacs short-cut to intelligently re-indent the whole file?
if not, is there at least a way to indent selected regions left or right?
I feel like I'm back in the stone age repeatedly pressing the arrows
C-x h selects the entire buffer. C-M-\ reindents the selected region.
Ctrl-x, h (select all) followed by Tab (to indent)
cider-format-buffer command (Since cider 0.9.0)
When you capture data from a sequence like C-u C-c C-e
(cider-eval-last-sexp), the raw data output to your buffer can be
unwieldy to inspect/work with. And the normal code-indenting commands
(mentioned in answers here) don't handle it well.
For handling results from such evaluated expressions, try
cider-format-edn-region.
As a concrete example, have you ever tried reformatting your
~/.lein/profiles.clj? This is pretty hard to do and keep
consistent, until you discover cider-format-edn-region. Take
caution that it will, however, remove any comments.
Use cljfmt for many configurable ways to reformat/reindent. It has an Emacs plugin, but also can be run via lein.

Is there a way to make regions in term modes respect line wrapping?

When using a term mode derivative (like ansi-term or multi-term), I often want to select a region and copy it someplace else. If that region includes a line which wraps at the edge of the terminal window, pasting that region in another buffer always inserts a hard newline at the place where term wrapped the line. This means I often have to go back and clean up pasted text. Is there a way to avoid doing this? I tried both term-line-mode and term-char-mode; both do the same thing.
I do not want to write a yank hook which strips out all newlines, since I want to preserve existing hard newlines in the original content.
This works for me:
(setq term-suppress-hard-newline t)

emacs + vimpulse-visual-mode + "linewise" text selection

Is it possible to make visual-line-mode (one after pressing V from normal mode) conduct as if first mark was in the beginning of the first line of selection and second mark - end of the last line?
For an example, currently after V, j and M-x comment-dwim:
here<cursor>is a
simple example
becomes
here;; is a
;; simp
le example
whereas desired result is often:
;; here is a
;; simple example
Of course, one can write a wrapper for comment-dwim, but I suspect/hope that there is a more correct solution.
Thank you in advance.
Doesn't V (vimpulse-visual-toggle-line) already do that?
Linewise selection will select whole lines. (I use this all the time)
The behaviour you're talking about will occur if you're using v (vimpulse-visual-toggle-char).
comment-dwim calls comment-or-uncomment-region to perform the actual commenting on the marked region. There is no option to extend the region to beginning of the first line and/or end of the last line. You will have to write a wrapper or advice comment-or-uncomment-region to achieve the effect you want.

Swapping 2 columns with Emacs

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...