From 503c8c860fbb3f52d813332a9f2564d63ba9a8b1 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sat, 13 Apr 2013 01:07:03 +0900 Subject: [PATCH] vim-easy-align --- README.md | 72 ++++++++++--------- autoload/{lesser_align.vim => easy_align.vim} | 37 +++++----- doc/{lesser_align.txt => easy_align.txt} | 47 ++++++------ plugin/easy_align.vim | 6 ++ plugin/lesser_align.vim | 6 -- zip | 2 +- 6 files changed, 88 insertions(+), 82 deletions(-) rename autoload/{lesser_align.vim => easy_align.vim} (75%) rename doc/{lesser_align.txt => easy_align.txt} (50%) create mode 100644 plugin/easy_align.vim delete mode 100644 plugin/lesser_align.vim diff --git a/README.md b/README.md index a91bad5..f13e773 100644 --- a/README.md +++ b/README.md @@ -1,59 +1,61 @@ -vim-lesser-align -================ +vim-easy-align +============== Yet another Vim alignment plugin without too much ambition. This plugin clearly has less features than the other pre-existing ones with the similar goals, -but it is simpler, easier to use, and good enough (or even better) for the most of the cases. +but it is simpler, easier to use, and just good enough for the most of the cases. Usage ----- -Vim-lesser-align defines `LesserAlign` command in the visual mode. - +Vim-easy-align defines `:EasyAlign` command in the visual mode. For convenience, it is advised that you define a mapping for triggering it in your `.vimrc`. ```vim -vnoremap :LesserAlign +vnoremap :EasyAlign ``` -Then a key sequence becomes a combination of 3 parts. +With the mapping, you can align selected lines with a few keystrokes. -1. `` - - Shortcut for `:LesserAssign` -1. Integer (*optional*, default: 1) - - `1`: Alignment around 1st delimiter - - `2`: Alignment around 2nd delimiter - - `...` - - `*`: Alignment around all delimiters (tabularize) -1. Delimiter - - `=`: Operators containing equals sign (=, ==, !=, +=, &&=, ...) - - `` - - `:` - - `,` - - `|` +1. `` key to start EasyAlign command +1. Optional field number (default: 1) + - `1` Alignment around 1st delimiter + - `2` Alignment around 2nd delimiter + - ... + - `*` Alignment around all delimiters (recursive) +1. Delimiter (``, `=`, `:`, `.`, `|`, `,`) -Examples --------- +Alignment rules for the following delimiters have been crafted to meet the most needs. -| With visual map | Description | Equivalent command | -| ----------------- | -------------------------------------------------- | ----------------------- | -| `=` | Alignment around 1st equals sign (and the likes) | `:'<,'>LesserAlign =` | -| `2=` | Alignment around 2nd equals sign (and the likes) | `:'<,'>LesserAlign 2=` | -| `3=` | Alignment around 3rd equals sign (and the likes) | `:'<,'>LesserAlign 3=` | -| `*=` | Alignment around all equals signs (and the likes) | `:'<,'>LesserAlign *=` | -| `` | Alignment around 1st whitespace | `:'<,'>LesserAlign \ ` | -| `2` | Alignment around 2nd whitespace | `:'<,'>LesserAlign 2\ ` | -| `:` | Alignment around 1st colon | `:'<,'>LesserAlign :` | -| ... | ... | | +| Delimiter | Description/Use cases | +| --------- | ---------------------------------------------------------- | +| `` | General alignment around spaces | +| `=` | Operators containing equals sign (=, ==, !=, +=, &&=, ...) | +| `:` | Suitable for formatting JSON or YAML | +| `.` | Multi-line method chaining | +| `,` | Multi-line method arguments | +| `|` | Table markdown | + +### Example command sequences + +| With visual map | Description | Equivalent command | +| ----------------- | ------------------------------------------------- | --------------------- | +| `=` | Alignment around 1st equals sign (and the likes) | `:'<,'>EasyAlign =` | +| `2=` | Alignment around 2nd equals sign (and the likes) | `:'<,'>EasyAlign 2=` | +| `3=` | Alignment around 3rd equals sign (and the likes) | `:'<,'>EasyAlign 3=` | +| `*=` | Alignment around all equals signs (and the likes) | `:'<,'>EasyAlign *=` | +| `` | Alignment around 1st space | `:'<,'>EasyAlign \ ` | +| `2` | Alignment around 2nd space | `:'<,'>EasyAlign 2\ ` | +| `:` | Alignment around 1st colon | `:'<,'>EasyAlign :` | +| ... | ... | | Defining custom alignment rules ------------------------------- -Define (or override) alignment rules. - ```vim -let g:lesser_align_delimiters = { +let g:easy_align_delimiters = { +\ '/': { 'pattern': '//*' }, \ 'x': { \ 'pattern': '[xX]', \ 'margin_left': ' <<<', diff --git a/autoload/lesser_align.vim b/autoload/easy_align.vim similarity index 75% rename from autoload/lesser_align.vim rename to autoload/easy_align.vim index 09cf5a8..e7612a8 100644 --- a/autoload/lesser_align.vim +++ b/autoload/easy_align.vim @@ -1,21 +1,22 @@ -if exists("g:lesser_align_loaded") +if exists("g:easy_align_loaded") finish endif -let g:lesser_align_loaded = 1 -let g:lesser_align_delimiters_merged = { -\ ' ': { 'pattern': ' ', 'margin_left': '', 'margin_right': '', 'stick_to_left': 0 }, +let g:easy_align_loaded = 1 +let g:easy_align_delimiters_merged = { +\ ' ': { 'pattern': ' ', 'margin_left': '', 'margin_right': '', 'stick_to_left': 0 }, \ '=': { 'pattern': '<=>\|\(&&\|||\|<<\|>>\)=\|=\~\|=>\|[:+/*!%^=><&|-]\?=', -\ 'margin_left': ' ', 'margin_right': ' ', 'stick_to_left': 0 }, -\ ':': { 'pattern': ':', 'margin_left': '', 'margin_right': ' ', 'stick_to_left': 1 }, -\ ',': { 'pattern': ',', 'margin_left': '', 'margin_right': ' ', 'stick_to_left': 1 }, -\ '|': { 'pattern': '|', 'margin_left': ' ', 'margin_right': ' ', 'stick_to_left': 0 } +\ 'margin_left': ' ', 'margin_right': ' ', 'stick_to_left': 0 }, +\ ':': { 'pattern': ':', 'margin_left': '', 'margin_right': ' ', 'stick_to_left': 1 }, +\ ',': { 'pattern': ',', 'margin_left': '', 'margin_right': ' ', 'stick_to_left': 1 }, +\ '|': { 'pattern': '|', 'margin_left': ' ', 'margin_right': ' ', 'stick_to_left': 0 }, +\ '.': { 'pattern': '\.', 'margin_left': '', 'margin_right': '', 'stick_to_left': 0 } \ } -if !exists("g:lesser_align_delimiters") - let g:lesser_align_delimiters = {} +if !exists("g:easy_align_delimiters") + let g:easy_align_delimiters = {} endif -call extend(g:lesser_align_delimiters_merged, g:lesser_align_delimiters) +call extend(g:easy_align_delimiters_merged, g:easy_align_delimiters) function! s:do_align(fl, ll, pattern, nth, ml, mr, stick_to_left, recursive) let lines = {} @@ -78,13 +79,13 @@ function! s:do_align(fl, ll, pattern, nth, ml, mr, stick_to_left, recursive) endif endfunction -function! lesser_align#align(...) range +function! easy_align#align(...) range let recursive = 0 let n = '' let ch = '' if a:0 == 0 - echon "\rlesser-align ()" + echon "\reasy-align ()" while 1 let c = getchar() let ch = nr2char(c) @@ -96,14 +97,14 @@ function! lesser_align#align(...) range return endif let n = n . nr2char(c) - echon "\rlesser-align (". n .")" + echon "\reasy-align (". n .")" elseif ch == '*' if !empty(n) echon "\rField number(". n .") already specified" return endif let n = '*' - echon "\rlesser-align (*)" + echon "\reasy-align (*)" else break endif @@ -133,15 +134,15 @@ function! lesser_align#align(...) range return endif - if has_key(g:lesser_align_delimiters_merged, ch) - let dict = g:lesser_align_delimiters_merged[ch] + if has_key(g:easy_align_delimiters_merged, ch) + let dict = g:easy_align_delimiters_merged[ch] call s:do_align(a:firstline, a:lastline, \ get(dict, 'pattern', ch), \ n, \ get(dict, 'margin_left', ' '), \ get(dict, 'margin_right', ' '), \ get(dict, 'stick_to_left', 0), recursive) - echon "\rlesser-align (". (recursive ? '*' : n) . ch .")" + echon "\reasy-align (". (recursive ? '*' : n) . ch .")" else echon "\rUnknown delimiter: ". ch endif diff --git a/doc/lesser_align.txt b/doc/easy_align.txt similarity index 50% rename from doc/lesser_align.txt rename to doc/easy_align.txt index c9f8354..522feb5 100644 --- a/doc/lesser_align.txt +++ b/doc/easy_align.txt @@ -1,4 +1,4 @@ -vim-lesser-align *vim-lesser-align* *lesser-align* +vim-easy-align *vim-easy-align* *easy-align* ========================================================================= Author: Junegunn Choi @@ -6,36 +6,37 @@ Author: Junegunn Choi Yet another Vim alignment plugin without too much ambition. This plugin clearly has less features than the other pre-existing ones -with the similar goals, but it is simpler, easier to use, and good enough -(or even better) for the most of the cases. +with the similar goals, but it is simpler, easier to use, +and just good enough for the most of the cases. -https://github.com/junegunn/vim-lesser-align +https://github.com/junegunn/vim-easy-align -LesserAlign *LesserAlign* +EasyAlign *EasyAlign* ------------------------------------------------------------------------- -Vim-lesser-align defines `LesserAlign` command in the visual mode. -For convenience, it is advised that you define a mapping for triggering -it in your .vimrc. +Vim-easy-align defines `:EasyAlign` command in the visual mode. - vnoremap :LesserAlign +For convenience, it is advised that you define a mapping for triggering it +in your `.vimrc`. -Then a key sequence becomes a combination of 3 parts. + vnoremap :EasyAlign -1. - - Shortcut for `:LesserAssign` -2. Integer (optional, default: 1) - 1 Alignment around 1st delimiter - 2 Alignment around 2nd delimiter +With this mapping, you can align selected lines with a few keystrokes. + +1. key to start EasyAlign command +2. Optional field number (default: 1) + 1 Alignment around 1st delimiter + 2 Alignment around 2nd delimiter ... - * Alignment around all delimiters (tabularize) + * Alignment around all delimiters (recursive) 3. Delimiter - = Operators containing equals sign (=, ==, !=, +=, &&=, ...) - - : - , - | + General alignment around whitespaces + = Operators containing equals sign (=, ==, !=, +=, &&=, ...) + : Suitable for formatting JSON or YAML + . Multi-line method chaining + , Multi-line method arguments. CSV. + | Table markdown Examples: @@ -51,7 +52,8 @@ Examples: Defining custom alignment rules ------------------------------------------------------------------------- - let g:lesser_align_delimiters = { + let g:easy_align_delimiters = { + \ '/': { 'pattern': '//*' }, \ 'x': { \ 'pattern': '[xX]', \ 'margin_left': ' <<<', @@ -59,3 +61,4 @@ Defining custom alignment rules \ 'stick_to_left': 0 \ } \ } + diff --git a/plugin/easy_align.vim b/plugin/easy_align.vim new file mode 100644 index 0000000..d1c743d --- /dev/null +++ b/plugin/easy_align.vim @@ -0,0 +1,6 @@ +if exists("g:easy_align_plugin_loaded") + finish +endif +let g:easy_align_plugin_loaded = 1 + +command! -nargs=* -range EasyAlign ,call easy_align#align() diff --git a/plugin/lesser_align.vim b/plugin/lesser_align.vim deleted file mode 100644 index 66b034e..0000000 --- a/plugin/lesser_align.vim +++ /dev/null @@ -1,6 +0,0 @@ -if exists("g:lesser_align_plugin_loaded") - finish -endif -let g:lesser_align_plugin_loaded = 1 - -command! -nargs=* -range LesserAlign ,call lesser_align#align() diff --git a/zip b/zip index a3bda6e..9f49924 100755 --- a/zip +++ b/zip @@ -1,2 +1,2 @@ #!/bin/sh -git archive -o vim-lesser-align.zip HEAD +git archive -o vim-easy-align.zip HEAD