Emacs-Inspired Search in Vim

Search for a command to run...

No comments yet. Be the first to comment.
Remember my post about switching to VSCode? Well, plot twist: the very next day after publishing it, I switched to WebStorm. And guess what? I've been on JetBrains IDEs ever since. I know, I know. Another "why I switched editors" post. But hear me ou...

I had been doing some basic interpreter work in Go and wanted to redo it in another language to solidify my understanding. Naturally, I chose Odin, it’s another great language, with a Go-like, easy-to-approach syntax and no unnecessary complexity. I ...

Why Emacs Keybindings? I've always preferred Emacs-style editing, especially the way it handles cursor movement and text manipulation. Even though I have switched to using VSCode for most of my development work, I often found myself missing the intui...

In my previous post about building C/C++ projects with CMake, I shared a simple CMakeLists.txt setup for compiling a basic project. Today, I’ll dive into a specific need that many developers encounter: linking libraries directly from project folders ...

Some time ago, I wrote a post about using Vim to look cool and why that’s not a good idea. Now, funny enough, here I am—having switched to VSCode after years of hopping between Vim and Emacs. This isn’t one of those "Vim sucks, I'm going back to VSCo...

I wanted to share some in-buffer search techniques that I brought back to my Neovim setup from my year of experience with Emacs.
You know how you can use "/" and "?" to perform forward and backward searches for text in the current buffer. Similarly, in Emacs, you can use Ctrl+s and Ctrl+r to search back and forth. There's a slight difference in how it works in Emacs, though. In Emacs, searching starts immediately as you type Ctrl+s, and the text you type is incrementally added to the current search term. You can achieve the same behavior in Vim with "set incsearch," but there's more. In Emacs, you can keep hitting Ctrl+s and Ctrl+r to continue searching back and forth in the buffer while still editing the search term.
Can we do the same in Vim?
Yes, you can, but it works slightly differently in Vim. When you are in incsearch mode, i.e., once you hit "/" or "?" and start typing a text, you can still edit the text and search back and forth without having to go back to normal mode. The default keys to do that are Ctrl+g (search forward) and Ctrl+t (search backward).
However, this might be a lot to remember, and you may not end up using it even though you know it because of all the keybindings you have to remember.
I have a simpler way to achieve this in my configuration, where I'm able to use Ctrl+s just like in Emacs. Instead of Ctrl+r, I mapped search backward to Alt+s because Ctrl+r is the default keybinding to redo.
The following is what I have in my Neovim config to make this work:
vim.keymap.set('n', '<C-s>', '/')
vim.keymap.set('c', '<C-s>', '<C-g>')
vim.keymap.set('n', '<A-s>', '?')
vim.keymap.set('c', '<A-s>', '<C-t>')