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.
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 <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>`
- Shortcut for `:LesserAssign<cr>`
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 (=, ==, !=, +=, &&=, ...)
- `<space>`
- `:`
- `,`
- `|`
1. `<Enter>` 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 (`<space>`, `=`, `:`, `.`, `|`, `,`)
Examples
--------
Alignment rules for the following delimiters have been crafted to meet the most needs.
| Delimiter | Description/Use cases |
| --------- | ---------------------------------------------------------- |
| `<space>` | 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 |
| ----------------- | -------------------------------------------------- | ----------------------- |
| `<Enter>=` | Alignment around 1st equals sign (and the likes) | `:'<,'>LesserAlign =` |
| `<Enter>2=` | Alignment around 2nd equals sign (and the likes) | `:'<,'>LesserAlign 2=` |
| `<Enter>3=` | Alignment around 3rd equals sign (and the likes) | `:'<,'>LesserAlign 3=` |
| `<Enter>*=` | Alignment around all equals signs (and the likes) | `:'<,'>LesserAlign *=` |
| `<Enter><space>` | Alignment around 1st whitespace | `:'<,'>LesserAlign \ ` |
| `<Enter>2<space>` | Alignment around 2nd whitespace | `:'<,'>LesserAlign 2\ ` |
| `<Enter>:` | Alignment around 1st colon | `:'<,'>LesserAlign :` |
| ----------------- | ------------------------------------------------- | --------------------- |
| `<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
-------------------------------
Define (or override) alignment rules.
```vim
let g:lesser_align_delimiters = {
let g:easy_align_delimiters = {
\ '/': { 'pattern': '//*' },
\ 'x': {
\ 'pattern': '[xX]',
\ 'margin_left': ' <<<',

View File

@@ -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 = {
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 }
\ '|': { '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

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>
@@ -6,36 +6,37 @@ Author: Junegunn Choi <https://github.com/junegunn>
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 <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>
- Shortcut for `:LesserAssign<cr>`
2. Integer (optional, default: 1)
With this mapping, you can align selected lines with a few keystrokes.
1. <Enter> 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
<space> General alignment around whitespaces
= Operators containing equals sign (=, ==, !=, +=, &&=, ...)
<space>
:
,
|
: 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
\ }
\ }

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
git archive -o vim-lesser-align.zip HEAD
git archive -o vim-easy-align.zip HEAD