Allign the words to the specified column in vim using commands

泄露秘密 提交于 2019-12-02 02:20:49

Approach with built-in commands

First :substitute the whitespace with a Tab character, and then :retab to a tab stop to column 8, expanding to spaces (for your given example):

:.,.+4substitute/\s\+/\t/ | set tabstop=7 expandtab | '[,']retab

(I'm omitting the resetting of the modified options, should that matter to you.)

Approach with plugin

My AlignFromCursor plugin has commands that align text to the right of the cursor to a certain column. Combine that with a :global command that invokes this for all lines in the range, and a W motion to go to the second word in each, and you'll get:

.,.+4global/^/exe 'normal! W' | LeftAlignFromCursor 8

except for vim-plugins mentioned by others, if you were working on a linux box with column command available, you could just :

%!column -t

% could be vim ranges, e.g. visual selections etc..

I use the Tabular plugin. After installing it, you run the following command:

:%Tab/\s

where \s means whitespace character

I have made two functions for this problem. I have posted it here : https://github.com/imbichie/vim-vimrc-/blob/master/MCCB_MCCE.vim

We need to call this function in vim editor and give the Number of Occurrence of the Character or Space that you wants to move and the character inside the '' and the column number.

The number of occurrence can be from the starting of each line (MCCB function) or can be at the end of each line (MCCE function).

for the above example mentioned in the question we can use the MCCB function and the character we can use space, so the usage will be like this in the vim editor.

:1,5call MCCB(1,' ',8)

So this will move the first space (' ') to the 8th column from line number 1 to 5.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!