As the name suggests, the vim-sneak plugin is used to quickly jump to your target location. If you're looking to supercharge navigation skills within the Vim text editor, this guide is for you. This plugin truly amazed me when I first discovered it because it's not just useful; it's extremely useful. In this article, we'll walk you through how to configure and use the vim-sneak plugin, making it a seamless addition to your Vim toolkit. With vim-sneak, you can easily navigate to specific characters or character sequences, enhancing your text-editing efficiency and boosting your overall productivity. Whether you're new to Vim or a seasoned user, let's dive into the world of effortless navigation.
How to Install the vim-sneak Plugin
First, install the justinmk/vim-sneak plugin using your favorite plugin manager.
For example, to install it using vim-plug, use the following command in your init.vim
or .vimrc
:
Plug 'justinmk/vim-sneak'
If you are unfamiliar with vim-plug or need detailed instructions on managing plugins with it, please refer to this article: How to Manage Plugins in Vim/Neovim with vim-plug.
For other plugin managers, consult their respective documentation for installation instructions.
The Configuration File for vim-sneak
I like to split my configuration into various files and then source them into init.vim
:
You can do the same by following the detailed instructions here.
First, create the file ~/.config/nvim/plug-config/sneak.vim
and then source it in your init.vim
:
source $HOME/.config/nvim/plug-config/sneak.vim
Now, put all the configuration related to vim-sneak
into this file.
Here is the overall configuration file I have checked to be working.
" label-mode
let g:sneak#label = 1
" sneak next by pressing s and S
let g:sneak#s_next = 1
" case insensitive search
let g:sneak#use_ic_scs = 1
" remove the default , and ; keybinding set by sneak
map gS <Plug>Sneak_,
map gs <Plug>Sneak_;
" colors
highlight Sneak guifg=black guibg=#00C7DF ctermfg=black ctermbg=cyan
highlight SneakScope guifg=red guibg=yellow ctermfg=red ctermbg=yellow
" sneak prompt
let g:sneak#prompt = '🔎'
" better f, F, t, and T
map f <Plug>Sneak_f
map F <Plug>Sneak_F
map t <Plug>Sneak_t
map T <Plug>Sneak_T
Now, I am going to explain what each part does and how you can further customize it.
The following is a sample text file, you can use to try on this plugin and this configuration. Search for the cm
as you follow this article.
Plug 'neovim/nvim-lspconfig'
Plug 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-cmdline'
Plug 'hrsh7th/cmp-path'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/cmp-calc'
How to Use vim-sneak without Any Configuration
By default (i.e., if you have not configured vim-sneak), it works similarly to f
, F
, t
, and T
.
Press s
and S
to search for any two consecutive characters (I will call them 'items' from now on). The s
will search in the forward direction, and S
will search in the backward direction. All the found items will be highlighted. Now, ,
and ;
are used to jump to the forward and backward found items.
Setting Labels on the Found Items and Jumping to Them
To use the sneak-label-mode, set g:sneak#label
to 1. This creates labels over each found item. These labels are made of single characters found on your keyboard. You can press these characters to jump directly to any of these labels. I find it extremely helpful.
let g:sneak#label = 1
Along with these labels, you can also press s
or S
repeatedly to navigate through the found items one by one. For this behavior set the variable g:sneak#s_next
to 1.
let g:sneak#s_next = 1
Case-Insensitive Search Using vim-sneak
Just like case-insensitive search, you can also configure case-insensitive Sneak. Here, it will search case-insensitively.
let g:sneak#use_ic_scs = 1
Remapping Keys in vim-sneak Plugin to Avoid Conflicts with Other Mappings
By default, the vim-sneak plugin uses many keys. If you find that some of your old mappings are gone after installing this plugin, it's because vim-sneak has remapped them. You need to disable these vim-sneak mappings by remapping them to other keys. For example, ,
and ;
are also used by default in vim/Neovim for f
, F
, t
, and T
operations. To prevent vim-sneak from using it up, you can use the following two lines:
map gS <Plug>Sneak_,
map gs <Plug>Sneak_;
This will help you avoid conflicts and ensure that your custom mappings work as intended.
How to Change the Sneak Colors
Default sneak colors are not very appealing. Change them as described below.
For this, the plugin provides two highlight groups - Sneak
and SneakScope
.
The group Sneak
highlights all the found sneak items. By default, the magenta color is used if you have not set any color.
highlight Sneak guifg=black guibg=#00C7DF ctermfg=black ctermbg=cyan
The group SneakScope
highlights the sneak-vertical-scope
, as well as the sneak-label-mode
. By default, it is white or black depending on your dark/light background.
highlight SneakScope guifg=red guibg=yellow ctermfg=red ctermbg=yellow
I haven't discussed sneak-vertical-scope
in much detail in this article. To learn about this, use the vim command :help sneak-vertical-scope
.
The GUI settings are for the GUI version of Vim/Neovim, bg
is for background, fg
is for foreground, and cterm
is for the terminal version of Vim/Neovim. To learn more about guifg
, ctermfg
, and how to customize other colors in Vim/Neovim, refer to this link.
Cool Sneak Prompts
You can change the prompt used by vim-sneak. To do this, set the variable g:sneak#prompt
to 🔎, 🕵, or anything you want.
let g:sneak#prompt = '🔎'
If your terminal (like Urxvt or other minimal ones) is not configured to display these characters, don't use these cool prompts. Just don't define the variable g:sneak#prompt
. In that case, it will use the default >
.
Replace Vim's Default 'f' and 't' with Sneak's 'f' and 't'
If you have read my guide on how to use Vim/Neovim's movement, you already know that by pressing 'f', 't', 'F', or 'T', you can jump to any characters, but only within the current line.
You can use Sneak_f
, Sneak_F
, Sneak_t
, and Sneak_T
, and map them accordingly to achieve a better 'f', 't', 'T', 'F' behavior. To do this, configure Sneak as follows:
map f <Plug>Sneak_f
map F <Plug>Sneak_F
map t <Plug>Sneak_t
map T <Plug>Sneak_T
How to Use It:
Press 'f' to search for any single character. Then press 'f' (or 'F') again and again to move forward (or backward). The same procedure follows for 't' and 'T'.
But personally, I don't want to choose between 'f,' 'F,' 'f,' 's,' 'S,' and 't' each time I want to search for something. So I have enabled only the 's' and 'S' functionality mentioned above and don't use this configuration anymore.
Conclusion
The plugin also has other functionality which I have not mentioned here, like deleting/cutting a part of text or vertical. Explore these features by reading the help manual :help sneak.txt
. That's all, folks. Thanks for reading. If you have any comments, questions, or suggestions, please put them in the comment section below.