emacs visual highlight to rg (ripgrep) - emacs

I'm almost through adding features from spacemacs to a hand rolled emacs config, but can't figure out how to replicate spacemacs feature of highlighting a term to search for in the page (i've replicated this using evil-viusual-star) and then sending that highlight to counsel-rg.
For non-spacemacs users the workflow is:
(i'm using evil-mode)
In command mode press v, highlight a term
Shift + 8 to search in the buffer for that term
/ to recursively search files in the current directory for search term (using counsel-rg)
Has anyone implemented sending highlighted term to counsel-rg in their config?
Thanks

thing-at-point will return the highlighted word that you can pass to counsel-rg.
(defun my-search ()
(interactive)
(counsel-rg (thing-at-point 'word t)))

Related

Search selected text and show all occurrence of it in current (all other buffers) in emacs?

So in notepad++ I can select text, hit C-f, then, if I need to look for occurrence of selection in all opened files, i hit M-o and get nice clickable list with navigating to occurrence option. Or if I need list only for current file I point mouse to “Find all in current document” button do a click and get same nice clickable list only for currently active file. So is it possible to do exact thing in emac?
You can implement that functionality with the following lisp function:
(defun occur-selection ()
(interactive)
(when (region-active-p)
(let (deactivate-mark)
(occur (regexp-quote (buffer-substring (region-beginning) (region-end)))))))
If you put that code in your ~/.emacs file together with the follwing line:
(global-set-key [(meta o)] 'occur-selection)
you should be able to select some text, hit M-o and get a list of all occurrences of the selected text displayed in a separate buffer.
User M-g n and M-g p do cycle through the matching lines in the original buffer.
Note, however, that multiple occurrences in a single line are not distinguished.
By default, Emacs has M-x occur which work similar but slightly differently. It allows you to specify a regular expression, all matches of which in the current buffer will be displayed and hyperlinked.
If your focus is more on navigation than on highlighting all matches of a search term, there might be an external alternative that could help you.
Emacs' original philosophy is not built around user interface metaphors such as clicking with a mouse, it comes from a keyboard only background. If you're interested in this approach, you might want to have a look at the Avy package for Emacs. It lets you quickly jump to one of multiple occurrences of a word.
Check out the excellent Emacs Rocks episode "Jumping Around" to see a precursor of Avy (called ace-jump-mode) in action: http://emacsrocks.com/e10.html
You can do the same thing with the helm package.
Emacs will search the word the cursor/"point" is on (you
don't need to highlight it).
To make helm search in all open files/buffers, use:
M-x helm-multi-swoop-all
To make helm search only in file/buffer you're currently in, use:
M-x helm-swoop
Press the ENTER key to drop into the selected file at the
selected line.
To bind these functions to the same key-comboes, you'd need
this in your .emacs:
(global-set-key (kbd "M-o") 'helm-multi-swoop-all)
(global-set-key (kbd "C-f") 'helm-swoop)
NB
Helm is hosted in the MELPA repository.
HTH,
Michael

How can I delete a note written in Emacs howm-mode

I wrote a new note in Emacs howm-mode. Now I want to delete it, and I am not able to do so. Should I delete the associated file on my system? Any help is appreciated!
Use Emacs "directory editor", dired, though I think of it simply as a directory explorer.
Use C-x d to bring up a mini-buffer prompt for where to open dired.
I use ido-mode and a vertical presentation to make that mini-buffer easier to use, with these settings,
(ido-mode 1)
(setq ido-decorations '("\n-> " "" "\n " "\n ..." "[" "]"
" [No match]" " [Matched]"
" [Not readable]"
" [Too big]" " [Confirm]"))
(setq ido-default-buffer-method 'selected-window)
In dired, if you type ?, it will show
d-elete, u-ndelete, x-punge, f-ind, o-ther window, R-ename, C-opy, h-elp
in the mini-buffer, so for instance, to delete the file under cursor, type d x.
Useful commands include + to create a new directory, or using m to mark several files, then R to move them all to a new directory, or ^ to jump to the containing directory. (I rarely use ^, it's too far away from home row's j. More often I use dired-x's more general C-x C-j, described below, or selecting the .. you see in dired's buffer.) To create a file, simply use the global C-x C-f binding. More exotic commands would as usual be found in the menu-bar or manuals.
I would recommend the following settings to make dired easier to look at, in a minimal way,
(setq dired-details-hidden-string "")
(require 'dired-x)
(setq-default dired-omit-files-p t)
(setq dired-omit-files "^\\.$")
(when (locate-library "dired-details")
(require 'dired-details)
(dired-details-install))
You can use ( and ) to hide and reveal file information (dired-details), and M-o to toggle hiding of hidden files (dired-x).
dired-x also provides a dired-jump function, bound to C-x C-j, which will jump to the directory containing the current window's file.
The when (locate-library... statement is to prevent errors if you haven't installed dired-details, as it is not built-in to Emacs.
Actually, deleting the associated file from my system did delete the note.
Good enough.
I just wonder if there was a way to do it from within emacs/howm...

emacs tabbar customisation, making unsaved changes visible

i want to mark buffers, that have unsaved changes, in the tabbar tab bar in emacs. Also i want to assign F1 - F12 to the buffer and open them by pressing the according button.
But since i have no programming practice in lisp, i have no idea where to start. Also i got the feeling that learning lisp to know how to configure emacs on a low lwevel is an incredible usefull skill.
So my question is not really how to archieve that, but rather where to start looking. And maybe how to start learning Lisp.
Thanks
This following code works with stock versions of Emacs and tabbar.el -- it creates + sign at the beginning of the modified buffer name in the tab.
;; BUFFER MODIFICATION STATE INDICATOR
(defadvice tabbar-buffer-tab-label (after fixup_tab_label_space_and_flag activate)
(setq ad-return-value
(if (and (buffer-modified-p (tabbar-tab-value tab))
(buffer-file-name (tabbar-tab-value tab)))
(concat " + " (concat ad-return-value " "))
(concat " " (concat ad-return-value " ")))))
(defun ztl-modification-state-change ()
(tabbar-set-template tabbar-current-tabset nil)
(tabbar-display-update))
(defun ztl-on-buffer-modification ()
(set-buffer-modified-p t)
(ztl-modification-state-change))
(add-hook 'after-save-hook 'ztl-modification-state-change)
(add-hook 'first-change-hook 'ztl-on-buffer-modification)
If you want to take it one step further, look at the source code for aquamacs-tabbar.el -- it contains customizable options such as tabbar-unselected-modified and tabbar-selected-modified. You would either need to use Aquamacs for the options mentioned above, or you would need to make a few revisions to the following files so that they work with a stock version of Emacs: aquamacs-tabbar.el, tabbar.el, and tabbar-window.el:
https://github.com/davidswelt/aquamacs-emacs/tree/master/aquamacs/src/site-lisp/tabbar
See also this example of possibilities to further customize the look and feel.
(source: lawlist.com)
As sds mentioned, you'll have to go through the info pages. You could do C-h i m Emacs Lisp RET as mentioned by sds, or you could do M-x info and then find the Emacs Lisp Intro from there.
You raised too many issues in a single question.
I will respond briefly to each, but you should ask a separate question if you are not clear.
i want to mark buffers, that have unsaved changes, in the tabbar tab bar in emacs.
No, you do not want that.
This is already done in the mode line.
i want to assign F1 - F12 to the buffer and open them by pressing the according button
No, you do not want that.
Keys are precious, you do not want to waste them like that.
Use Mouse Buffers menu or list-buffers.
how to start learning Lisp
In Emacs, type C-h i m Emacs Lisp Intro RET and start reading.
Emacs is superbly customizable, and you can make it do exactly what you want.
However, you must realize that it has been used for over 30 years by many smart people, so, whenever your wish is reasonable, chances are this can be done out of the box, and if not, you will have a lot of fun implementing it yourself.
You can also look at tabbar.el to try and figure out where the actual text of the bar is created (i.e. where you'll want to add the "unsaved" indicators). To figure out if a buffer has unsaved changes, you can use the buffer-modified-p function.

Force flyspell to go to the end of the word when autocorrecting word in Emacs

I have found it annoying that flyspell seems to stay in the middle of the word when you do flyspell-auto-correct-word command. Can this be changed to force it to go to the end of the word after running the command? It might be as simple as setting a key binding to auto-complete-word and then move-forward-word which I know how to do. But this won't work in all cases because sometimes it puts the cursor behind the word if the auto-complete word was smaller than the typed word. Any help on this would be great.
Try this code:
(eval-after-load "flyspell"
'(defun flyspell-ajust-cursor-point (save cursor-location old-max)
(when (not (looking-at "\\b"))
(forward-word))))
Tested with flyspell version 1.7k, and with the version shipped with Emacs 23.2.
I looked through the (defun flyspell-auto-correct-word ...) and I can't see any good hooks or other customization points there so I think your best bet is to use C-h f defadvice:
(defadvice flyspell-auto-correct-word (after flyspell-forward-word activate) (flyspell-goto-next-error))

How to highlight all occurrences of a word in an Emacs buffer?

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