Emacs scrollbars with exact positioning - emacs

How can I get scrollbars with exact positioning?
To illustrate what I mean:
C-h C-f (Read the The GNU Emacs FAQ)
Now try to position the buffer content such that the paragraph starting with This is the version ... is on top. With the default scrollbar you need several redraws to do this. Or you left-click for cursor positioning then C-lC-l
Now go back to where you have been before. This is impossible with regular scrollbars.
What I would like to have is to just click in the scrollbar at the height of This is the version ... to position this line on top for step 2. And to go back where I came from, I rightclick (without having moved the mouse, indeed).
In the past, I have always installed Emacs with the Athena widgets-like option to get such scrollbars, see a description of them. But I wonder if there is now a better or more modern way to do this. After all, I'd rather like to use standard distributions.
Maybe, it is not even necessary to change the scrollbars at all but to use rather the left-fringe for it. After all, mouse clicking in the fringe is recognized as <left-fringe>
M-x emacs-version
GNU Emacs 23.3.1 (x86_64-pc-linux-gnu, GTK+ Version 2.24.10) of 2012-09-21 on batsu, modified by Debian

Maybe a change of usage habits could help? Some Emacs user recommend disabling the scrollbars completely and navigate Emacs exclusively with the keyboard. (This habit is also beneficial for using Emacs inside a shell.)
Emacs provides a large number of navigation shortcuts. For instance, for jumping up and down large amounts of buffer space, you have:
C-v scroll-up
M-v scroll-down
M-} forward-paragraph
M-{ backward-paragaph
C-x ] forward-page
C-x [ backward-page
However, I would argue that by far the most useful navigation commands are:
C-s isearch-forward
C-r isearch-backward
In Emacs, search is so cheap, it is such an integral part of the work flow that it's often times much faster to get to a different point in the buffer by using a reference word as an anchor for a quick search, rather than leaving the home row to reach for the mouse. Hitting C-s or C-r repeatedly will move the point to consecutive matches. (You can even switch to regular expression search by hitting M-r while searching to make search even more powerful.)
You already mentioned C-l C-l to view the current line at the top of the buffer.
Now, if you want to go back to the previous location, you could use
C-u C-x
which jumps back to the mark. All the above commands for navigation push their start position on a "mark-ring" which means you can not only jump back one position, repeatedly hitting C-u C-x will take you back to many previous positions.

For this task I press C-l once, twice, three times.They will switch between middle-top-bottom.

Related

Emacs multiple-cursor - Cut and yank in multiple lines

Using the 'multiple-cursor' emacs package,
when I mark a word and cut ('C-w') in different multiple cursors, the only word in the 'active' cursor I control is cut.
I want to select different words in multiple positions and yank it to other relative position.
When I first tried this I remember some yes or no option at the screen bottom, but I couldn't really notice this at first and some keystroke made it to "no". I guess this might have been an option for multiple-cut. But I don't know how to revert this choice.
How do you do multiple cut and yank in emacs?
This works by default, so at some point you have indeed asked the library not do do this.
These settings are stored (by default) in ~/.emacs.d/.mc-lists.el
Just edit that file appropriately, and then M-x eval-buffer RET to make those same changes in the running instance.

what is called ring in emacs?

Unlike windows style self explanatory copy/cut/paste commands, I could not understand ring concept in emacs.
Since I don't program very often in emacs, I could have not realized the value of ring feature. Can you tell me what is called ring in emacs and how to use it?
Well, let me try in simple words. Each time you copy (M-w) or cut (C-w), the selection is inserted into a top of so called ring (which is just like a closed ended list).
The interesting part comes after. Now, if you paste with C-y then the most recently pasted region (which is now the front element of the ring) is inserted in the buffer. If you continue pressing M-y, then the text is replaced successively with older and older elements from the ring, so to say, the ring is rotated and the older text is designated as front element each time. This gives you an access to older cut/copied text.
The useful part does not end here. If you cut/copy some other text, it will be inserted at the top of the ring, and the ring will be rotated again such that the top is now the front. And you can start the C-y M-y sequence again, with the newly inserted text designated as front.
So, to conclude:
the top of the ring is the place where the newly copied/cut (M-w/C-w) text is inserted. When that happens, the top element becomes the front element.
the front element of the ring is the place on which paste (aka yank) commands C-y and M-y operate, by inserting it into the buffer. M-y also rotates the ring, such that older text in the ring becomes the front.
You can visualize it by imagining the circle with the front fixed at 12 hours position, but not part of the ring. The top is part of the ring instead, so when some command rotates the ring the top is also rotated.
Every time you copy or cut something to the clipboard in windows, you lose whatever was on your clipboard before. (Though some programs will store previous clipboard contents for you) The emacs "ring" will store old clipboard contents even after you copy/yank/cut/kill things. It can be handy if you get used to it because it lets you store more than one thing in the clipboard at once, and reduces the chances of accidentally overwriting something that you cut to the clipboard like you could in Windows.
A ring is a circular buffer. Think of a rolodex.
If you are killing/yanking to a ring, then you can walk forward and backwards in your history.
On Ubuntu, with the emacs-goodies-el package installed:
Press C-( M-x browse-kill-ring RET C-)
This defines a temporary keyboard macro which calls browse-kill-ring
when you press C-x e.
Now you don't have to just read about the kill ring, you can actually see what's in it.
Experiment with C-w (cut), M-w (copy), C-y (yank) and C-y M-y (yank next in ring), and press C-x e to see the effect on the kill ring.
From the Emacs manual: "A ring is a fixed-size data structure that supports insertion, deletion, rotation, and modulo-indexed reference and traversal." In other words, it's a circular queue.

Emacs remember text selection

I decided that I was ready to try something new, after a few years of using gEdit for most of my coding needs, and try to learn using Emacs. I knew this would be difficult, as I have heard how complex Emacs can be, but I was lured by its power. The hardest thing has been getting used to writing ELisp in the .emacs file to change things about the editor. I can't currently do it myself, but I have found a few helpful snippets here and there to change some options.
One thing I have been having a lot of problems with is getting Emacs to remember the text I have selected after a command. For instance, I commonly highlight a section of code to mass indent it. However, if I do this in Emacs, it will move the selected text only once before unselecting all of the text. Does anyone know a way around this?
Anyway, I apologize for what seems to me to be an easy question, but after an hour of Google searching and looking around here on SO, I thought it was worth asking. I have a few more questions about Emacs, but I will save them and ask separately after I get this straightened out. Thanks!
UPDATE
A few people have asked about what mod I am using and what type of text I am entering. While I don't know much about Emacs modes, I am editing a pure text file at the moment. Something like this:
Hello, I am a simple text file
that is made up of three separate
lines.
If I highlight all three lines and hit TAB, I get this:
Hello, I am a simple text file
that is made up of three separate
lines.
This is great, however, if I use C-x C-x like some suggest below to reselect the text and hit TAB again, I get this:
Hello, I am a simple text file
that is made up of three separate
lines.
I hope this helps!
FWIW, here is the reason for the behaviour of your newly-added example. (I'm not 'solving' the issue here, but I'm posting it to demystify what you're seeing.)
This was determined with emacs -q which disables my customisations, so the following is default behaviour for emacs 23.2.
You are in text-mode. You should see (Text) or similar in the mode line at the bottom of the screen, and C-h m will tell you (under the list of minor modes) "Text mode: Major mode for editing text written for humans to read." Emacs decides (by way of the auto-mode-alist variable) that it should switch to text-mode if you visit a filename matching certain extensions (such as .txt).
In text-mode pressing TAB with a region highlighted causes indent-according-to-mode to be called on each line of the region in sequence. The slightly convoluted path to finding this out starts at C-h k TAB, which tells us that TAB is bound to indent-for-tab-command, which in this instance calls indent-region -- that function name is not stated explicitly in the help, but can be seen in the code -- which checks the buffer-local indent-region-function variable, which is nil, and: "A value of nil means really run indent-according-to-mode on each line."
indent-according-to-mode checks the indent-line-function variable, which has the buffer-local value indent-relative.
Use C-h f indent-relative RET to see the help for this function. (Read this).
Although you probably won't yet have had the experience to know how to check all that (or necessarily even want to!), and fully understand everything it tells you, this is an example of how the self-documenting aspect of Emacs enables a user to figure out what is going on (which then makes it feasible to change things). I essentially just used C-h k (describe-key), C-h f (describe-function), and C-h v (describe-variable) to follow the documentation. Looking at the source code for indent-for-tab-command was as simple as clicking the file name shown as part of its help page.
I suggest doing the following to help see what is happening when indent-relative runs on each line:
M-x set-variable x-stretch-cursor t
M-x set-variable ruler-mode-show-tab-stops t
M-x ruler-mode
Now for each line in turn, put the cursor at the very start of the line and press TAB. You'll end up with all three lines indented to the first tab-stop ('T' in the ruler).
Now repeat this -- again, ensure you are at the very start of each line, in front of the existing indentation.
The first character of the first line (which is currently a tab) is once again indented to the first tab-stop, as there is no preceding line for it to examine.
Next, the first character of the second line is indented to match the position of the first non-white-space character of the preceding line. Because the first character of the second line is also a tab, the actual text of the second line is pushed one tab further along.
The third line follows suit. Its first tab character is lined up with the first non-white-space character of the second line, with the same relative effect as before, giving you the final state in your example.
To emphasise, note what happens if you now put enter the line "a b c" above the existing lines, then move back to the start of the next line (what was previously the first line) and press TAB. The first tab character will now be indented in line with the 'b'. Provided that the indent-tabs-mode variable is true (meaning you have actual tab characters), then this will have no practical effect on the position of the words in the line, as 'indenting' a tab with spaces will not have an effect until the number of spaces exceeds the width of the tab (but that's another kettle of fish entirely!)
All this really means is that text-mode in Emacs doesn't behave the way you'd like it to in this situation. Other major modes can do completely different things when you press TAB, of course.
As is invariably the case with Emacs, things you don't like can be changed or circumvented with elisp. Some searching (especially at the Emacs Wiki) will frequently turn up useful solutions to problems you encounter.
Try typing C-x C-x after Emacs unselects it.
Then, instead of hitting tab (I never knew that tab does what you said! That's totally whacked.), do M-8 C-x C-i. Pity it's so many keys, but it ought to do what you want -- namely, shove everything over 8 columns. Obviously replace the M-8 with something else if you want some other number of columns.
What I usually do is simply type C-x C-x (exchange-point-and-mark) after a command that deactives the region.
How are you indenting, and in which mode?
The indentation rules in any programming mode should generally just get it right. (If they don't, that's probably more indicative that you want to configure the rules for that mode differently, but I suspect that's a different question which has been asked already).
If you're in text-mode or similar and just using TAB, then I can see the problem.
Note that if you're using indent-rigidly (C-x C-i, or C-x TAB which is the same thing) then you can repeatedly indent the same region simply by repeating the command, even if the highlighting has disappeared from view.
You can also use a prefix arg to indent-rigidly to make it indent many times. e.g. C-u C-u C-x C-i (easier to type than it looks) will indent 16 spaces (4 x 4, as the prefix arg defaults to 4, and it multiplies on each repeat). Similarly, M-8 C-x C-i indents 8 spaces. This is fine in some circumstances, and way too cumbersome in others.
Personally I suggest putting (cua-selection-mode 1) into your .emacs and using that for rigid indentation. Trey Jackson made a handy blog about it. With this, you can C-RET to start rectangle selection, down as many lines as you need, TAB repeatedly to indent the lines, and C-RET to exit the mode.
While the rectangle is active, RET cycles through the corners. For left-hand corners, typing inserts in front. For right-hand corners, typing inserts after. For the single-column rectangle, bottom counts as 'left' and top counts as 'right' for this purpose.
Trey's blog lists all the available features (or look in the source file: cua-base.el)
Be warned that indentation in Emacs is generally an unexpectedly complicated topic.
You can do this with something like the following:
(add-hook 'text-mode-hook (lambda ()
(set (make-local-variable 'indent-region-function)
(lambda (s e)
(indent-rigidly s e tab-width)))))
Then selecting a region and hitting TAB. will indent the region by a tab-width. You can then exchange point and mark with C-x C-x and hit TAB again to repeat.
I do, however, agree with the previous answers that suggest using indent-rigidly directly.

Display same file successive pages on Emacs split screen

I've done this before, years ago, so I know the answer is out there. However the google-space is heavily polluted with namespace collisions, especially new user guides.
This is what I'd like to do in Emacs 21:
Split the screen on a given file buffer (C-x 3).
As I page through the first screen (C-v), the next page of the file is automatically displayed on the second.
To be clear, the next line in the file after the last line on the left screen should be the first line on the right screen, always.
Any help?
You might be looking for follow-mode
It is minor mode that combines windows into one tall virtual window.
You want scroll-all-mode
scroll-all-mode is an interactive compiled Lisp function in `scroll-all.el'.
(scroll-all-mode &optional arg)
Toggle Scroll-All minor mode.
With arg, turn Scroll-All minor mode on if arg is positive, off otherwise.
When Scroll-All mode is on, scrolling commands entered in one window
apply to all visible windows in the same frame.
Set up the windows appropriately, and then turn on scroll-all-mode, and from then on all windows scroll together
If follow-mode doesn't work, you could define your own macro, something like
(defun align-windows ()
(set-window-start (other-window) (window-start))
(scroll-other-window))
Then either use this (along with scroll-down) instead of C-v, or rebind C-v to that, or add
advice to scroll-down.
You can start Follow mode and setting up a basic two-window layout using
M-x follow-delete-other-windows-and-split RET.
it is from http://www.emacswiki.org/emacs/FollowMode

Emacs - Multiple columns one buffer

I'm trying to edit some assembly code which tends to be formatted in long but thin listings. I'd like to be able to use some of the acres of horizontal space I have and see more code on-screen at one time. Is there a method for getting Emacs (or indeed another editor) to show me multiple columns all pointing to the same buffer?
C-x 3 (emacs) and :vsplit (vim) are great for multiple separate views into the code, but I'd like it to flow from one column to the other (like text in a newspaper).
See follow-mode.
Excerpt:
Follow mode is a minor mode that makes two windows, both showing the same buffer, scroll as a single tall “virtual window.” To use Follow mode, go to a frame with just one window, split it into two side-by-side windows using C-x 3, and then type M-x follow-mode. From then on, you can edit the buffer in either of the two windows, or scroll either one; the other window follows it.
In Follow mode, if you move point outside the portion visible in one window and into the portion visible in the other window, that selects the other window—again, treating the two as if they were parts of one large window.
I use this function to invoke follow-mode, although it would need customization for a different screen size:
;;; I want a key to open the current buffer all over the screen.
(defun all-over-the-screen ()
(interactive)
(delete-other-windows)
(split-window-horizontally)
(split-window-horizontally)
(balance-windows)
(follow-mode t))
The "Multipager" plugin for Vim can do this with VIM splits for people who want to get this behavior in Vim.
Get it from Dr. Chip's page: http://mysite.verizon.net/astronaut/vim/index.html#MPAGE
Docs: http://mysite.verizon.net/astronaut/vim/doc/mpage.txt.html
Vim can do this using :vsplit - and you can have the same buffer open in multiple "windows" (which are actually sections within a single "window").
Documentation here
A quick look at the emacs wiki doesn't show a mode like you describe. However, it shouldn't be too hard to write one... You just need to split the window with C-x 3 and move the text in the other window down, and whenever you move the text, do the same to the other window...
Problems may occur when you get to the bottom of the buffer, do you want the cursor to immediately go to the other window at the top?
Hmm, maybe its not that easy. But it should still be doable...
this is the default behaviour of emacs when splitting the window (C-x 3 for vertical split)
you get two columns which both have the current buffer open
Use vertical-split with C-x 3. This will split the current buffer into two columns that you can switch between with C-x o.