Kristian Quirapas
Posted on March 17, 2022
If you've been using Vim for a while now, you probably stumbled upon some navigation plugins like easymotion and vim-sneak. Unless... you're still stuck inside vim without any idea of how to quit. (SHEESH!)
Kidding aside...
easymotion is used for navigating the buffer by pressing a keystroke and marking the text you can jump to. This is demonstrated below. Github
vim-sneak, on the other hand, is also used for navigating your buffer. But instead of relying on marking texts with a keystroke like easymotion, it takes 1-2 characters as an input and marks the text that contains those input characters. As seen below. Github
Okay, full disclosure, I lied.
I didn't actually use easymotion and vim-sneak in the GIFs above. I used hop.nvim.
hop.nvim is a navigation plugin that was created in order to solve easymotion's tendency to temporarily manipulate the buffer in order to mark texts and combines vim-sneak motion as a bonus. Github
You can combine the best of both worlds with one plugin and here's my Neovim configuration for it.
-- hop.nvim
require'hop'.setup()
-- normal mode (easymotion-like)
vim.api.nvim_set_keymap("n", "<Leader><Leader>b", "<cmd>HopWordBC<CR>", {noremap=true})
vim.api.nvim_set_keymap("n", "<Leader><Leader>w", "<cmd>HopWordAC<CR>", {noremap=true})
vim.api.nvim_set_keymap("n", "<Leader><Leader>j", "<cmd>HopLineAC<CR>", {noremap=true})
vim.api.nvim_set_keymap("n", "<Leader><Leader>k", "<cmd>HopLineBC<CR>", {noremap=true})
-- visual mode (easymotion-like)
vim.api.nvim_set_keymap("v", "<Leader><Leader>w", "<cmd>HopWordAC<CR>", {noremap=true})
vim.api.nvim_set_keymap("v", "<Leader><Leader>b", "<cmd>HopWordBC<CR>", {noremap=true})
vim.api.nvim_set_keymap("v", "<Leader><Leader>j", "<cmd>HopLineAC<CR>", {noremap=true})
vim.api.nvim_set_keymap("v", "<Leader><Leader>k", "<cmd>HopLineBC<CR>", {noremap=true})
-- normal mode (sneak-like)
vim.api.nvim_set_keymap("n", "s", "<cmd>HopChar2AC<CR>", {noremap=false})
vim.api.nvim_set_keymap("n", "S", "<cmd>HopChar2BC<CR>", {noremap=false})
-- visual mode (sneak-like)
vim.api.nvim_set_keymap("v", "s", "<cmd>HopChar2AC<CR>", {noremap=false})
vim.api.nvim_set_keymap("v", "S", "<cmd>HopChar2BC<CR>", {noremap=false})
vim.api.nvim_set_keymap()
is used to bind keystrokes to a command in your init.lua file.
For those using Vimscript / VimL
vim.api.nvim_set_keymap("n", "<Leader><Leader>b", "<cmd>HopWordBC<CR>", {noremap=true})
-- is equivalent to
nnoremap <Leader><Leader>b :HopWordBC<CR>
There are more options for hop.nvim that you can check with :h hop
but these are the ones I use.
- HopWordBC looks for a word Before Cursor
- HopWordAC looks for a word After Cursor
- HopLineBC and HopLineAC does the same before and after cursor respectively, but looks for lines instead of words.
- HopChar2AC takes 2 characters as an input and looks for texts that contain those 2 characters After Cursor
- HopChar2BC does the same as HopChar2AC, but before the cursor
That's it! It's my first time posting in Dev.to and I'm glad to be part of this community.
Cheers!
Posted on March 17, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.