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.
A demo shows how to search a word in an asynchronous way.
A demo shows compact view which looks more similar to Vim's quickfix windows.
A demo shows how to rename a method named FocusWindow() to Focus() in multiple files, using vim-multiple-cursors.
- Features
- Installation
- Quick Start
- Key Maps
- Use Your Own Map
- Edit Mode
- Arguments
- Tips
- Configuration
- For user comes from pre v1.0
-
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)
-
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.
-
Make sure you have ack, ag, pt or rg installed. (Note: currently only Ack2 is supported by plan)
-
An easy way to install CtrlSF is using a package manager, like pathogen, vundle, neobundle or vim-plug.
In vim-plug:
Plug 'dyng/ctrlsf.vim' -
Read Quick Start for how to use.
-
Run
:CtrlSF [pattern], it will split a new window to show search result. -
If you are doing an asynchronous searching, you can explore and edit other files in the meanwhile, and can always press
Ctrl-Cto stop searching. -
In the result window, press
Enter/oto open corresponding file, or pressqto quit. -
Press
pto explore file in a preview window if you only want a glance. -
You can edit search result as you like. Whenever you apply a change, you can save your change to actual file by
:w. -
If you change your mind after saving, you can always undo it by pressing
uand saving it again. -
:CtrlSFOpencan 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:CtrlSFToggleis also available. -
If you prefer a quickfix-like result window, just try to press
Min CtrlSF window.
In CtrlSF window:
Enter,o,double-click- Open corresponding file of current line in the window which CtrlSF is launched from.<C-O>- LikeEnterbut open file in a horizontal split window.t- LikeEnterbut open file in a new tab.p- LikeEnterbut open file in a preview window.P- LikeEnterbut open file in a preview window and switch focus to it.O- LikeEnterbut always leave CtrlSF window opening.T- Liketbut 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.
CtrlSF provides many maps which you can use for quick accessing all features, here I will list some most useful ones.
-
<Plug>CtrlSFPromptInput
:CtrlSFin command line for you, just a handy shortcut. -
<Plug>CtrlSFVwordPathInput
:CtrlSF fooin command line wherefoois the current visual selected word, waiting for further input. -
<Plug>CtrlSFVwordExecLike
<Plug>CtrlSFVwordPath, but execute it immediately. -
<Plug>CtrlSFCwordPathInput
:CtrlSF fooin command line wherefoois word under the cursor. -
<Plug>CtrlSFCCwordPathLike
<Plug>CtrlSFCwordPath, but also add word boundary around searching word. -
<Plug>CtrlSFPwordPathInput
:CtrlSF fooin command line wherefoois 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 is not really a 'mode'. You don't need to press any key to enter edit mode, just edit the result directly.
-
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_saveto 0) -
Undo is the same as regular editing. You just need to press 'u' and save again.
-
Finally I recommend using vim-multiple-cursors together with edit mode.
-
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.
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.
-
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\"
-
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 bylet 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_roottoproject, 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. -
-filetypeis useful when you only want to search in files of specific type. Read option--typeinack's manual for more information. Also, a shortcut-Tis available. -
If
-filetypedoes not exactly match your need, there is an option-filematchwith which you have more control on which files should be searched.-filematchaccepts 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-Gis available. -
Running
:CtrlSFwithout any argument or pattern will use word under cursor.
-
g:ctrlsf_auto_closedefines if CtrlSF close itself when you are opening some file. By default, CtrlSF window will close automatically innormalview mode and keep open incompactview mode. You can customize the value as below:let g:ctrlsf_auto_close = { \ "normal" : 0, \ "compact": 0 \}
-
g:ctrlsf_auto_focusdefines how CtrlSF focuses result pane when working in async search mode. By default, CtrlSF will not focus at all, setting tostartmakes CtrlSF focus at search starting, setting todonemakes CtrlSF focus at search is done, but only for immediately finished search. An additionalduration_less_thanis 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_previewdefines whether CtrlSF shows the preview window automatically while moving from match to match in the results pane. The default value is 0.let g:ctrlsf_auto_preview = 1
-
g:ctrlsf_case_sensitivedefines default case-sensitivity in search. Possible values areyes,noandsmart,smartworks the same as it is in vim. The default value issmart.let g:ctrlsf_case_sensitive = 'no'
-
g:ctrlsf_contextdefines how many context lines will be printed. Please readack's manual for acceptable format. The default value is-C 3, and you can set another value bylet g:ctrlsf_context = '-B 5 -A 3'
-
g:ctrlsf_default_rootdefines how CtrlSF find search root when no explicit path is given. Two possible values arecwdandproject.cwdmeans current working directory andprojectmeans project root. CtrlSF locates project root by searching VCS root (.git, .hg, .svn, etc.)let g:ctrlsf_default_root = 'project'
-
g:ctrlsf_default_view_modedefines default view mode which CtrlSF will use. Possible values arenormalandcompact. The default value isnormal.let g:ctrlsf_default_view_mode = 'compact'
-
g:ctrlsf_extra_backend_argsis a dictionary that defines extra arguments that will be passed literally to backend, especially useful when you have your favorite backend and need some backend-specific features. For example, usingptignorefile for pt should be likelet g:ctrlsf_extra_backend_args = { \ 'pt': '--home-ptignore' \ }
-
g:ctrlsf_extra_root_markersis a list contains custom root markers. For example, this option is set['.root'], and there exists a file or directory/home/your/project/.root, then/home/your/projectwill be recognized as project root.let g:ctrlsf_extra_root_markers = ['.root']
-
g:ctrlsf_fold_resultdefines whether CtrlSF should fold result at first. Despite this option you can always fold or unfold manually byzcandzo.let g:ctrlsf_fold_result = 1
-
g:ctrlsf_mappingdefines maps used in result window and preview window. Value of this option is a dictionary, where key is a method and value is a key for mapping. An empty value can disable that method. To specify additional keys to run after a method, use the extended form demonstrated below to specify asuffix. You can just define a subset of full dictionary, those not defined functionalities will use default key mapping.let g:ctrlsf_mapping = { \ "openb": { key: "O", suffix: "<C-w>p" }, \ "next": "n", \ "prev": "N", \ }
-
g:ctrlsf_populate_qflistdefines if CtrlSF will also feed quickfix and location list with search result. By default this feature is disabled but you can enable it bylet g:ctrlsf_populate_qflist = 1
-
g:ctrlsf_regex_patterndefines CtrlSF using literal pattern or regular expression pattern as default. Default value is 0, which means literal pattern.let g:ctrlsf_regex_pattern = 1
-
g:ctrlsf_search_modedefines whether CtrlSF works in synchronous or asynchronous way.asyncis the recommendation for users who are using Vim 8.0+.let g:ctrlsf_search_mode = 'async'
-
g:ctrlsf_positiondefines where CtrlSf places its window in normal view mode. Possible values areleft,left_local,right,right_local,topandbottom. If nothing specified, the default value isleft.let g:ctrlsf_position = 'bottom'
-
g:ctrlsf_compact_positiondefines where CtrlSf places its window in compact view mode. Possible values arebottom_inside,bottom_outside,top_inside, andtop_outside. If nothing specified, the default value isbottom_outside.let g:ctrlsf_compact_position = 'bottom_inside'
-
g:ctrlsf_winsizedefines the width (if CtrlSF opens vertically) or height (if CtrlSF opens horizontally) of CtrlSF main window. You can specify it with percent value or absolute value.let g:ctrlsf_winsize = '30%' " or let g:ctrlsf_winsize = '100'
A full list of options can be found in :help ctrlsf-options.
There are many features and changes introduced in v1.0, but the most important difference is v1.0 breaks backward compatibility.
CtrlSF is at first designed as a wrapper of ag/ack within vim, and the principle of interface design is sticking to the interface of ag/ack running upon shell. This fact lets user get access to all features of ag/ack, and it's easier to implement too. However I found it is not as useful as I thought, what's worse, this principle limits features I could add to CtrlSF and makes CtrlSF counter-intuitive sometimes.
So I want to change it.
Example:
Case-insensitive searching in pre-v1.0 CtrlSF is like this
CtrlSF -i fooIn v1.0, that will be replaced by
CtrlSF -ignorecase fooFor those most frequently used arguments, an upper case short version is available
CtrlSF -I foo