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.)
Related
I like format all my code using tab instead of space, but I just want to convert spaces to tabs at the beginning of each lines.
Can tabify just convert space to tabs at the beging of lines?
The documentation for tabify mentions a suitable value for operating on line-leading whitespace only. I used it to write this function which I find handy, but you could just set it in your init file and forego a separate function:
(defun tabify-leading (start end)
"Call `tabify' with `tabify-regexp' set so that only leading
spaces are treated."
(interactive "r")
(setq tabify-regexp-old tabify-regexp)
(unwind-protect
(progn
(setq tabify-regexp "^\t* [ \t]+")
(tabify start end))
(setq tabify-regexp tabify-regexp-old)))
Take a look at SmartTabs
It'll add onto several modes (for several languages) and make it so code indentation are tabs only, while ensuring the display of code is correct regardless of the viewer's tab width.
Excerpt:
Tabs are only used at the beginning of lines. Everything else, like ASCII art and tables, should be formatted with spaces.
Tabs are only used for expressing the indentation level. One tab per “block” – any remaining whitespace is spaces only.
Together with this, you can "tabify" existing code using the tabify command.
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.
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
Is there a way to highlight a string in a text (but not ALL such strings) in a buffer where font-lock-mode is on.
Let's imagine I have a buffer with SQL mode and I want to highlight a string in it.
The following code does not work
(set-text-properties 10 20 '(face hi-yellow))
When I call
(font-lock-mode -1)
it works, but all sql highlighting disappears.
There must be a solution because it's possible to select a region and it will be highlighted but I can't figure out how to do it programmatically
Have a look at http://www.emacswiki.org/emacs/HighlightTemporarily.
Both MarkerPens and Highlight provide functions to highlight a region.
Maybe this helps:
open ***scratch* buffer and enter:
(with-current-buffer "foo" (add-text-properties 1 10 '(comment t face highlight)))
then evaluate with C-j
Characters 1-10 will be highlited in buffer "foo".
Building on Getting Emacs to untabify when saving certain file types (and only those file types) , I'd like to run a hook to untabify my C++ files when I start modifying the buffer. I tried adding hooks to untabify the buffer on load, but then it untabifies all my writable files that are autoloaded when emacs starts.
(For those that wonder why I'm doing this, it's because where I work enforces the use of tabs in files, which I'm happy to comply with. The problem is that I mark up my files to tell me when lines are too long, but the regexp matches the number of characters in the line, not how much space the line takes up. 4 tabs in a line can push it far over my 132 character limit, but the line won't be marked appropriately. Thus, I need a way to tabify and untabify automatically.)
Take a look at the variable "before-change-functions".
Perhaps something along this line (warning: code not tested):
(add-hook 'before-change-functions
(lambda (&rest args)
(if (not (buffer-modified-p))
(untabify (point-min) (point-max)))))
Here is what I added to my emacs file to untabify on load:
(defun untabify-buffer ()
"Untabify current buffer"
(interactive)
(untabify (point-min) (point-max)))
(defun untabify-hook ()
(untabify-buffer))
; Add the untabify hook to any modes you want untabified on load
(add-hook 'nxml-mode-hook 'untabify-hook)
This answer is tangential, but may be of use.
The package wide-column.el link text changes the cursor color when the cursor is past a given column - and actually the cursor colors can vary depending on the settings. This sounds like a less intrusive a solution than your regular expression code, but it may not suit your needs.
And a different, tangential answer.
You mentioned that your regexp wasn't good enough to tell when the 132 character limit was met. Perhaps a better regexp...
This regexp will match a line when it has more than 132 characters, assuming a tabs width is 4. (I think I got the math right)
"^\\(?: \\|[^ \n]\\{4\\}\\)\\{33\\}\\(.+\\)$"
The last parenthesized expression is the set of characters that are over the limit. The first parenthesized expression is shy.