using neovim, i would like to write some helper that does the following:
when i'm inside a cypress test file inside a it('bla', () => { /* somewhere in here */ }); function, i would like to go up to change the call to be it.only('bla', // ... with a few keystrokes or a macro or something, what are my options?
Also, when there already is a .only present, i want to remove that instead.
The function can have multiple lines and also functions inside of it, btw.
The macro should also not match functions that just end in it(', like e.g. wait('');
Assistant
You can achieve this in Neovim by creating a Lua function and mapping it to a key combination. Here's an example implementation:
lua
User
forgot to add before: i do not like vimscript, i'd rather have lua code.
Also, i'd like to return the cursor to where it was before calling the macro
Assistant
Here's an updated version of the code that uses Lua and restores the cursor position after executing the macro:
lua
local function toggle_only()
local row, col = vim.api.nvim_win_get_cursor(0)
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
-- Find the 'it' block from the current position
local start_row = row
while start_row > 1 and not vim.regex('it\\s*\\('):match_str(lines[start_row]) do
start_row = start_row - 1
end
-- Check if we found an 'it' block
if vim.regex('it\\s*\\('):match_str(lines[start_row]) then
-- Check if '.only' is already present
if vim.regex('it\\.only\\s*\\('):match_str(lines[start_row]) then
-- Remove '.only'
local new_line = vim.regex('it\\.only\\s*\\('):substitute(lines[start_row], 'it (')
vim.api.nvim_buf_set_lines(0, start_row, start_row + 1, true, {new_line})
else
-- Add '.only'
local new_line = vim.regex('it\\s*\\('):substitute(lines[start_row], 'it.only (')
vim.api.nvim_buf_set_lines(0, start_row, start_row + 1, true, {new_line})
end
end
end
-- Map the function to a key combination (e.g., <leader>o)
vim.api.nvim_set_keymap('n', '<leader>o', '<cmd>lua toggle_only()<CR>', { noremap = true })
This code defines a Lua function toggle_only that:
Finds the current cursor position and gets the buffer lines.
Searches upwards from the current position to find the nearest it block.
Checks if the it block already has .only. If it does, it removes it; otherwise, it adds it.
The function is then mapped to the <leader>o key combination in normal mode. You can change the key combination to your liking.
Note that this implementation uses Neovim's Lua API, which is available in Neovim 0.5 and later. If you're using an earlier version, you may need to use Vimscript instead.
local function toggle_only()
-- Save the current cursor position
local winnr = vim.api.nvim_get_current_win()
local cursor_row, cursor_col = vim.api.nvim_win_get_cursor(winnr)
-- Get the buffer lines
local bufnr = vim.api.nvim_get_current_buf()
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true)
-- Find the 'it' block from the current position
local start_row = cursor_row - 1
while start_row > 0 and not vim.regex('it\\s*\\('):match_str(lines[start_row]) do
start_row = start_row - 1
end
-- Check if we found an 'it' block
if vim.regex('it\\s*\\('):match_str(lines[start_row]) then
-- Check if '.only' is already present
if vim.regex('it\\.only\\s*\\('):match_str(lines[start_row]) then
-- Remove '.only'
local new_line = vim.regex('it\\.only\\s*\\('):substitute(lines[start_row], 'it (')
vim.api.nvim_buf_set_lines(bufnr, start_row, start_row + 1, true, {new_line})
else
-- Add '.only'
local new_line = vim.regex('it\\s*\\('):substitute(lines[start_row], 'it.only (')
vim.api.nvim_buf_set_lines(bufnr, start_row, start_row + 1, true, {new_line})
end
end
-- Restore the cursor position
vim.api.nvim_win_set_cursor(winnr, cursor_row, cursor_col)
end
-- Map the function to a key combination (e.g., <leader>o)
vim.api.nvim_set_keymap('n', '<leader>o', '<cmd>lua toggle_only()<CR>', { noremap = true })
This updated code uses the vim.api.nvim_win_set_cursor function to restore the cursor position to its original location after executing the macro. It also uses Lua's vim.regex module for pattern matching, which is more efficient and powerful than Vim's built-in pattern matching.
“using neovim, i would like to write some helper that does the following: when i'm inside….” Conversation with llama-3.1-405b-instruct, from the LMArena Human Preference 100K dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m850cr582xa0p4zfc6qpqh7f