How to display indentation guides in Emacs? [closed] - emacs

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to switch to Emacs as my primary source-code editor. I really miss one thing (common in even much simpler editors) - indentation guides (unobtrusive vertical lines which show the indentation level). Is Emacs able to display them?

I've made a function highlight-indentation for this purpose, code is on github.
When invoking highlight-indentation without a prefix argument the current indentation level is naively guessed from major mode (python, ruby and languages based on cc-mode). Only works for space indentations. Customize highlight-indent-face to change appearance of indentation lines.
Examples (ruby, python):
I also frequently use this snippet that folds all code on an indentation level greater than the current line. It's a great way of getting a quick overview of the outline.
(defun aj-toggle-fold ()
"Toggle fold all lines larger than indentation on current line"
(interactive)
(let ((col 1))
(save-excursion
(back-to-indentation)
(setq col (+ 1 (current-column)))
(set-selective-display
(if selective-display nil (or col 1))))))
(global-set-key [(M C i)] 'aj-toggle-fold)

There now is a mode called highlight-indent-guides which seems to work quite well.

to my knowledge nobody has implemented indentation guides for Emacs so far. The closest thing you can get are visualization of TABs with the whitespace package, see Show tabs with a different character (Emacs).

Suppose you could bend ColumnMarker to your needs but it will highlight a column
not givin you a single pixel.
I indent with 8 spaces so i have never thought about it ;P

Related

Why does emacs display tabs differently?

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.

Have emacs highlight characters over 80? [duplicate]

This question already has answers here:
How can I make emacs highlight lines that go over 80 chars?
(8 answers)
Closed 8 years ago.
I have seen some solutions on here which will highlight an entire line if it goes beyond 80 characters, and also to do a line-wrap once the line becomes greater than 80. What I would like to do is to edit my .emacs file so that any character beyond 80 takes on a different background. So the first 80 characters will have my current emacs background, then characters > 80 would have a black background say. Can someone point me in the right directions? Thanks!
I recently wrote an extensive article on the subject. Here's the gist of it:
(require 'whitespace)
(setq whitespace-line-column 80) ;; limit line length
(setq whitespace-style '(face lines-tail))
(add-hook 'prog-mode-hook 'whitespace-mode)
Here are some other solutions that people use, in addition to the one that #BozhidarBatsov mentions:
Library column-marker.el -- highlight any column(s)
Library fill-column-indicator.el -- highlight the fill column
Library mode-line-posn.el -- highlight column number in mode line when greater than limit
Library wide-column.el -- highlight cursor when column passes limit
See also:
FindLongLines
HighlightLongLines
EightyColumnRule
MarginMode
(FWIW, my personal preference is mode-line-posn.el --- less intrusive, just the right amount of indication.)

How to exchange words in emacs(by replace them with each other one) [duplicate]

This question already has answers here:
How can I swap or replace multiple strings in code at the same time?
(5 answers)
Closed 8 years ago.
Say I have some text like:
First one is good, so I used first one.Second one is bad, so I drop it.
I want to switch the 'first' and 'second',and like replace-string,leave the capital to the same case as original word.
Is there any built-in functions to handle this situations?
Edit:
Let me explain the problem further.If I use usual replace-string twice,will some times cause unwanted results.In the example above, If using replace-string first RET second RET, then replace-string second RET first RET,it will out put: First one is good. so I used first one.First one is bad, so I drop it. It also a problem in some case like "clientFolder=>serverFolder and server=> client"
#huaiyuan answered the same question here:
How can I swap or replace multiple strings in code at the same time?
His code allows you to enter arbitrary list of pairs to do parallel replacement.
Incidently, if you want to read some cool lisp code, click on #huaiyuan and read his answers.
Here's a great trick courtesy of Mickey's Mastering Emacs blog (see http://www.masteringemacs.org/articles/2013/01/25/evaluating-lisp-forms-regular-expressions/ under the heading "Swapping Elements")
C-M-% \(first\)\|second RET \,(if \1 "second" "first") RET
Edit: and here's an elisp version of that:
(defun my-swap-text (a b)
"Swap two pieces of text wherever they appear, using `query-replace-regexp'."
(interactive "sSwap: \nswith: ")
(let ((use-region (and transient-mark-mode mark-active)))
(query-replace-regexp
(rx (or (group (eval a)) (eval b)))
(quote (replace-eval-replacement replace-quote (if (match-string 1) b a)))
nil
(when use-region (region-beginning))
(when use-region (region-end)))))
did you google search this at all?
http://kb.iu.edu/data/abdp.html

Is there a good emacs mode for displaying and editing huge delimiter separated files?

I've been searching without finding for a while for a mode that makes editing huge tab/comma/colon-separated files easy. I've been wanting a mode that ensures that columns always line up, just like org-mode tables. I know I can easily turn the whole file into an org-mode table and then turn it back when I'm done, but that gets really slow with huge files, and is a hassle for quick edits (there's also the problem of what happens if a field contains a vertical bar). So does anyone know of either a mode or a built-in function/variable I can use so that I can get a file like
col1\tcol2\tcol3
very long column1\tcol2\tcol3
displayed like
col1 col2 col3
very long column1 col2 col3
? (perhaps with some color lining the separator)
Perhaps you could tell us what you've already found and rejected?
If you've been searching, then you must surely have seen http://emacswiki.org/emacs/CsvMode ? You don't mention it, or say why it wasn't any good, though.
SES (Simple Emacs Spreadsheet) might be a useful approach:
C-hig (ses) RET
You can create a ses-mode buffer and yank tab-delimited data into it (that's the import mechanism).
It's probably more hassle than you were after, though, and I'm not sure how well it will perform with "huge" files.
Try csv-mode, which works in at least Emacs 24.
You can set the variable csv-separators to change the separator if you do not use the default one (comma).
See EmacsWiki.
As #choroba mentioned, use csv-mode. To answer your question specifically:
Make sure your separator is in csv-separators, which for example you can set with
(setq csv-separators '("," " "))
Use csv-align-fields (default keybinding C-c C-a) to line up the field values into columns.
#unhammer's comment about aligning only visible lines is great. Their code properly indented:
(add-hook 'csv-mode-hook
(lambda ()
(define-key csv-mode-map (kbd "C-c C-M-a")
(defun csv-align-visible (&optional arg)
"Align visible fields"
(interactive "P")
(csv-align-fields nil (window-start) (window-end))
)
)
)
)
There is pretty-column.el, which I found in Group gnu.emacs.sources years ago (it was added in 1999). That group is now blocked, by Google. I just used pretty-column.el on a ~5000 line tab-separated text file that Org mode choked on (Org mode has a 999 line limit on converting such a file--for that reason).
Added in edit: This seems to now be called delim-col.el (see this Emacs Wiki entry); the author is the same person.

Scheme editing in Emacs - modes and keyboard layout

Recently I started using Emacs as my Scheme (Lisp) editor. I'm thinking what extensions should I use in order to achieve the best performance. Currently I'm using Paredit and it helps a lot. I know that there are numerous Scheme extensions for Emacs: Geiser, Quack to name the two that seem very popular, and EmacsWiki lists many more. Which of these have you guys used and which ones do you find the best? At the moment my biggest problem is lack of parentheses colouring, which makes it vary hard to pair them visually - indentations are not enough when you have a line of code ending with ))))))))
I'm also thinking how could I improve the keyboard layout of Emacs in order to do better in Scheme editing? I've found some good advice on CLiki. I swapped [] with () on the keyboard and that's helpful. I'm also considering swapping Alt and Ctrl keys.
Do you have any other tips and suggestions that make it easier to edit Scheme in Emacs?
I've found rainbow delimiters mode really helpful for highlighting different levels of parentheses.
Among other modes that help me write lisp are hideshow mode for folding of sexps, slime which is primarily for Common Lisp but I use it's indentation capabilities in scheme too, low-contrast color theme called solarized with which my eyes don't fatigue any more and heavily mutated vim mode which permits me to keep my keybindings manageable through editing modes.
I use show-paren-mode, a minor mode, with these in my .emacs:
(show-paren-mode t)
(setq show-paren-delay 0)
(setq show-paren-style 'expression) ; alternatives are 'parenthesis' and 'mixed'
Relevant faces to modify are show-paren-match and show-paren-mismatch.
It only highlights a sexp when point is immediately before or after it, but I like that it's not so in-your-face.
I use autopair to get parenthesis right, show-paren-mode to see the end and beginning of s-expressions and expand-region to mark s-expression (It works on a lot more than that).
I think as you keep playing with paredit you may see less and less need for parenthesis coloring. For example, type ')' within any sexp, and the opening and closing parens will be momentarily highlighted; then point will move to the end of the sexp. Being able to navigate the nested sexp structure easily - for example, C-M-u and C-M-d to navigate up and down one paren level - also takes away some of the need to visually pair parenthesis.