SkillAgentSearch skills...

Ctrlsf.vim

A text searching plugin mimics Ctrl-Shift-F on Sublime Text 2

Install / Use

/learn @dyng/Ctrlsf.vim
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

ctrlsf.vim

An ack/ag/pt/rg powered code search and view tool, takes advantage of Vim 8's power to support asynchronous searching, and lets you edit file in-place with Edit Mode.

Asynchronous Search

A demo shows how to search a word in an asynchronous way.

ctrlsf async_demo

Compact View

A demo shows compact view which looks more similar to Vim's quickfix windows.

ctrlsf compact_demo

Edit Mode

A demo shows how to rename a method named FocusWindow() to Focus() in multiple files, using [vim-multiple-cursors][7].

ctrlsf_edit_demo

Table of Contents

Features

  • Search and display result in a user-friendly view with adjustable context.

  • Works in both asynchronous (for Vim 8.0.1039+ and NeoVim) and synchronous (for older version of Vim) manner.

  • Edit mode which is incredible useful when you are working on project-wide refactoring. (Inspired by [vim-ags][6])

  • Preview mode for fast exploring.

  • Has two types of view. For both users who love a sublime-like, rich context result window, and users who feel more comfortable with good old quickfix window. (similar to ack.vim)

  • Various options for customized search, view and edition.

Installation

  1. Make sure you have [ack][1], [ag][2], [pt][8] or [rg][10] installed. (Note: currently only Ack2 is supported by plan)

  2. An easy way to install CtrlSF is using a package manager, like [pathogen][3], [vundle][4], [neobundle][5] or [vim-plug][9].

    In vim-plug:

    Plug 'dyng/ctrlsf.vim'
    
  3. Read Quick Start for how to use.

Quick Start

  1. Run :CtrlSF [pattern], it will split a new window to show search result.

  2. If you are doing an asynchronous searching, you can explore and edit other files in the meanwhile, and can always press Ctrl-C to stop searching.

  3. In the result window, press Enter/o to open corresponding file, or press q to quit.

  4. Press p to explore file in a preview window if you only want a glance.

  5. You can edit search result as you like. Whenever you apply a change, you can save your change to actual file by :w.

  6. If you change your mind after saving, you can always undo it by pressing u and saving it again.

  7. :CtrlSFOpen can reopen CtrlSF window when you have closed CtrlSF window. It is free because it won't invoke a same but new search. A handy command :CtrlSFToggle is also available.

  8. If you prefer a quickfix-like result window, just try to press M in CtrlSF window.

Key Maps

In CtrlSF window:

  • Enter, o, double-click - Open corresponding file of current line in the window which CtrlSF is launched from.
  • <C-O> - Like Enter but open file in a horizontal split window.
  • t - Like Enter but open file in a new tab.
  • p - Like Enter but open file in a preview window.
  • P - Like Enter but open file in a preview window and switch focus to it.
  • O - Like Enter but always leave CtrlSF window opening.
  • T - Like t but focus CtrlSF window instead of new opened tab.
  • M - Switch result window between normal view and compact view.
  • q - Quit CtrlSF window.
  • <C-J> - Move cursor to next match.
  • <C-N> - Move cursor to next file's first match.
  • <C-K> - Move cursor to previous match.
  • <C-P> - Move cursor to previous file's first match.
  • <C-C> - Stop a background searching process.
  • <C-T> - (If you have fzf installed) Use fzf for faster navigation. In the fzf window, use <Enter> to focus specific match and <C-O> to open matched file.

In preview window:

  • q - Close preview window.

Some default defined keys may conflict with keys you have been used to when you are editing. But don't worry, you can customize your mapping by setting g:ctrlsf_mapping. :h g:ctrlsf_mapping for more information.

Use Your Own Map

CtrlSF provides many maps which you can use for quick accessing all features, here I will list some most useful ones.

  • <Plug>CtrlSFPrompt

    Input :CtrlSF in command line for you, just a handy shortcut.

  • <Plug>CtrlSFVwordPath

    Input :CtrlSF foo in command line where foo is the current visual selected word, waiting for further input.

  • <Plug>CtrlSFVwordExec

    Like <Plug>CtrlSFVwordPath, but execute it immediately.

  • <Plug>CtrlSFCwordPath

    Input :CtrlSF foo in command line where foo is word under the cursor.

  • <Plug>CtrlSFCCwordPath

    Like <Plug>CtrlSFCwordPath, but also add word boundary around searching word.

  • <Plug>CtrlSFPwordPath

    Input :CtrlSF foo in command line where foo is the last search pattern of vim.

For a full list of maps, please refer to the document.

I strongly recommend you should do some maps for a nicer user experience, because typing 8 characters for every single search is really boring and painful experience. Another reason is that one of the most useful feature 'Search Visual Selected Word' can be accessed by map only.

Example:

nmap     <C-F>f <Plug>CtrlSFPrompt
vmap     <C-F>f <Plug>CtrlSFVwordPath
vmap     <C-F>F <Plug>CtrlSFVwordExec
nmap     <C-F>n <Plug>CtrlSFCwordPath
nmap     <C-F>p <Plug>CtrlSFPwordPath
nnoremap <C-F>o :CtrlSFOpen<CR>
nnoremap <C-F>t :CtrlSFToggle<CR>
inoremap <C-F>t <Esc>:CtrlSFToggle<CR>

Edit Mode

  1. Edit mode is not really a 'mode'. You don't need to press any key to enter edit mode, just edit the result directly.

  2. When your editing is done, save it and CtrlSF will ask you for confirmation, 'y' or just enter will make CtrlSF apply those changes to actual files. (You can turn off confirmation by setting g:ctrlsf_confirm_save to 0)

  3. Undo is the same as regular editing. You just need to press 'u' and save again.

  4. Finally I recommend using [vim-multiple-cursors][7] together with edit mode.

Limitation

  • You can modify or delete lines but you can't insert. (If it turns out that inserting is really needed, I'll implement it later.)

  • If a file's content varies from last search, CtrlSF will refuse to write your changes to that file (for safety concern). As a rule of thumb, invoke a new search before editing, or just run :CtrlSFUpdate.

Arguments

CtrlSF has a lot of arguments you can use in search. Most arguments are similar to Ack/Ag's but not perfectly same. Here are some most frequently used arguments:

  • -R - Use regular expression pattern.
  • -I, -S - Search case-insensitively (-I) or case-sensitively (-S).
  • -C, -A, -B - Specify how many context lines to be printed, identical to their counterparts in Ag/Ack.
  • -W - Only match whole words.

Read :h ctrlsf-arguments for a full list of arguments.

Example

  • Search a regular expression pattern case-insensitively:

    :CtrlSF -R -I foo.*
    
  • Search a pattern that contains space:

    :CtrlSF 'def foo():'
    
  • Search a pattern with characters requiring escaping:

    :CtrlSF '"foobar"'
    " or
    :CtrlSF \"foobar\"
    

Tips

  • CtrlSF searches pattern literally by default, which is different from Ack/Ag. If you need to search a regular expression pattern, run :CtrlSF -R regex. If you dislike this default behavior, turn it off by let g:ctrlsf_regex_pattern = 1.

  • By default, CtrlSF use working directory as search path when no path is specified. But CtrlSF can also use project root as its path if you set g:ctrlsf_default_root to project, CtrlSF does this by searching VCS directory (.git, .hg, etc.) upward from current file. It is useful when you are working with files across multiple projects.

  • -filetype is useful when you only want to search in files of specific type. Read option --type in ack's [manual][6] for more information. Also, a shortcut -T is available.

  • If -filetype does not exactly match your need, there is an option -filematch with which you have more control on which files should be searched. -filematch accepts a pattern that only files match this pattern will be searched. Note the pattern is in syntax of your backend but not vim's. Also, a shortcut -G is available.

  • Running :CtrlSF without any argument or pattern will use word under cursor.

Configuration

  • g:ctrlsf_auto_close defines if CtrlSF close itself when you are opening some file. By default, CtrlSF window will close automatically in normal view mode and keep open in compact view mode. You can customize the value as below:

    let g:ctrlsf_auto_close = {
        \ "normal" : 0,
        \ "compact": 0
        \}
    
  • g:ctrlsf_auto_focus defines how CtrlSF focuses result pane when working in async search mode. By default, CtrlSF will not focus at all, setting to start makes CtrlSF focus at search starting, setting to done makes CtrlSF focus at search is done, but only for immediately finished search. An additional duration_less_than is used to define max duration of a search can be focused for 'at done', which is an integer value of milliseconds.

    let g:ctrlsf_auto_focus = {
        \ "at": "start"
        \ }
    " or
    let g:ctrlsf_auto_focus = {
        \ "at": "done",
        \ "duration_less_than": 1000
        \ }
    
  • g:ctrlsf_auto_preview de

View on GitHub
GitHub Stars1.6k
CategoryDevelopment
Updated24d ago
Forks89

Languages

Vim Script

Security Score

85/100

Audited on Mar 5, 2026

No findings