Skip to content

Commit 4ebdc88

Browse files
committed
feat(search): center screen on search results
- Users can now center the screen on the next search result. - The previous search result can also be centered on the screen. - New key mappings streamline the search experience. - The split window functionality allows for searching the current word from the beginning of the file.
1 parent dd50fb3 commit 4ebdc88

1 file changed

Lines changed: 16 additions & 32 deletions

File tree

plugin/keymaps/normal.lua

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ k("cN", "*``cgN", { desc = "Change word (backward)" })
8282

8383
-- Search {{{
8484

85+
-- Center screen on search results
86+
k("n", "nzvzz", { desc = "Search: Next (center screen)" })
87+
k("N", "Nzvzz", { desc = "Search: Previous (center screen)" })
88+
8589
-- Mark position before search
8690
-- Use `'s` to go back to where search started
8791
-- https://github.com/justinmk/config/blob/master/.config/nvim/plugin/my/keymaps.lua#L51
@@ -104,42 +108,22 @@ k("'s", function()
104108
vim.cmd.nohlsearch()
105109
end, { desc = "Last searched from position" })
106110

107-
-- Split search
108-
-- Mimics the native behaviour of `:h CTRL-W_i`
109-
local split_search_keys = {
110-
["/"] = "Split window and search",
111-
["*"] = "Split window and search current word (forward)",
112-
["#"] = "Split window and search current word (backward)",
113-
["£"] = "Split window and search current word (backward)",
114-
["g*"] = "Split window and search current word (forward, not whole word)",
115-
["g#"] = "Split window and search current word (backward, not whole word)",
116-
[""] = "Split window and search current word (backward, not whole word)",
117-
}
118-
for key, desc in pairs(split_search_keys) do
119-
k("<c-w>" .. key, "<c-w>s" .. key, { desc = desc })
120-
end
121-
122111
-- Split window and search current word from beginning of file
123112
-- Make `<c-w><c-i>` work with `n` and `N` `:h CTRL-W_i`
124-
k("<c-w><c-i>", function()
125-
local cword = vim.fn.expand("<cword>")
126-
vim.cmd.split()
127-
vim.cmd("normal! gg")
128-
vim.cmd("/" .. vim.fn.escape(cword, "\\[]^$.*~/"))
129-
vim.cmd("normal! n")
130-
end, { desc = "Split window and search current word from beginning of file" })
131-
132-
-- This simulates the native `[<c-i>` behaviour but doesn't include imported files.
133-
-- It sets a mark before searching, so can return to the position before the search with `'s`.
134-
-- It also allows using `n` to jump to the next search result (native is `]<c-i>`).
135-
-- `<c-i>` is the same as `<tab>` :h [_CTRL-I
136-
k("[<c-i>", function()
113+
local function split_and_search_current_word()
137114
local cword = vim.fn.expand("<cword>")
138-
vim.cmd("normal! ms")
139-
vim.cmd("normal! gg")
115+
vim.ux.open_side_panel("vsplit || normal! gg")
140116
vim.cmd("/" .. vim.fn.escape(cword, "\\[]^$.*~/"))
141-
vim.cmd("normal! n")
142-
end, { desc = "Search current word (from beginning of file)" })
117+
vim.cmd("normal! nzvzz")
118+
end
119+
120+
local split_search_keys = {
121+
"<c-w><c-i>",
122+
"<c-w>i",
123+
}
124+
for _, key in ipairs(split_search_keys) do
125+
k(key, split_and_search_current_word, { desc = "Split window and search current word from beginning of file" })
126+
end
143127

144128
-- }}}
145129

0 commit comments

Comments
 (0)