I am relatively new to emacs environment and I have trouble find the solution for this problem.
what happens is when I insert a curly bracket e.g. foo(, it automatically adds 4 spaces in the front of the current line.
I am currently using bsd C style. Is there any setting that I can change this behavior? I have tried changing the C Basic Offset but it has no effect so far.
Emacs isn't really adding spaces, instead it's "fixing" the indentation, which it most likely thinks is 4 spaces per indentation level. c-basic-offset is the variable you want to change, you should put this in your .emacs:
(setq-default c-basic-offset 2)
You need setq-default. If you just use setq, it will only change it for the current buffer.
Related
So I've heard about the goodness of emacs and have only recently started using it. Forgive me if this is a stupid question, but why does emacs display tabs, differently? It seems as though it doubles the number of spaces, but it doesn't, at least, I guess. Here're some pictures to describe what I'm talking about:
And this is what it looks like in emacs:
As I've previously stated, it seems as though it doubles the number of spaces. When I add this line to .emacs:
(setq c-basic-offset 4)
and reindenting the code using C-x h C-M-\ makes it look normal in emacs, but the secondary indentation are in-line with the first indentation (as in 2 tabs are now 1 tab) when viewed in other text editors, and again, I couldn't understand why. Changing it to
(setq c-basic-offset 8)
makes it save and display normally in other text editors though. At this point I'm really, really confused.
Can someone please explain why? Thanks.
The variable tab-width is the distance between tab spaces in columns, and defaults to 8. If you'd like it to default to 4, you can (setq-default tab-width 4). If you'd like to untabify everything and convert tabs to spaces, you can do M-: (untabify (point-min) (point-max)).
And you might find this thread helpful, especially the point on tab-stop-list when you want to ADD your own tabs.
You can also adopt sanity and not use TAB chars in your code. ;-)
To prevent inserting TAB chars when you hit the TAB key (and RET or C-j, depending on your Emacs version) set the value of option indent-tabs-mode to nil.
To remove pre-existing TAB chars from code you are editing, use command untabify.
See also Tabs Are Evil and Untabify Upon Save.
And note that, in Emacs, whether or not you use TAB chars is unrelated to how much and whether code is indented. For example, option c-basic-offset governs indentation amount regardless of whether TABs are used for some of the indenting.
Note too that after you kick the TAB habit, any TAB chars left in your code that are meaningful to the code are much easier to find. They are not lost in an ocean of insignificant-whitespace TABs.
Finally, note that there are various ways to highlight TAB chars. Command hc-toggle-highlight-tabs in library highlight-chars.el is one way. See Show Whitespace.
I have an emacs Major/Minor mode which handles alot of automatic indentation, spacing etc via fill-region and other commands. Mostly this works, but there are times when I'd like to select a region, and force the region to simply left justify each new line at the first cursor.
So, for example, the indentation provided by the major mode for certain rare instances erroneously looks like this:
#{Returns a new #racket[list?] without the
#racket[last] of #racket[lst].
#examples[#:eval the-eval (remove-last '(1 2 3 4 5))]}
I want to highlight that region and make it look like this:
#{Returns a new #racket[list?] without the
#racket[last] of #racket[lst].
#examples[#:eval the-eval (remove-last '(1 2 3 4 5))]}
All of the alignment, align-regexp attempts I've tried havent worked. It seems like this should be a simple enough desire, and may it's the major mode that's interfering here?
Thanks.
The easiest function I found out is evil mode's evil-shift-left. It is bound to < in evil mode, but you can call the function without using the mode:
;; install the mode, prebuilt in emacs24
(require 'evil)
;; define your key binding
and then highlight the region and call M-x evil-shift-left
Hope that suits your needs.
Motivation: Using the defaults, Auto Fill mode seems not as useful as I might
have hoped: If I insert a sentence in the middle of a paragraph, only the current
line is re-filled. When I insert a sentence, I want the entire paragraph to be re-filled.
Question: How can I set auto-fill-function (or perhaps
normal-auto-fill-function) in my .emacs
file so that the paragraph is re-filled whenever a single line overflows?
I tried setting it to fill-paragraph, but then I cannot insert any spaces at the end of a paragraph (e.g., to add another word).
More details: I primarily use Auto Fill mode in the AUCTeX major mode for LaTeX.
The built-in Emacs documentation for auto-fill-mode states:
When auto-fill-mode is on, the auto-fill-function variable is
non-nil.
The value of normal-auto-fill-function specifies the function to use
for auto-fill-function when turning Auto Fill mode on.
The documentation for the normal-auto-fill-function variable
says that it is the function to use for auto-fill-function if Auto Fill mode is
turned on, and that the initial value is do-auto-fill.
You might like to try refill-mode. But in general, it's just tricky to make such a feature work well. Another approach is to only do the refill as part of the redisplay (i.e. without affecting the buffer's actual content). For that, try setting word-wrap or enabling visual-line-mode.
For LaTeX files you can try (requires AUCTeX)
(add-hook 'LaTeX-mode-hook '(lambda ()
(setq auto-fill-function 'LaTeX-fill-paragraph)))
but use it with caution.
Is there an equivalent to the fill-column variable for the "Wrap" mode (visual-line-mode) in Emacs? If not, how could I set a limit on the length of visual lines when the window/frame of the buffer is wider ?
In response to this question I created a minor mode called window-margin that accomplishes what #Stefan suggested in his answer.
Turn on window-margin-mode with:
(add-hook 'text-mode-hook 'turn-on-window-margin-mode)
The way you can still do it without installing window-margin is to use the longlines-mode that ships with Emacs, but is being phased out since there are some problems with longlines-mode, but here's the old way to do it if you want:
Turn on longlines-mode with something like:
(add-hook 'text-mode-hook 'longlines-mode)
which wraps text at the fill-column.
longlines-mode has been removed. For visual-line-mode, the simplest way is to make the window as narrow as you want it to be. You can do that with C-x 3 and then adjusting the size of the window. Or you can set a wide margin or wide fringes.
I like to run my editor full-screen. The only thing is, though, that when I do this, the word wrap only kicks in when the line hits the right edge of the screen. I would like it to do so, already when the line hits, say, column number 200.
How do I do that?
I would like it to happen in all modes, e.g., Org-mode. I added the line (global-visual-line-mode t) to my .emacs file, in order for the word wrapping also to work in org-mode.
I'm running Emacs 23.
I got it working! Here is what I added to my .emacs file to make it happen:
(add-hook 'text-mode-hook 'turn-on-auto-fill)
(add-hook 'text-mode-hook
'(lambda() (set-fill-column 80)))
Type M-x auto-fill-mode to activate automatic line-wrapping after a certain column. Then set the actual line width through the variable fill-column as described by user choroba (C-x f).
Note though that this works a bit differently from what other text editors do. M-q will re-format the current paragraph.
You can set the line width with C-xf (set-fill-column).
Afterwards, you might need to hit M-q to reformat the current paragraph (fill-paragraph), or select text to be justified and run fill-region.
The suggestion for turn-on-auto-fill will work if you want hard newlines in the files you're editing. If not, and you just want word-wrap, consider instead visual-fill-column-mode, which just does the normal word-wrap that would happen at the edge of the window, but at the specified fill-column.
See the Emacs manual (C-h r), node Filling. See in particular the first subnode in the menu, Auto Fill.