Emacs tabs strange behaviour .conf files - emacs

Edit: Fresh install of emacs (emacs version from M-x emacs-version: GNU Emacs 25.2.2 (x86_64-pc-linux-gnu, GTK+ Version 3.22.21) of 2017-09-22, modified by Debian)
I had originall had emacs version 47.0 (that was from dpkg -l emacs which showed that as a metapackage)
Server was new and never had it before.
I am not an emacs guru. I don't fiddle too much and I can just get around in it.
I set
tab-width 4
indent-tabs-mode nil
I tried it before setting tab-stop-list and then setting it to nil and to "4 8 12" and to "0 4 8" but it didn't change the behaviour I describe below. In fact none of the settings I changed seemed to affect anything for this.
I then tried searching for the problem, and I tried everything mentioned (c-basic-offset, standard-indent, etc) nothing seemed to help.
I am simply trying to edit conf files.
This is an example
settings.conf:
setting name {
param1=1
param2={
s1="test"
s2=5
}
}
however when I hit the tab key the behaviour is all over the place.
[a1]
setting name {
[a2]
param1=1
param2={
[a3]
s1="test"
s2=5
}
}
in [a1] - tab is 4 spaces - I can hit all day and its fine - perfect
in [a2] hitting tab from the start of the line:
first tab key inserts a tab (size of 4 spaces but backspace deletes it as a whole) and then 3 spaces
second tab key inserts 3 tabs (each the size of 4 spaces)
third tab key inserts 2 tabs (4 spaces size) and then 3 spaces
in [a3] hitting from the start of the line:
first tab key inserts a tab (size 4 spaces)
second tab key inserts a tab (4 spaces) and then 3 individual spaces
third tab key seems to move forward 1 space and converts the 3 individual spaces from before and its newly added 1 space to a tab character as backspace deletes a size of 4 spaces in 1 go
I have no idea what is going on and except for those settings mentioned above that I change through M-x customize I haven't touched anything else at all.
Only other thing I can say is I am running emacs on Ubuntu 18.04 (edit from 20.04 - my mistake, sorry) on a server I ssh too.
By C-h k I get the following:
TAB runs the command indent-for-tab-command (found in global-map),
which is an interactive compiled Lisp function in ‘indent.el’.
It is bound to TAB.
(indent-for-tab-command &optional ARG)
Indent the current line or region, or insert a tab, as appropriate.
This function either inserts a tab, or indents the current line,
or performs symbol completion, depending on ‘tab-always-indent’.
The function called to actually indent the line or insert a tab
is given by the variable ‘indent-line-function’.
If a prefix argument is given, after this function indents the
current line or inserts a tab, it also rigidly indents the entire
It works in 'text-mode" but I don't want to switch to 'text-mode' (in which it works) as I like the highlighting it does.
*Edit (from comment):
I need to edit files that are listed as .conf and .cnf. There are hundreds of them in total. Some are for internal applications. The .cnf file are for nagios, and the .conf files are for icinga
Any help is appreciated.
Thanks.

Related

Indent multiple lines in Emacs

I am looking for shortcuts or functions to add one tab to all lines in the selection. Also how to reduce one tab to all lines in the selection?
There are multiple references for this. But I could not find one that mimics use of tab and shift-tab on selected text as in regular editors like geany. There one can select multiple lines and then use tab or shift-tab to move text left and right till one finds it best.
I found C-x tab <left> or <right> : this moves all lines in the selection to left or right one space at a time. C-5 C-x tab move it 5 spaces but then stops. But I want these lines to move tab spaces at a time while keeping the selection so that I can repeat or undo the process. How can I do that?
Basically, I need a function which I can call after selecting some lines. It should:
Detect the selected lines
On pressing a key (say 1 on num pad) it should move selected text to left by 5 spaces (and keeping the selection)
On pressing another key (say 3 on num pad), it should move selected text to right by 5 spaces (and keep selection)
The function should end on pressing ENTER.
Can above ability be added to C-x tab shortcut, which works exactly like that (with left and right keys) but it moves selected text only one space at a time.
I am looking for shortcuts or functions to add one tab to all lines in the selection.
First select a region.
C-SPC [set-mark-command]
Select region of lines, with the mark at beginning of first line and the point at the beginning of last last line (or vice versa), and then
C-x r t [string-rectangle]
C-q TAB [quoted-insert]
I found C-x TAB <left> or <right>: this moves all lines in the selection to left or right one space at a time.
Did you see on option for hitting S-<left> and S-<right>? That should do it for you, Shift + Right, perhaps only depending on what mode you are in and whether indent-tabs-mode is enabled.
If you use evil-mode, you have the key > that indents the current line or the selection to one level, according to the mode. I don't know how to make it insert tabs. By a quick look at the documentation by keywords (C-h d indent tab RET) it looks like we have the variable indent-tabs-mode:
indentation can insert tabs if this is non nil
More generally, this doesn't seem to suit your requirements but nearly in that it helps on indenting multiple lines in a row and repeatedly: indent-tools (on melpa) (my toy). It's meant for indentation-based files (so yaml, python,…) and it gives an hydra (which helps to repeat the actions) with the commands: indent or de-indent the current block of indentation, the paragraph, etc, and the same to comment, delete, fold, and move around blocks of indentation.

emacs - stop adding indentation to current and next line when pressing RET

I have a file open called test.scss, and when I press RET on a line, emacs will add 2 spaces to the current line and 4 extra spaces to the next line.
I've highlighted what the file looks like with whitespace-mode.
before pressing RET
after pressing RET
You can see that the .my-element row was auto-indented by 2 spaces, and the new line is indented by 4 spaces too many.
I want the output to look like this instead
desired output
What can I do to make emacs produce my desired output?
Here is the output of describe-mode:
Enabled minor modes: Auto-Composition Auto-Compression Auto-Encryption
Electric-Indent File-Name-Shadow Font-Lock Global-Eldoc
Global-Font-Lock Line-Number Menu-Bar Tooltip Whitespace
(Information about these minor modes follows the major mode info.)
SCSS mode defined in `css-mode.el':
Major mode to edit "Sassy CSS" files.
In addition to any hooks its parent mode `css-mode' might have run,
this mode runs the hook `scss-mode-hook', as the final step
during initialization.
Although in this case I'm in scss-mode, I see similar behavior with most of the other modes I use, such as ruby-mode, sgml-mode, js.el mode and others. I'd like to make the behavior match the desired output shown above.
Each mode can handle indentation in its own way, and you may have to look for mode-specific settings to get two-space indentation working everywhere.
For starters, you can set css-indent-offset, which should cover css-mode and scss-mode:
(setq css-indent-offset 2)
You can set the basic indenting of many other modes similarly. ruby-mode seems to use ruby-indent-level, sgml-mode uses sgml-basic-offset, and js-mode uses js-indent-level.

Indent several lines in Emacs

In my Emacs, space key can indent correctly 1 line. If I select several lines, and press space key, the indenting does not work.
By following this link, I try C-M-\ or C-M-q, but C-M deletes directly the whole block selected.
Here is my .emacs, could anyone help?
Select multiply lines, then type C-u 8 C-x Tab, it will indent the region by 8 spaces.
C-u -4 C-x Tab will un-indent by 4 spaces
If you want Emacs to correctly indent multiple lines, then the command is C-META \ also known as C-M-\. In other words, it's not Control - M, but Control - Meta - Backslash (Control + Alt + \ on most keyboards)
[Warning: This approach might lead to errors as mentioned in one of the comments. Iniviting suggestions to fix it.]
Steps to do indentation interactively.
Select the region to be indented.
C-x TAB.
Use arrows (← and →) to indent interactively.
Press Esc three times when you are done with the required indentation.
None of the above method worked for me except Kev's- and that is too verbose for my slow fingers.
For me, I can highlight the region and press Alt-left arrow . Strangely, even though Alt registers as meta in emacs, Esc-left arrow doesn't work.
Another method to indent several lines is just to use the commands to edit several lines: Edit multiple lines at once in Emacs.
Basically, select region and then C-x r t+Tab.

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