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
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.
来源:https://stackoverflow.com/questions/23217066/allign-the-words-to-the-specified-column-in-vim-using-commands