Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,20 @@ Plug 'arthurxavierx/vim-caser'

All mappings below must be followed by a motion or a text object, or be applied within visual mode.

Case | Mapping
------|---------
`MixedCase` or `PascalCase` | `gsm` or `gsp`
`camelCase` | `gsc`
`snake_case` | `gs_`
`UPPER_CASE` | `gsu` or `gsU`
`Title Case` | `gst`
`Sentence case` | `gss`
`space case` | `gs<space>`
`dash-case` or `kebab-case` | `gs-` or `gsk`
`dot.case` | `gs.`

The `gs` prefix can be changed through the option `g:caser_prefix`.
Case | Default Mapping | Plug Mapping (normal/visual)
------|-----------------|------------------------------
`MixedCase` or `PascalCase` | `gsm` or `gsp` | `<Plug>CaserMixedCase`/`<Plug>CaserVMixedCase`
`camelCase` | `gsc` | `<Plug>CaserCamelCase`/`<Plug>CaserVCamelCase`
`snake_case` | `gs_` | `<Plug>CaserSnakeCase`/`<Plug>CaserVSnakeCase`
`UPPER_CASE` | `gsu` or `gsU` | `<Plug>CaserUpperCase`/`<Plug>CaserVUpperCase`
`Title Case` | `gst` | `<Plug>CaserTitleCase`/`<Plug>CaserVTitleCase`
`Sentence case` | `gss` | `<Plug>CaserSentenceCase`/`<Plug>CaserVSentenceCase`
`space case` | `gs<space>` | `<Plug>CaserSpaceCase`/`<Plug>CaserVSpaceCase`
`dash-case` or `kebab-case` | `gs-` or `gsk` | `<Plug>CaserKebabCase`/`<Plug>CaserVKebabCase`
`Title-Dash-Case` or `Title-Kebab-Case` | `gsK` | `<Plug>CaserTitleKebabCase`/`<Plug>CaserVTitleKebabCase`
`dot.case` | `gs.` | `<Plug>CaserDotCase`/`<Plug>CaserVDotCase`

The `gs` prefix can be changed through the option `g:caser_prefix`. Alternatively, the default mappings can be disabled by setting `g:caser_no_mappings`.

### Limitations
Currently `vim-caser` only supports the casing options displayed in the table above. If you would like to have another casing option in `vim-caser`, please feel free to create an issue in this repository, or to submit a pull request with the feature properly documented.
Expand All @@ -48,3 +49,4 @@ Currently `vim-caser` only supports the casing options displayed in the table ab
Name | Default | Description
------|---------|-------------
`g:caser_prefix` | `gs` | The prefix of all mappings created by `vim-caser`. E.g.: `gsc`, `gs-`, `gsu`, etc.
`g:caser_no_mappings` | not set | Set to `1` to disable default mappings. Custom mappings can be set using the `<Plug>` mappings.
16 changes: 16 additions & 0 deletions autoload/caser.vim
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ function! caser#KebabCase(word)
return substitute(caser#SnakeCase(a:word), '_', '-', 'g')
endfunction

function! caser#TitleKebabCase(word)
return substitute(caser#TitleCase(a:word), ' ', '-', 'g')
endfunction

function! caser#SpaceCase(word)
return substitute(caser#SnakeCase(a:word), '_', ' ', 'g')
endfunction
Expand All @@ -45,3 +49,15 @@ endfunction
function! caser#DotCase(word)
return substitute(caser#SnakeCase(a:word), '_', '.', 'g')
endfunction

function! caser#KebabCapsCase(word)
return toupper(caser#KebabCase(a:word))
endfunction

function! caser#DotCapsCase(word)
return toupper(caser#DotCase(a:word))
endfunction

function! caser#SpaceCapsCase(word)
return toupper(caser#SpaceCase(a:word))
endfunction
135 changes: 131 additions & 4 deletions doc/caser.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ allows you to select with a |movement|, a |text-object| or |Visual-mode|.

------------------------------------------------------------------------------

DEFAULT MAPPINGS *caser-defaults*

gsp{motion} *gsp* *caser-pascal*
gsm{motion} *gsm* *caser-mixed*
{Visual}gsp Make {motion} or highlighted text `PascalCase`/`MixedCase`
Expand All @@ -33,33 +35,158 @@ gst{motion} *gst* *caser-title*
{Visual}gst Make {motion} or highlighted text `Title Case`
(for {Visual} see |Visual-mode|).

gss{motion} *gss* *caser-sentence*
gss{motion} *gss* *caser-sentence*
{Visual}gss Make {motion} or highlighted text `Sentence case`
(for {Visual} see |Visual-mode|).

gs<space>{motion} *gs<space>* *caser-space*
gs<space>{motion} *gs<space>* *caser-space*
{Visual}gs<space> Make {motion} or highlighted text `space case`
(for {Visual} see |Visual-mode|).

gs-{motion} *gs-* *caser-dash*
gsk{motion} *gsk* *caser-kebab*
gsK{motion} *gsK* *caser-title-kebab*
{Visual}gs- Make {motion} or highlighted text `dash-case`/`kebab-case`
{Visual}gsk (for {Visual} see |Visual-mode|).
{Visual}gsK

gs.{motion} *gs.* *caser-dot*
{Visual}gs. Make {motion} or highlighted text `dot.case`
(for {Visual} see |Visual-mode|).

------------------------------------------------------------------------------

OPTIONS *caser-options*
MAPPINGS *caser-mappings*

Each default mapping corresponds to a |<Plug>| mapping which can be used to set
custom mappings. For example |<Plug>CaserCamelCase| corresponds to the
|caser-mixed| default normal mode binding 'gsc{motion}', while
|<Plug>CaserVCamelCase| corresponds to the default visual mode binding
'{Visual}gsc'.

<Plug>CaserMixedCase *<Plug>CaserMixedCase*
<Plug>CaserVMixedCase *<Plug>CaserVMixedCase*
Make {motion} or highlighted text `MixedCase`.

<Plug>CaserCamelCase *<Plug>CaserCamelCase*
<Plug>CaserVCamelCase *<Plug>CaserVCamelCase*
Make {motion} or highlighted text `camelCase`.

<Plug>CaserSnakeCase *<Plug>CaserSnakeCase*
<Plug>CaserVSnakeCase *<Plug>CaserVSnakeCase*
Make {motion} or highlighted text `snake_case`.

<Plug>CaserUpperCase *<Plug>CaserUpperCase*
<Plug>CaserVUpperCase *<Plug>CaserVUpperCase*
Make {motion} or highlighted text `UPPER_CASE`.

<Plug>CaserTitleCase *<Plug>CaserTitleCase*
<Plug>CaserVTitleCase *<Plug>CaserVTitleCase*
Make {motion} or highlighted text `Title Case`.

<Plug>CaserSentenceCase *<Plug>CaserSentenceCase*
<Plug>CaserVSentenceCase *<Plug>CaserVSentenceCase*
Make {motion} or highlighted text `Sentence case`.

<Plug>CaserSpaceCase *<Plug>CaserSpaceCase*
<Plug>CaserVSpaceCase *<Plug>CaserVSpaceCase*
Make {motion} or highlighted text `space case`.

<Plug>CaserKebabCase *<Plug>CaserKebabCase*
<Plug>CaserVKebabCase *<Plug>CaserVKebabCase*
Make {motion} or highlighted text `kebab-case`.

<Plug>CaserTitleKebabCase *<Plug>CaserTitleKebabCase*
<Plug>CaserVTitleKebabCase *<Plug>CaserVTitleKebabCase*
Make {motion} or highlighted text `Kebab-Case`.

<Plug>CaserDotCase *<Plug>CaserDotCase*
<Plug>CaserVDotCase *<Plug>CaserVDotCase*
Make {motion} or highlighted text `dot.case`.

In addition to mappings that correspond to default bindings, a few mappings
have no default but can still be used in custom mappings.

<Plug>CaserSpaceCapsCase *<Plug>CaserSpaceCapsCase*
<Plug>CaserVSpaceCapsCase *<Plug>CaserVSpaceCapsCase*
Make {motion} or highlighted text `SPACE CASE`.

<Plug>CaserKebabCapsCase *<Plug>CaserKebabCapsCase*
<Plug>CaserVKebabCapsCase *<Plug>CaserVKebabCapsCase*
Make {motion} or highlighted text `KEBAB-CASE`.

<Plug>CaserDotCapsCase *<Plug>CaserDotCapsCase*
<Plug>CaserVDotCapsCase *<Plug>CaserVDotCapsCase*
Make {motion} or highlighted text `DOT.CASE`.

------------------------------------------------------------------------------

OPTIONS *caser-options*

*g:caser_prefix*
*g:caser_prefix*
g:caser_prefix Allows for customizing the prefix used in all
|vim-caser| mappings. Example: to use 'gc' as the
prefix -- making commands such as 'gcs' to convert text
text to `snake_case`: >
:let g:caser_prefix = 'gc'

*g:caser_custom_mappings*
g:caser_custom_mappings Override the default mappings for specific conversions.
Defined as a dictionary with CamelCase keys for each
conversion.

For context, the default mappings are:

let s:caser_default_mappings = {
\ 'MixedCase': ['m', 'p'],
\ 'CamelCase': ['c'],
\ 'SnakeCase': ['_'],
\ 'UpperCase': ['u', 'U'],
\ 'TitleCase': ['t'],
\ 'SentenceCase': ['s'],
\ 'SpaceCase': ['<space>'],
\ 'SpaceCapsCase': [],
\ 'KebabCase': ['k', '-'],
\ 'TitleKebabCase': ['K'],
\ 'KebabCapsCase': [],
\ 'DotCase': ['.'],
\ 'DotCapsCase': []
\ }

|g:caser_custom_mappings| can be used to selectively
override mappings for some conversions, while leaving
others unchanged.

Example: We override the mappings for `CamelCase`,
`MixedCase`, `SnakeCase`, and `UpperCase`. We keep the
defaults for `KebabCase` and `DotCase`, and disable all
other mappings: >

let g:caser_custom_mappings = {
\ 'MixedCase': ['C'],
\ 'CamelCase': ['c'],
\ 'SnakeCase': ['s'],
\ 'UpperCase': ['S'],
\ 'TitleCase': [],
\ 'SentenceCase': [],
\ 'SpaceCase': [],
\ 'SpaceCapsCase': [],
\ 'TitleKebabCase': [],
\ 'KebabCapsCase': [],
\ 'DotCapsCase': []
\ }


*g:caser_no_mappings*
g:caser_no_mappings Disable default mappings. The value of |g:caser_prefix|
is ignored. See |caser-mappings| for setting custom
mappings. Example: to use 'c' for `camelCase` and 'C'
for `MixedCase`, but not set any other mappings: >
:let g:caser_no_mappings = 1
:nmap gsc <Plug>CaserCamelCase
:xmap gsc <Plug>CaserVCamelCase
:nmap gsC <Plug>CaserMixedCase
:xmap gsC <Plug>CaserVMixedCase

==============================================================================
vim:tw=78:ts=2:sts=2:sw=2:ft=help:norl:
43 changes: 31 additions & 12 deletions plugin/caser.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ if !exists('g:caser_prefix')
let g:caser_prefix = 'gs'
endif

let s:caser_default_mappings = {
\ 'MixedCase': ['m', 'p'],
\ 'CamelCase': ['c'],
\ 'SnakeCase': ['_'],
\ 'UpperCase': ['u', 'U'],
\ 'TitleCase': ['t'],
\ 'SentenceCase': ['s'],
\ 'SpaceCase': ['<space>'],
\ 'SpaceCapsCase': [],
\ 'KebabCase': ['k', '-'],
\ 'TitleKebabCase': ['K'],
\ 'KebabCapsCase': [],
\ 'DotCase': ['.'],
\ 'DotCapsCase': []
\ }

if !exists('g:caser_custom_mappings')
let g:caser_custom_mappings = {}
endif

" }}}

" Setup {{{
Expand Down Expand Up @@ -74,12 +94,11 @@ endfunction
function! s:ActionSetup(fn)
let s:encode_fn = a:fn
let &opfunc = matchstr(expand('<sfile>'), '<SNR>\d\+_').'ActionOpfunc'
return 'g@'
endfunction

function! s:MapAction(fn, keys)
exe 'nnoremap <expr> <Plug>Caser'.a:fn.' <SID>ActionSetup("'.a:fn.'")'
exe 'xnoremap <expr> <Plug>CaserV'.a:fn.' <SID>DoAction("'.a:fn.'",visualmode())'
exe 'nnoremap <silent> <Plug>Caser'.a:fn.' :<C-U>call <SID>ActionSetup("'.a:fn.'")<CR>g@'
exe 'xnoremap <silent> <Plug>CaserV'.a:fn.' :<C-U>call <SID>DoAction("'.a:fn.'",visualmode())<CR>'

if !exists('g:caser_no_mappings') || !g:caser_no_mappings
for key in a:keys
Expand All @@ -91,12 +110,12 @@ endfunction

" }}}

call s:MapAction('MixedCase', ['m', 'p'])
call s:MapAction('CamelCase', ['c'])
call s:MapAction('SnakeCase', ['_'])
call s:MapAction('UpperCase', ['u', 'U'])
call s:MapAction('TitleCase', ['t'])
call s:MapAction('SentenceCase', ['s'])
call s:MapAction('SpaceCase', ['<space>'])
call s:MapAction('KebabCase', ['k', '-'])
call s:MapAction('DotCase', ['.'])
function! s:MapAllActions()
for pair in items(s:caser_default_mappings)
let fn = pair[0]
let keys = get(g:caser_custom_mappings, fn, pair[1])
call s:MapAction(fn, keys)
endfor
endfunction

call s:MapAllActions()