Skip to content

Instantly share code, notes, and snippets.

@othree
Created May 27, 2013 07:08
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save othree/5655583 to your computer and use it in GitHub Desktop.
Save othree/5655583 to your computer and use it in GitHub Desktop.
Switch between different case type: camelCase -> MixedCase -> snake_case -> UPPER_CASE -> dash-case -> camelCase
let g:switch_custom_definitions =
\ [
\ {
\ '\<\(\l\)\(\l\+\(\u\l\+\)\+\)\>': '\=toupper(submatch(1)) . submatch(2)',
\ '\<\(\u\l\+\)\(\u\l\+\)\+\>': "\\=tolower(substitute(submatch(0), '\\(\\l\\)\\(\\u\\)', '\\1_\\2', 'g'))",
\ '\<\(\l\+\)\(_\l\+\)\+\>': '\U\0',
\ '\<\(\u\+\)\(_\u\+\)\+\>': "\\=tolower(substitute(submatch(0), '_', '-', 'g'))",
\ '\<\(\l\+\)\(-\l\+\)\+\>': "\\=substitute(submatch(0), '-\\(\\l\\)', '\\u\\1', 'g')",
\ }
\ ]
@mosheavni
Copy link

lua adaptation using lazy-nvim:

{
    'AndrewRadev/switch.vim',
    keys = {
      { 'gs', nil, { 'n', 'v' } },
    },
    config = function()
      local fk = [=[\<\(\l\)\(\l\+\(\u\l\+\)\+\)\>]=]
      local fv = [=[\=toupper(submatch(1)) . submatch(2)]=]
      local sk = [=[\<\(\u\l\+\)\(\u\l\+\)\+\>]=]
      local sv = [=[\=tolower(substitute(submatch(0), '\(\l\)\(\u\)', '\1_\2', 'g'))]=]
      local tk = [=[\<\(\l\+\)\(_\l\+\)\+\>]=]
      local tv = [=[\U\0]=]
      local fok = [=[\<\(\u\+\)\(_\u\+\)\+\>]=]
      local fov = [=[\=tolower(substitute(submatch(0), '_', '-', 'g'))]=]
      local fik = [=[\<\(\l\+\)\(-\l\+\)\+\>]=]
      local fiv = [=[\=substitute(submatch(0), '-\(\l\)', '\u\1', 'g')]=]
      vim.g['switch_custom_definitions'] = {
        vim.fn['switch#NormalizedCaseWords'] { 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday' },
        vim.fn['switch#NormalizedCase'] { 'yes', 'no' },
        vim.fn['switch#NormalizedCase'] { 'on', 'off' },
        vim.fn['switch#NormalizedCase'] { 'left', 'right' },
        vim.fn['switch#NormalizedCase'] { 'up', 'down' },
        vim.fn['switch#NormalizedCase'] { 'enable', 'disable' },
        { '==', '!=' },
        {
          [fk] = fv,
          [sk] = sv,
          [tk] = tv,
          [fok] = fov,
          [fik] = fiv,
        },
      }
    end,
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment