Skip to content

Instantly share code, notes, and snippets.

@g0xA52A2A
Last active November 30, 2020 18:15
Show Gist options
  • Select an option

  • Save g0xA52A2A/a03b05bcff4092277f093de3aa713bf8 to your computer and use it in GitHub Desktop.

Select an option

Save g0xA52A2A/a03b05bcff4092277f093de3aa713bf8 to your computer and use it in GitHub Desktop.

Revisions

  1. @George-B George-B revised this gist Nov 30, 2020. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions Vim_cabbrev.md
    Original file line number Diff line number Diff line change
    @@ -19,8 +19,7 @@ function! Cabbrev(command, local) abort
    let rhs .= "'" . lowercase . "'"
    if !hasmapto(rhs, 'c', 1)
    let args = (a:local) ? '<buffer> <expr>' :'<expr>'
    execute 'cnoreabbrev' args lowercase rhs
    execute 'cnoreabbrev' (a:local ? '<buffer>' : '') '<expr>' lowercase rhs
    endif
    endfunction
  2. @George-B George-B revised this gist Oct 6, 2020. 1 changed file with 25 additions and 11 deletions.
    36 changes: 25 additions & 11 deletions Vim_cabbrev.md
    Original file line number Diff line number Diff line change
    @@ -6,17 +6,31 @@ to have it replaced with `:Grep` for the sake of muscle memory.
    For other user commands a similar replacement scheme may be desirable
    simply to avoid having to chord with the shift key.

    We can achieve this with command-line abbreviations. Using the following
    in somewhere like `~/.vim/after/plugin/cabbrev.vim` gives us that in
    a very generic fashion.
    We can achieve this with command-line abbreviations.

    ```vim
    for command in getcompletion('[A-Z]', 'command')
    let lowercase = tolower(command)
    execute "cnoreabbrev <expr> " . lowercase
    \ "(getcmdtype() ==# ':' && getcmdline() ==# '" . lowercase . "') ?"
    \ "'" . command . "'"
    \ ":"
    \ "'" . lowercase . "'"
    endfor
    function! Cabbrev(command, local) abort
    let lowercase = tolower(a:command)
    let rhs = "(getcmdtype() ==# ':' && getcmdline() ==# '" . lowercase . "')"
    let rhs .= " ? "
    let rhs .= "'" . a:command . "'"
    let rhs .= " : "
    let rhs .= "'" . lowercase . "'"
    if !hasmapto(rhs, 'c', 1)
    let args = (a:local) ? '<buffer> <expr>' :'<expr>'
    execute 'cnoreabbrev' args lowercase rhs
    endif
    endfunction
    augroup AutoAbbreviate
    autocmd!
    autocmd VimEnter *
    \ call map(getcompletion('[A-Z]', 'command'),
    \ "Cabbrev(v:val, 0)")
    autocmd FileType *
    \ call map(getcompletion('[A-Z]', 'command'),
    \ "Cabbrev(v:val, 1)")
    augroup END
    ```
  3. @George-B George-B created this gist Jun 18, 2020.
    22 changes: 22 additions & 0 deletions Vim_cabbrev.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    Some user commands are intended as replacements for built-in commands,
    for example `:Grep` may replace `:grep`. Given the existence of such
    a "replacement" user command when typing `:grep` it would be convenient
    to have it replaced with `:Grep` for the sake of muscle memory.

    For other user commands a similar replacement scheme may be desirable
    simply to avoid having to chord with the shift key.

    We can achieve this with command-line abbreviations. Using the following
    in somewhere like `~/.vim/after/plugin/cabbrev.vim` gives us that in
    a very generic fashion.

    ```vim
    for command in getcompletion('[A-Z]', 'command')
    let lowercase = tolower(command)
    execute "cnoreabbrev <expr> " . lowercase
    \ "(getcmdtype() ==# ':' && getcmdline() ==# '" . lowercase . "') ?"
    \ "'" . command . "'"
    \ ":"
    \ "'" . lowercase . "'"
    endfor
    ```