Select the first x letters of each row - emacs

I have the following org doc
How could only select vertically the numer) part of each row

Emacs has a mean to manipulate arbitrary rectangles in the same way regions are manipulate.
You need first to select a rectangular region:
select a starting point with C-<Space>
go to the opposite point of the rectangular region
Then you can apply several 'rectangle' actions.
All these actions all prefixed with C-x r
Most useful are :
C-x r M-w copies a rectangular region and it is probably what you need
C-x r k Cut (kill)) a rectangular region
C-x r y will yank a copied rectangle at a given position.
Rectangle actions are available in every mode, but to facilitate rectangle manipulation, you can use C-x <Space>; It is a toggle that turns standard copy/cut/paste actions (M-w, C-w, C-y and so on), to apply on rectangles. You also have a visual feedback on the selected rectangle.
Rectangle mode is very handy to manipulate columns in a tabular display (for instance to permute two columns).
C-x r N (rectangle number lines) is also useful to insert an automatic numbering of lines and it may also solve your problem.

If you use evil, you can type C-v to enter visual mode and use j to select the row you want from top to down.

Related

emacs split window side by side independently

My question is whether it's possible (simply!) to split a buffer window into two side by side frames which are independent, so that for example in org mode expanding a headline in one frame does not affect that headline in the other frame? C-x 2 does this for a top/bottom split but C-x 3 splits the screen but the buffers are not independent. On my laptop two side by side frames would be much easier to work with than a top/bottom split.
C-x4c calls clone-indirect-buffer-other-window which I believe is the closest thing to what you're asking for.
That command doesn't enforce any particular window split, but Emacs may do what you want by default. If not, then it will do what you want if the frame is already split appropriately, such that the 'other' window is where you want the indirect clone to be.
i.e.:
C-x3
C-x4c
See the manual for more information:
C-hig (emacs)Indirect Buffers

Emacs - Preserve horizontal position when changing lines

Is there a way to make Emacs preserve my cursor's horizontal position, when going up and down between lines of varying widths in a file?
I'll explain what I mean by example. Say I have 3 lines of text:
1: ------a
2: --b
3: --c---d
Say the cursor is at the position marked above by a. If I move down a line, the cursor will be at b. If I move down again, it will be at c. But I actually want to end up at d.
Is there a way to make Emacs do that automatically, so that I can go from a to b to d just by pressing the down arrow twice? Emacs would have to remember my horizontal position from my most recent horizontal movement, and try to restore that horizontal position each time I move vertically.
Customize user option goal-column to nil.
(You can also use command set-goal-column anytime to set goal-column to the current column.)

How select a rectangle in emacs

In Notepad++, you can press Alt-LeftMouseClick-Drag to select a rectangular region.
How do I do this in emacs (windows) ?
I tried the methods in http://www.emacswiki.org/emacs/RectangleCommands but
my selection happens as a regular region instead of rectangular shape:
http://i.stack.imgur.com/tBMBN.png
I also tried enabling/disabling cua mode.
In Emacs 24.4 and greater, C-x SPC is bound to rectangle-mark-mode which selects a rectangle visually.
http://emacsredux.com/blog/2014/01/01/a-peek-at-emacs-24-dot-4-rectangular-selection/ describes this in greater detail.
One more fun one to add to the list.
If you're using evil-mode (ie, the Vim emulation layer), you can use evil-visual-block (default binding of C-v) to select a rectangle visually.
In Emacs, whether a command affects a continuous piece of text or a rectangle depends on the command, not on what the selection looks like.
I'm not aware of any way to make the selection appear as a rectangle on the screen (but see this answer), but if you use any of the rectangle commands listed in the page you linked to, such as C-x r k or C-x r t, you'll see that they operate on the rectangle defined by the beginning and the end of the selection, despite the fact that the selection looks like a regular region.
You use rectangle commands by placing the mark at one corner of the rectangle and point at the opposite corner. Even though the region will be displayed as normal, the rectangle commands will act on the rectangle delimited by point and mark. CUA mode doesn't affect this.
You just have to use your imagination :)
Enter rectangle mark mode with C-x SPC
Shrink or grow region by (This is going to select a rectangle):
2.1. Do C-n or C-p to expand the cursor to the next or previous lines respectively.
2.2. You can now play with text selection by doing C-f, C-b, M-f, M-b, etc.
This selected rectangle region can be used to perform some actions like
kill : C-x r k
delete: C-x r d
yank: C-x r y (paste last killed rectangle at cursor position)
If you want to see the rectangular selection, use CUA rectangles instead. Enable it with
(cua-selection-mode t)
Then use M-RET to start the selection, move the cursor to change it (you can also hit RET to move the cursor to different corners), C-? to get a list of commands to operate on the rectangle. M-RET with the selection active cancels the selection, as does C-g.
CUA selection mode does not enable the rest of CUA, so C-x, C-c, C-v, etc. will not change.
You can do the same thing with the mouse if you so wish. While not as specific as doing it via keyboard where you can select characters that don't exist following the end of the line for example it will fulfill most use cases.
(defun mouse-start-rectangle (start-event)
(interactive "e")
(deactivate-mark)
(mouse-set-point start-event)
(rectangle-mark-mode +1)
(let ((drag-event))
(track-mouse
(while (progn
(setq drag-event (read-event))
(mouse-movement-p drag-event))
(mouse-set-point drag-event)))))
(global-set-key (kbd "M-<down-mouse-1>") #'mouse-start-rectangle)
Found this here: https://emacs.stackexchange.com/a/7261
Most rectangle commands are accessed via the C-x r prefix (shared
with the "register" commands). See the
Rectangles manual section
here
for a description of all the special kill, yank, delete, number, replace,
etc. commands, which you'll need to do anything useful with your rectangles.
The only non-prefixed rectangle command is the initiator C-x SPC
(rectangle-mark-mode), but that's easy enough to remember, being a
lot like the very common C-SPC (set-mark-command).
It looks like in recent Emacs versions, the selected region is working
well to show exactly what's selected.
(I recommend
helm-descbinds or
guide-key to complete your
C-x r command prefixes, as well as any others.)
In addition to what others have mentioned, including about highlighting shown as a rectangle by rectangle-mark-mode and cua-rectangle-mark-mode, library Mode-Line Position (modeline-posn.el) provides the following feature for use with rectangle commands: It shows the length and width of the rectangle in the mode line, as part of size-indication-mode.
Normally, size-indication-mode shows only the buffer size and the current relative position in the buffer. With library modeline-posn.el what you see is different when the region is active:
For a rectangle command that reads input, you see the number of rectangle rows and columns (e.g. 21 rows, 16 cols)
Otherwise, the region size.
You can customize how this information appears (option modelinepos-style). By default, the active region size is shown as the number of characters and the number of lines in the region (e.g. 473 ch, 3 l).
The face used for the mode-line indication of an active region (including rectangle) is face region, so it looks the same as the region.
An additional feature draws your attention further to the mode-line region indication when a command that acts on the active region or changes its behavior when the region is active reads input. This just uses a different face, which by default is face region but with a red overline and underline.
Finally, Boolean option modelinepos-empty-region-flag determines whether an empty active region is indicated in the mode line, to attract your attention (you might not otherwise notice that you are acting on an empty region). E.g., you see (highlighted using face region): 0 ch, 0 l.

How do I rearrange a split pane in Emacs?

In Vim I can move a split around. For example, if my window was split in two horizontally, with the topmost split split vertically (3 splits in total) I could move the top-right split to the right to become a vertical split taking up the entire vertical space.
Is this kind of rearrangement possible?
Update: I know resizing is possible, I'm looking to move though. I get the feeling this is not supported by Emacs.
You may be interested by C-x + when you have more than 2 windows. It rearranges equally the windows on the frame. It's convenient for example when you do two C-x 2 in a row and want to have the windows to occupy the same space on the frame.
No, not by default. What you have to play with is basically C-x 0, C-x 1, etc. Look in the Emacs Wiki for extensions that may or may not do what you're looking for.
FWIW, if you are running within a GUI, then you can precisely re-arrange window sizes quickly and easily with the mouse. This isn't quite the same thing as you're asking for, but may be a handy alternative in some cases.
You can click on any non-'active' area of the mode line (such as the buffer name) and then drag it up or down.
Dragging side to side is more fiddly. You must click on the exact border between the two mode lines, and then you can drag left/right.
For your specific example, I don't believe that is supported. AFAIK you can only reorganise the window splits within their existing 'parent' window (the upper split in this example). To make the upper-right window fill the vertical space you would either remove the bottom window with C-x 0, or use C-x 1 to remove all other windows, and then re-split them in the desired manner.
(Tangentially, I've often thought a custom library to 'rotate' the window splits would be a nice thing to have.)
I believe that the window resize commands are built in to window.el, from emacswiki the functions you want documented are:
shrink-window-horizontally ; C-x {
enlarge-window-horizontally ; C-x }
enlarge-window ; C-x ^
shrink-window ; not bound on my system
The comments are what they are bound to on my system, but I don't know if I did that myself.
All of them take a prefix argument, the number of lines to enlarge/shrink. The last two default to vertical.
As far as I know you cannot create a new window that runs the length or width of the screenfrom a window already split in that direction. Buffers remain open if you close the windows though so you can remove windows and then split them in the configuration you want. Then change which buffer is displayed in the window you are standing in by pressing C-x left arrow or right arrow.
I should add that this answer is regarding "vanilla" emacs, there is probably a way of doing what the OP asks if you really want to. It's emacs after all.

Rectangle functions in emacs

I've read in several places that the rectangle functions in emacs are very useful. I've read a bit about them, and I can't quite figure why. I mean, when you want to kill a paragraph, you mark the first row/column and then the last one, and that's actually a rectangle, right? But you can still use the normal kill...
So what kind of transformations would you do with them?
If you have data in columns in a text file with M-x delete-rectangle or M-x kill-rectangle you can delete a single column of data. Similarly, M-x yank-rectangle will paste in a column of text.
For example, take the following text:
1. alligator alphorn
2. baboon bugle
3. crocodile cornet
4. dog didgeridoo
5. elephant euphonium
6. fish flugelhorn
7. gopher guitar
Select from the a of alligator to the g of guitar. The beginning and end of the selection mark out two opposite corners of the rectangle. Enter M-x kill-rectangle and you immediately have:
1. alphorn
2. bugle
3. cornet
4. didgeridoo
5. euphonium
6. flugelhorn
7. guitar
Next put the mark at the end of the top line, add a few spaces if required and enter M-x yank-rectangle and ta-da! You have re-ordered the columns:
1. alphorn alligator
2. bugle baboon
3. cornet crocodile
4. didgeridoo dog
5. euphonium elephant
6. flugelhorn fish
7. guitar gopher
I like to use rectangle for 2 main purposes, inserting the same text on every line, or killing a column of text (similar to Dave Webb's answer).
There are 2 useful shortcuts for these, C-x r k will kill a rectangle, and C-x r t to insert (there are other rectangle commands with a C-x r prefix, but these are the ones I use).
So let's say you want to take some code and format it so that you can post it in a Stack Overflow post... you need to prefix with 4 spaces. So, go to the beginning of the first line, C-SPC to mark, then go to the beginning of the last line and C-x r t <SPC> <SPC> <SPC> <SPC> <RET>, and there you have it! Then you can just copy and paste it into Stack Overflow. I have run into more complex situations where this is useful, where you actually have text you want to insert on every line at a particular place.
So the other situation like Dave Webb's situation, if you want to kill a rectangle, use C-x r k though, because it's just a lot quicker ;-)
Also, according to my reference card that I printed out when I first started, you can do the following:
C-x r r: copy to a register
C-x r y: yank a rectangle
C-x r o: open a rectangle, shifting text right (whatever that means...)
C-x r c: blank out a rectangle (I assume that means replace it with spaces, but you'd have to try it out to see)
C-x r t: prefix with text (as described above)
C-x r k: killing (as described above)
In emacs24+ there's also function for numbering lines:
(rectangle-number-lines START END START-AT &optional FORMAT)
Insert numbers in front of the region-rectangle.
START-AT, if non-nil, should be a number from which to begin
counting. FORMAT, if non-nil, should be a format string to pass
to `format' along with the line count. When called interactively
with a prefix argument, prompt for START-AT and FORMAT.
It is binded to C-x r N by default.