vim-easy-align

This commit is contained in:
Junegunn Choi
2013-04-13 01:07:03 +09:00
parent 8dcba21260
commit 503c8c860f
6 changed files with 88 additions and 82 deletions

View File

@@ -1,59 +1,61 @@
vim-lesser-align vim-easy-align
================ ==============
Yet another Vim alignment plugin without too much ambition. 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, 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 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`. For convenience, it is advised that you define a mapping for triggering it in your `.vimrc`.
```vim ```vim
vnoremap <silent> <Enter> :LesserAlign<cr> vnoremap <silent> <Enter> :EasyAlign<cr>
``` ```
Then a key sequence becomes a combination of 3 parts. With the mapping, you can align selected lines with a few keystrokes.
1. `<Enter>` 1. `<Enter>` key to start EasyAlign command
- Shortcut for `:LesserAssign<cr>` 1. Optional field number (default: 1)
1. Integer (*optional*, default: 1) - `1` Alignment around 1st delimiter
- `1`: Alignment around 1st delimiter - `2` Alignment around 2nd delimiter
- `2`: Alignment around 2nd delimiter - ...
- `...` - `*` Alignment around all delimiters (recursive)
- `*`: Alignment around all delimiters (tabularize) 1. Delimiter (`<space>`, `=`, `:`, `.`, `|`, `,`)
1. Delimiter
- `=`: Operators containing equals sign (=, ==, !=, +=, &&=, ...)
- `<space>`
- `:`
- `,`
- `|`
Examples Alignment rules for the following delimiters have been crafted to meet the most needs.
--------
| With visual map | Description | Equivalent command | | Delimiter | Description/Use cases |
| ----------------- | -------------------------------------------------- | ----------------------- | | --------- | ---------------------------------------------------------- |
| `<Enter>=` | Alignment around 1st equals sign (and the likes) | `:'<,'>LesserAlign =` | | `<space>` | General alignment around spaces |
| `<Enter>2=` | Alignment around 2nd equals sign (and the likes) | `:'<,'>LesserAlign 2=` | | `=` | Operators containing equals sign (=, ==, !=, +=, &&=, ...) |
| `<Enter>3=` | Alignment around 3rd equals sign (and the likes) | `:'<,'>LesserAlign 3=` | | `:` | Suitable for formatting JSON or YAML |
| `<Enter>*=` | Alignment around all equals signs (and the likes) | `:'<,'>LesserAlign *=` | | `.` | Multi-line method chaining |
| `<Enter><space>` | Alignment around 1st whitespace | `:'<,'>LesserAlign \ ` | | `,` | Multi-line method arguments |
| `<Enter>2<space>` | Alignment around 2nd whitespace | `:'<,'>LesserAlign 2\ ` | | `|` | Table markdown |
| `<Enter>:` | Alignment around 1st colon | `:'<,'>LesserAlign :` |
| ... | ... | | ### Example command sequences
| With visual map | Description | Equivalent command |
| ----------------- | ------------------------------------------------- | --------------------- |
| `<Enter>=` | Alignment around 1st equals sign (and the likes) | `:'<,'>EasyAlign =` |
| `<Enter>2=` | Alignment around 2nd equals sign (and the likes) | `:'<,'>EasyAlign 2=` |
| `<Enter>3=` | Alignment around 3rd equals sign (and the likes) | `:'<,'>EasyAlign 3=` |
| `<Enter>*=` | Alignment around all equals signs (and the likes) | `:'<,'>EasyAlign *=` |
| `<Enter><space>` | Alignment around 1st space | `:'<,'>EasyAlign \ ` |
| `<Enter>2<space>` | Alignment around 2nd space | `:'<,'>EasyAlign 2\ ` |
| `<Enter>:` | Alignment around 1st colon | `:'<,'>EasyAlign :` |
| ... | ... | |
Defining custom alignment rules Defining custom alignment rules
------------------------------- -------------------------------
Define (or override) alignment rules.
```vim ```vim
let g:lesser_align_delimiters = { let g:easy_align_delimiters = {
\ '/': { 'pattern': '//*' },
\ 'x': { \ 'x': {
\ 'pattern': '[xX]', \ 'pattern': '[xX]',
\ 'margin_left': ' <<<', \ 'margin_left': ' <<<',

View File

@@ -1,21 +1,22 @@
if exists("g:lesser_align_loaded") if exists("g:easy_align_loaded")
finish finish
endif endif
let g:lesser_align_loaded = 1 let g:easy_align_loaded = 1
let g:lesser_align_delimiters_merged = { 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': '<=>\|\(&&\|||\|<<\|>>\)=\|=\~\|=>\|[:+/*!%^=><&|-]\?=', \ '=': { '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': 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 },
\ '.': { 'pattern': '\.', 'margin_left': '', 'margin_right': '', 'stick_to_left': 0 }
\ } \ }
if !exists("g:lesser_align_delimiters") if !exists("g:easy_align_delimiters")
let g:lesser_align_delimiters = {} let g:easy_align_delimiters = {}
endif 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) function! s:do_align(fl, ll, pattern, nth, ml, mr, stick_to_left, recursive)
let lines = {} let lines = {}
@@ -78,13 +79,13 @@ function! s:do_align(fl, ll, pattern, nth, ml, mr, stick_to_left, recursive)
endif endif
endfunction endfunction
function! lesser_align#align(...) range function! easy_align#align(...) range
let recursive = 0 let recursive = 0
let n = '' let n = ''
let ch = '' let ch = ''
if a:0 == 0 if a:0 == 0
echon "\rlesser-align ()" echon "\reasy-align ()"
while 1 while 1
let c = getchar() let c = getchar()
let ch = nr2char(c) let ch = nr2char(c)
@@ -96,14 +97,14 @@ function! lesser_align#align(...) range
return return
endif endif
let n = n . nr2char(c) let n = n . nr2char(c)
echon "\rlesser-align (". n .")" echon "\reasy-align (". n .")"
elseif ch == '*' elseif ch == '*'
if !empty(n) if !empty(n)
echon "\rField number(". n .") already specified" echon "\rField number(". n .") already specified"
return return
endif endif
let n = '*' let n = '*'
echon "\rlesser-align (*)" echon "\reasy-align (*)"
else else
break break
endif endif
@@ -133,15 +134,15 @@ function! lesser_align#align(...) range
return return
endif endif
if has_key(g:lesser_align_delimiters_merged, ch) if has_key(g:easy_align_delimiters_merged, ch)
let dict = g:lesser_align_delimiters_merged[ch] let dict = g:easy_align_delimiters_merged[ch]
call s:do_align(a:firstline, a:lastline, call s:do_align(a:firstline, a:lastline,
\ get(dict, 'pattern', ch), \ get(dict, 'pattern', ch),
\ n, \ n,
\ get(dict, 'margin_left', ' '), \ get(dict, 'margin_left', ' '),
\ get(dict, 'margin_right', ' '), \ get(dict, 'margin_right', ' '),
\ get(dict, 'stick_to_left', 0), recursive) \ get(dict, 'stick_to_left', 0), recursive)
echon "\rlesser-align (". (recursive ? '*' : n) . ch .")" echon "\reasy-align (". (recursive ? '*' : n) . ch .")"
else else
echon "\rUnknown delimiter: ". ch echon "\rUnknown delimiter: ". ch
endif endif

View File

@@ -1,4 +1,4 @@
vim-lesser-align *vim-lesser-align* *lesser-align* vim-easy-align *vim-easy-align* *easy-align*
========================================================================= =========================================================================
Author: Junegunn Choi <https://github.com/junegunn> Author: Junegunn Choi <https://github.com/junegunn>
@@ -6,36 +6,37 @@ Author: Junegunn Choi <https://github.com/junegunn>
Yet another Vim alignment plugin without too much ambition. Yet another Vim alignment plugin without too much ambition.
This plugin clearly has less features than the other pre-existing ones 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 with the similar goals, but it is simpler, easier to use,
(or even better) for the most of the cases. 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. 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.
vnoremap <silent> <Enter> :LesserAlign<cr> 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 <silent> <Enter> :EasyAlign<cr>
1. <Enter> With this mapping, you can align selected lines with a few keystrokes.
- Shortcut for `:LesserAssign<cr>`
2. Integer (optional, default: 1) 1. <Enter> key to start EasyAlign command
1 Alignment around 1st delimiter 2. Optional field number (default: 1)
2 Alignment around 2nd delimiter 1 Alignment around 1st delimiter
2 Alignment around 2nd delimiter
... ...
* Alignment around all delimiters (tabularize) * Alignment around all delimiters (recursive)
3. Delimiter 3. Delimiter
= Operators containing equals sign (=, ==, !=, +=, &&=, ...) <space> General alignment around whitespaces
<space> = Operators containing equals sign (=, ==, !=, +=, &&=, ...)
: : Suitable for formatting JSON or YAML
, . Multi-line method chaining
| , Multi-line method arguments. CSV.
| Table markdown
Examples: Examples:
@@ -51,7 +52,8 @@ Examples:
Defining custom alignment rules Defining custom alignment rules
------------------------------------------------------------------------- -------------------------------------------------------------------------
let g:lesser_align_delimiters = { let g:easy_align_delimiters = {
\ '/': { 'pattern': '//*' },
\ 'x': { \ 'x': {
\ 'pattern': '[xX]', \ 'pattern': '[xX]',
\ 'margin_left': ' <<<', \ 'margin_left': ' <<<',
@@ -59,3 +61,4 @@ Defining custom alignment rules
\ 'stick_to_left': 0 \ 'stick_to_left': 0
\ } \ }
\ } \ }

6
plugin/easy_align.vim Normal file
View File

@@ -0,0 +1,6 @@
if exists("g:easy_align_plugin_loaded")
finish
endif
let g:easy_align_plugin_loaded = 1
command! -nargs=* -range EasyAlign <line1>,<line2>call easy_align#align(<f-args>)

View File

@@ -1,6 +0,0 @@
if exists("g:lesser_align_plugin_loaded")
finish
endif
let g:lesser_align_plugin_loaded = 1
command! -nargs=* -range LesserAlign <line1>,<line2>call lesser_align#align(<f-args>)

2
zip
View File

@@ -1,2 +1,2 @@
#!/bin/sh #!/bin/sh
git archive -o vim-lesser-align.zip HEAD git archive -o vim-easy-align.zip HEAD