DEV Community

Dima Portenko
Dima Portenko

Posted on

Switching Between camelCase and snake_case in Neovim using Lua

Introduction:

In this blog post, we'll demonstrate how to create a custom Lua function for Neovim that allows you to switch between camelCase and snake_case for variable names. This can be quite useful when you're working with different programming languages or projects that follow different naming conventions. We'll also show you how to create a key binding for this function, allowing you to quickly toggle between the two cases with a simple key combination.


Demo

Step 1: Create the Lua Function

To get started, create a new Lua file called switch_case.lua in your Neovim configuration directory (typically ~/.config/nvim/ or ~/.config/nvim/lua/). Add the following code to this file:



local function switch_case()
  local line, col = unpack(vim.api.nvim_win_get_cursor(0))
  local word = vim.fn.expand('<cword>')
  local word_start = vim.fn.matchstrpos(vim.fn.getline('.'), '\\k*\\%' .. (col+1) .. 'c\\k*')[2]

  -- Detect camelCase
  if word:find('[a-z][A-Z]') then
    -- Convert camelCase to snake_case
    local snake_case_word = word:gsub('([a-z])([A-Z])', '%1_%2'):lower()
    vim.api.nvim_buf_set_text(0, line - 1, word_start, line - 1, word_start + #word, {snake_case_word})
  -- Detect snake_case
  elseif word:find('_[a-z]') then
    -- Convert snake_case to camelCase
    local camel_case_word = word:gsub('(_)([a-z])', function(_, l) return l:upper() end)
    vim.api.nvim_buf_set_text(0, line - 1, word_start, line - 1, word_start + #word, {camel_case_word})
  else
    print("Not a snake_case or camelCase word")
  end
end

return { switch_case = switch_case }


Enter fullscreen mode Exit fullscreen mode

This function detects the case of the word under the cursor and converts it to the other case. The word's starting position within the line is calculated using the matchstrpos function, ensuring accurate replacement even if the cursor is in the middle of the word.

Step 2: Add the Function to Your Neovim Configuration

Now, let's add the Lua function to your Neovim configuration file (init.vim or init.lua). You'll also create a key binding for the function, allowing you to switch between camelCase and snake_case with a key combination. In this example, we'll use the <Leader>s key combination.

For init.vim:



luafile ~/.config/nvim/switch_case.lua
nnoremap <Leader>s :lua require'switch_case'.switch_case()<CR>


Enter fullscreen mode Exit fullscreen mode

For init.lua:



require'switch_case'
vim.api.nvim_set_keymap('n', '<Leader>s', '<cmd>lua require("switch_case").switch_case()<CR>', {noremap = true, silent = true})

Enter fullscreen mode Exit fullscreen mode




Step 3: Test Your New Function

With the function and key binding in place, open a file in Neovim and test it out. Place the cursor on a word in camelCase or snake_case, and press <Leader>s. The word should be converted to the other case.

Conclusion:

This blog post has demonstrated how to create a custom Lua function for Neovim to switch between camelCase and snake_case naming conventions. By incorporating this

Top comments (0)