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".
Related
Under Emacs, how to get nice page-break-lines like these?
Install the page-break-lines package, then:
M-x turn-on-page-break-lines-mode
To insert a new page break line: C-q C-l
(this inserts a ^L char in your buffer)
You can also easily jump to next/previous page break using C-x [ or C-x ]
Library Pretty Control-L lets you do that. You can easily customize the appearance of a page-break (aka line feed, aka Control-l character).
The library is pp-c-l.el.
Mode pretty-control-l-mode turns the display on and off.
You have these user options and face, which you can customize:
pp^L-^L-string - Highlighted string displayed in place of each Control-l (^L) character.
pp^L-^L-string-pre - String displayed just before pp^L-^L-string.
pp^L-^L-string-post - String displayed just after pp^L-^L-string (empty by default).
pp^L-^L-string-function - Function to produce string displayed in place of a Control-l (^L) char. If the option value is non-nil, option pp^L-^L-string is not used.
You can use this option to have a dynamically defined display string.
For example, this value displays a window-width horizontal line:
(lambda (win) (make-string (1- (window-width win)) ?_))
pp^L-highlight (a face, not a variable) - Face used to highlight pp^L-^L-vector.
I am trying to get emacs to highlight trailing-spaces. I have tried using WhiteSpace, and also tried setting show-trailing-whitespace variable to true, but in each case it changes the representation of newline and space characters to $ and · characters, as shown in this screen capture.
Ideally I would like to just see the trailing whitespace highlighted in red without any such characters.
Disclaimer: I'm new to emacs, so this may well be obvious.
I don't use any library. I just set show-trailing-whitespace to t and any trailing white space is shown in red. The representation of newline and space characters is NOT changed.
Actually, my ".emacs" file contains this simple line:
(setq-default show-trailing-whitespace t)
In case you don't want to edit your ".emacs" file, you may try:
C-h v show-trailing-whitespace RET then click the customize link
(or just M-x customize-variable RET show-trailing-whitespace RET)
Click the toggle button to set it to on (non-nil)
Click the menu button State > Set for Current Session
Click the menu button State > Save for Future Sessions
[EDIT] (thanks to Francesco Frassinelli for his comment)
With setq-default, the value is changed for every mode.
If you want to disable it for some mode (like term-mode for example), you have to:
find the mode name of the current buffer. Usually you can get it from within the buffer with M-x describe-mode RET (shortcut C-h m or <f1> m).
find the entry "hook" for this mode. Usually, it's the mode name with the suffix -hook. You can find it by searching "hook" in the buffer describing the mode. For example, you may read:
Entry to this mode runs the hooks on ‘term-mode-hook’
add the following to your ".emacs" file:
(add-hook 'term-mode-hook (lambda () (setq show-trailing-whitespace nil)))
or you may try:
M-x customize-variable RET term-mode-hook RET
Click the INS button
Paste (lambda () (setq show-trailing-whitespace nil))
Click the menu button State > Set for Current Session
Click the menu button State > Save for Future Sessions
Note that show-trailing-whitespace automatically becomes buffer-local when set with setq.
Change the value of the whitespace-style variable to
(face trailing)
You might need to restart whitespace-mode for the changes to take effect.
To set a variable, use M-xset-variableEnter.
Another answer is to use library highlight-chars.el (description: Highlight library).
Command hc-toggle-highlight-trailing-whitespace does what you request.
You can also turn on such highlighting automatically, either everywhere or in a given buffer or for a given mode.
Is there some way I can mark text in emacs and shift it left (removing starting spaces) by space/Tab granularity?
Same way I would do on some other editor with Shift+Tab.
Select your region;
Type C-u followed by the number of spaces you want to indent (negative number if you want to decrease indentation);
Use C-x TAB (by default bound to indent-rigidly) to apply the indentation to the region.
This is much more cumbersome than S-TAB, but it is IMHO some kind of last resort in case Emacs formatting doesn't solve your problem.
EDIT: much better solution: Shift a region or line in emacs (accepted answer). This is what I'm currently using in Emacs for changing indentation. WARNING: involves some Emacs Lisp.
This might be simpler and more visually intuitive: first make sure cua-mode is enabled (M-x cua-mode toggles it). Then go to the start of the line and press C-return. A red rectangle appears. Now move your cursor down and right to grow the rectangle as needed. Then press C-d to delete it. That's it.
I come across this problem often when the major-mode doesn't dictate any automatic indentation (or when it messes up).
There is a lot more you can do with cua-mode's rectangles, see http://trey-jackson.blogspot.com/2008/10/emacs-tip-26-cua-mode-specifically.html
Generally emacs places things where the current style dictates when you hit <TAB>, so naturally it's a little different here. The closest thing that comes to mind is M-\ which collapses horizontal whitespace around point. If you want to remove a "rectangle" of space before the lines, then delete-rectangle might be more appropriate, which you can do by setting mark and moving point to select the rectangle and then using C-x r d.
It sounds like the problem you're trying to solve is incorrect indentation of code when you're cutting/pasting. You can solve that by automatically re-indenting the text with something like the following.
Note: Using a prefix argument forces no re-indentation (C-u C-y), plus there's the size threshold variable.
;; automatically indenting yanked text if in programming-modes
(defvar yank-indent-modes '(emacs-lisp-mode
c-mode c++-mode
tcl-mode sql-mode
perl-mode cperl-mode
java-mode jde-mode
lisp-interaction-mode
LaTeX-mode TeX-mode)
"Modes in which to indent regions that are yanked (or yank-popped)")
(defvar yank-advised-indent-threshold 1000
"Threshold (# chars) over which indentation does not automatically occur.")
(defun yank-advised-indent-function (beg end)
"Do indentation, as long as the region isn't too large."
(if (<= (- end beg) yank-advised-indent-threshold)
(indent-region beg end nil)))
(defadvice yank (after yank-indent activate)
"If current mode is one of 'yank-indent-modes, indent yanked text (with prefix arg don't indent)."
(if (and (not (ad-get-arg 0))
(member major-mode yank-indent-modes))
(let ((transient-mark-mode nil))
(yank-advised-indent-function (region-beginning) (region-end)))))
Suppose I have a few words I would like to highlight, so I want to change the color of those few words only to, say, green.
Is there an easy way to do this in emacs?
Thank you.
This is what I've done, using font-lock-add-keywords. I wanted to highlight the words TODO:, HACK:, and FIXME: in my code.
(defface todo-face
'((t ()))
"Face for highlighting comments like TODO: and HACK:")
(set-face-background 'todo-face cyan-name)
;; Add keywords we want highlighted
(defun add-todo-to-current-mode ()
(font-lock-add-keywords nil
'(("\\(TODO\\|HACK\\|FIXME\\):" 1 'todo-face prepend))
t))
Use library HighLight. You can use overlays or text properties. You can save the highlighting permanently or let it be temporary. You can highlight in many ways (regexp, mouse-drag,...). Lots of possibilities.
The highlight package has hlt-highlight-regexp-region and hlt-highlight-regexp-to-end, which do exactly what you want.
http://www.emacswiki.org/cgi-bin/wiki/highlight.el
Use the function font-lock-add-keywords to define a new matcher for the string in question, binding that matcher to some face you've defined that will display as green. For example:
(font-lock-add-keywords nil
'("\\<foo\\>" 0 my-green-face))
Note that you can specify a particular mode where I wrote nil above, and the matching forms can take on any of six different styles. See the documentation for the variable font-lock-keywords for the rules and a few examples.
If you want them highlighted only temporarily, I find M-x highlight-regexp command very helpful, it is especially nice for looking through log files of sorts. For example you made yourself a logging class that outputs some tracing info like MyClass::function() > when function is run and MyClass::function() < when it exits (can be especially useful sometimes when debugging multithreading issues) then you just ask emacs to highlight some of them green and other red and then you can see how did the execution go.
I use what Dimitri suggested. In particular, I have the following two lines in my .emacs
(global-hi-lock-mode t)
(global-set-key (kbd "C-M-h") 'highlight-regexp)
Every-time I need to highlight a certain word (or regex) in a buffer, I hit "C-M-h", which then prompts me for the word (or regex) I want to be displayed differently and then for a face to display it in.
Notepad++ has a convenient feature: if you select a word in your text (not necessarily a keyword), the word is highlighted throughout the text. Can this be done in Emacs as well? And if so, how?
It doesn't necessarily have to work exactly like Notepad++ (i.e., via selection); ideally, I would like to set up a key binding that causes all occurrences of the word under cursor to be highlighted.
It would be great if the highlights were permanent, i.e., moving point away from a highlighted word should not cause the highlight to be removed.
Also, it would be useful if there was a solution that made it possible to navigate between highlights (using custom key bindings).
The hi-lock suggestions are good. I think it's easier to use the M-x versions, though:
M-x highlight-regexp RET <REGEXP>
M-x highlight-phrase RET <REGEXP>
highlight-phrase is just a bit of sugar around highlight-regexp that ignores case and translates a space in the regex to match arbitrary whitespace. Handy.
Maybe highlight-symbol.el at http://nschum.de/src/emacs/highlight-symbol/ is what you are looking for:
Type C-s, then type the current word or type C-w. As a bonus, you can now hit C-s again to search for the word.
This is called incremental search.
What I use is idle-highlight
http://www.emacswiki.org/emacs/IdleHighlight
M-x idle-highlight sets an idle timer that highlights all occurences in the buffer of the word under the point.
To enable it for all programming modes, in ~/.emacs.d/init.el:
;; highlight words
(add-hook 'prog-mode-hook (lambda () (idle-highlight-mode t)))
Light-symbol will highlight whatever symbol point is over.
Alternately, you can use occur, which lists all lines matching a regexp. It's useful to quickly see all functions in a class.
Nobody mentioned symbol-overlay mode. It's basically a better rewrite of highlight-symbol-mode. "Better" as in, lacks bugs of original highlight-symbol (such as, temporary highlight getting stuck, or the temporary highlight disappearing for moving inside the highlighted word; or not being able to highlight symbols like *), better integrated, and maintained. See "Advantages" paragraph of its README.
You can install it as usual, with M-xpackage-install (make sure to update package list beforehand with package-list-packages). For reference, at the bottom I attached code I use to enable the mode and disable a few of the more advanced features which you may or may not want.
Notepad++ has a convenient feature: if you select a word in your text (not necessarily a keyword), the word is highlighted throughout the text. Can this be done in Emacs as well? And if so, how?
Once you enable overlay-symbol, occurrences on the screen will be shown for every word that you put cursor upon after a timeout (timeout by default is 0.5s, can be configured with symbol-overlay-idle-time variable). If a word don't get highlighted, this means there's just one match on the screen (the one you put cursor upon), hence there's no need to highlight it.
It would be great if the highlights were permanent, i.e., moving point away from a highlighted word should not cause the highlight to be removed.
To highlight the word under cursor permanently there's a function symbol-overlay-put. To unhighlight call it once again.
In my config example it's bound to Logo+` key.
(require 'symbol-overlay)
(defun enable-symbol-overlay-mode ()
(unless (or (minibufferp)
(derived-mode-p 'magit-mode)
(derived-mode-p 'xref--xref-buffer-mode))
(symbol-overlay-mode t)))
(define-global-minor-mode global-symbol-overlay-mode ;; name of the new global mode
symbol-overlay-mode ;; name of the minor mode
enable-symbol-overlay-mode)
(global-symbol-overlay-mode) ;; enable it
(global-set-key (kbd "s-`") 'symbol-overlay-put)
(setq symbol-overlay-ignore-functions nil) ;; don't ignore keywords in various languages
(setq symbol-overlay-map (make-sparse-keymap)) ;; disable special cmds on overlays
This may not be as nice as what you were hoping but if you put
(global-hi-lock-mode 1)
in your .emacs file then you can type C-x w h REGEX <RET> <RET> to highlight all occurances of REGEX, and C-x w r REGEX <RET> to unhighlight them again. Again, not as elegant as you'd probably like, but it'll work.
Try http://www.emacswiki.org/emacs/msearch.el
All occurences of the text selected with the cursor are highlighted.
You have to drag over the string which you want to highlight. That enables you to easily change the selection without changing the highlight.
If you want to preserve the highlighting of a string you can freeze it.
You can enslave a buffer to another buffer. Text selected in the master buffer will also be highlighted in the slave buffer. That is useful for comparing buffers.
It is also useful for taking notes in one buffer while you investigate the text in another one. You can have a collection of keywords in the notes buffer. Drag over such a keyword and its occurences in the investigated text will be highlighted.
I am using this stuff for years now. I added the freezing quite recently. So, maybe something is broken. If this is the case leave me a note on http://www.emacswiki.org/emacs/msearch or here.
Check Interactive Highlighting
Should be:
C-x w h word <RET> <RET>
Try iedit. It highlights the word at point and lets you edit all occurrences of it easily. With an additional keystroke (C-'), it hides all the lines without that word in it. Very handy!
Commands in library highlight.el
let you (un)highlight text matching a regexp (in this case a symbol), using overlays or text properties. You can cycle among the occurrences. Highlighting can be temporary or persistent. (more info).
This maybe won't highlight but will search for a word without you needing to type it...
when you've reached the word you wanted to search, C-S, then read the full word with C-W then you can C-S and it will search for it. In my Emacs it also highlights all instances in the document.
This package available in Melpa works, you can customize the highlight style as well.
https://github.com/ignacy/idle-highlight-in-visible-buffers-mode