总结了几个关于ivy的使用技巧

关于ivy的使用技巧 包括:

  • 使用tab键来补全选中项与切换目录,试用过spacemacsdoom-emacs,都需要两次tab才能切换目录,自定义ivy-done只需要一次tab就可切换目录
  • 优化ivy-occur批量操作,自动打开wgrep-mode(参考spacemacs),使用 find-name-dired对文件名批量修改
  • counsel-find-file重命名与自动创建不存在的目录优化
  • counsel-agswiper自动搜索选中的内容
  • evil用户使用C-h来清除输入的内容
  • 适应左右分割的窗口,避免ivy选项出现在视线外

还有其它的ivy技巧,欢迎大家补充

2 个赞

建议把所有的小技巧弄一个个gif会比较直观。

1 个赞

之前记的一些snippet,添加args (比方说file mask)比较有用。英文太多,懒得翻译了lul

Counsel

Counsel args (file mask)

Purpose

Enable users to interactively specify extra switches along with the search pattern when invoking counsel-ag.

For example, M-x counsel-ag:

[~/.emacs.d] ag: --elisp --ignore site-lisp -- let

UI

Assume counsel-ag-command is set to ag --nocolor --nogroup.

  • It behaves the same as before when the query starts with anything but a dash: M-x counsel-ag abc ↵ Produces ag --nocolor --nogroup abc

  • Regex are escaped: M-x counsel-ag abc[^d]. ↵ Produces ag --nocolor --nogroup abc\[\^d\].

  • Starting with - or -- passes the text as switches until a --: M-x counsel-ag --elisp -- abc ↵ Produces ag --nocolor --nogroup --elisp abc

  • Initial dashes need to be escaped if they are not switches: M-x counsel-ag \-abc ↵ Produces ag --nocolor --nogroup \\-abc

  • Switches are not escaped. Queries are: M-x counsel-ag --elisp -- abc[^d]. ↵ Produces ag --nocolor --nogroup --elisp abc\[\^d\].

  • Using a prefix argument behaves the same as before: C-u M-x counsel-ag ag --nocolor --nogroup --elispabc ↵ Produces ag --nocolor --nogroup --elisp abc

File Mask

  • -t can apply builtin file masks to filter files, use rg --type-list to see all the builtin masks, e.g. M-x counsel-rg ↵ -t txt -- list to search “list” in all txt files.
  • --type-add can help to define custom file mask, e.g. --type-add 'foo:*.{foo,boo}' -t foo PATTERN.
  • Or, we can define custom search functions using the following format:
    (counsel-rg &optional INITIAL-INPUT INITIAL-DIRECTORY EXTRA-RG-ARGS RG-PROMPT)
    

Swiper

Swiper-avy

In 2015, swiper-avy was added, which could also be used as a workaround for many candidates on a single line. Press

C-' to visually select any candidate on screen using avy.

Ivy

Ivy dispatching done

After open a ivy buffer, call M-o ivy-posframe-dispatching-done to dispatching actions.

Example M-x counsel-colors-webM-oH to insert hex value

Actions can be set via ivy-set-acions:

(ivy-set-actions
 'counsel-colors-web
 '(("h" counsel-colors-action-insert-hex "insert hexadecimal value")
   ("H" counsel-colors-action-kill-hex "kill hexadecimal value")))
1 个赞