sponsor Vim development Vim logo Vim Book Ad

right_align : Right alignment of string under cursor

 script karma  Rating -1/3, Downloaded by 3451  Comments, bugs, improvements  Vim wiki

created by
Alexey Radkov
script type
This script can be used to align the line under cursor to the right border. The value of the right border column can be specified using global varialbe g:RightAlign_RightBorder in your .vimrc file, for example:

let g:RightAlign_RightBorder = 80

If g:RightAlign_RightBorder is not set then value of option textwidth will be used. Alignment to the right border means that the right edge of the string will not exceed the specified value but will be as close as possible according to your shiftwidth and shiftround settings. For example if shiftwidth is 4, shiftround is set, g:RightAlign_RightBorder is 80 and length of the current line is 14, then first non-space character of this line will be moved to 65th column. This behaviour differs from that of builtin command :right.

Since version 0.5 the shiftround behaviour is active even If you did not set option shiftround. This is due to default value of variable g:RightAlign_ShiftRound is equal 1. If you do not want shiftround behaviour then put in your .vimrc line

let g:RightAlign_ShiftRound = 0

The script provides single command RightAlign with an optional argument. The optional argument can be set to 'kc' in order to make script function keep cursor on the same character of the line when moving it: this is especially useful in insert mode.

To use script efficiently you can choose to use mappings in insert and normal mode, for example

imap <silent> <C-b>    <Plug>RightAlign
nmap <silent> <C-k>b    :RightAlign<CR>
vmap <silent> <C-k>b    :RightAlign<CR>

Now when you press ^b in insert mode the line under cursor will be aligned to the right according to values of the g:RightAlign_RightBorder or textwidth, cursor position will not change in relation to the content of the string. This mapping can be regarded as addition to existing commands ^d, ^t and ^f in insert mode. The second and third mappings enable RightAlign in normal and visual modes respectively when pressing ^kb. In normal mode a range value may be specified to right-align multiple lines at once, for example entering 10^kb will right-align the current line and 9 lines below it.
install details
put the script in plugin directory

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
right_align.vim 0.7 2014-03-09 7.0 Alexey Radkov fixed alignment of wrapped lines / narrow windows
right_align.vim 0.6 2011-12-29 7.0 Alexey Radkov simple workaround for cases when line is wrapped and options 'linebreak' and/or 'showbreak' are set
right_align.vim 0.5.1 2011-12-16 7.0 Alexey Radkov small bugfix
right_align.vim 0.5 2011-12-14 7.0 Alexey Radkov - variable g:RightBorder was renamed to g:RightAlign_RightBorder
- a new variable g:RightAlign_ShiftRound was added to allow using shiftround behaviour even when option 'shiftround' is not set (the variable is set by default)
- small bug fixes and improvements
right_align.vim 0.4 2011-11-22 7.0 Alexey Radkov Range capability was added to command RightAlign, which makes it possible to right align multiple lines at once in normal and visual modes
right_align.vim 0.3 2011-09-06 7.0 Alexey Radkov bugfix: do not align if the line under cursor contains only blank characters
right_align.vim 0.2 2011-09-05 7.0 Alexey Radkov 1. a new <Plug> imap implemented using <C-r>= instead of <C-o>: the latter option splits undo history in insert mode, therefore it could make unwanted jumps in history list; as such recommended mapping in insert mode was changed. Also the cursor position after undo will be kept now. The both changes make right_align function mimic behaviour of builtin insert mode commands ^t, ^d and ^f.
2. Optional parameter of RightAlign command was renamed from 'on' to 'kc' (which means Keep Cursor)
right_align.vim 0.1 2011-09-04 7.0 Alexey Radkov Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.