Allign the words to the specified column in vim using commands

微笑、不失礼 提交于 2019-12-02 03:37:10

问题


How I can move or shift the words in the entire file to the specified column? For example like below: Before :

123 ABC
112 XYZS
15925 asdf
1111 25asd
1 qwer

After :

123    ABC
112    XYZS
15925  asdf
1111   25asd
1      qwer

How it can be done using command mode? Here the thing is we need to shift the 2nd word to the specified column Here the specified column is 8


回答1:


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



回答2:


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..




回答3:


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

:%Tab/\s

where \s means whitespace character




回答4:


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.



来源:https://stackoverflow.com/questions/23217066/allign-the-words-to-the-specified-column-in-vim-using-commands

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