mirror of
https://github.com/junegunn/vim-easy-align.git
synced 2025-11-08 09:53:51 -05:00
vim-easy-align
This commit is contained in:
72
README.md
72
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 <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.
|
||||
|
||||
| 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 :` |
|
||||
| ... | ... | |
|
||||
| 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) | `:'<,'>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': ' <<<',
|
||||
|
||||
@@ -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
|
||||
@@ -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)
|
||||
1 Alignment around 1st delimiter
|
||||
2 Alignment around 2nd delimiter
|
||||
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
|
||||
= Operators containing equals sign (=, ==, !=, +=, &&=, ...)
|
||||
<space>
|
||||
:
|
||||
,
|
||||
|
|
||||
<space> 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
|
||||
\ }
|
||||
\ }
|
||||
|
||||
6
plugin/easy_align.vim
Normal file
6
plugin/easy_align.vim
Normal 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>)
|
||||
@@ -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>)
|
||||
Reference in New Issue
Block a user