Can I transfer a file to Vim?

I know that I can use AWK, but I am in a Windows window. I am making a function for others who may not have AWK. I also know that I can write a program in C, but I would not want to create and compile something for the small Vim utility that I create.

The source file may be

THE DAY WAS LONG THE WAY WAS FAST 

and it will become

 TT HH EE DW AA YY WW AA SS LF OA NS GT 

UPDATE: Golf rules apply to choose the right answer.

UPDATE: Python fans should check out Mr. Duffy's answer below.

+12
python vim text-parsing text-files
Apr 01 '09 at 5:09
source share
6 answers

Here is the Vim command. This way you do not need to compile Vim with + python support.

 function! s:transpose() let maxcol = 0 let lines = getline(1, line('$')) for line in lines let len = len(line) if len > maxcol let maxcol = len endif endfor let newlines = [] for col in range(0, maxcol - 1) let newline = '' for line in lines let line_with_extra_spaces = printf('%-'.maxcol.'s', line) let newline .= line_with_extra_spaces[col] endfor call add(newlines, newline) endfor 1,$"_d call setline(1, newlines) endfunction command! TransposeBuffer call s:transpose() 

Place this in the newly created .vim file in the vim / plugin directory or place it on your [._] vimrc.
Run :TransposeBuffer to wrap the current buffer

+12
Apr 01 '09 at 9:42
source share

Vim support for a number of built-in scripting languages ​​- see the Python interface for an example.

Just change vim.current.buffer accordingly and you will install.

To be more specific:

 function! Rotate() python <<EOF import vim, itertools max_len = max((len(n) for n in vim.current.buffer)) vim.current.buffer[:] = [ ''.join(n) for n in itertools.izip( *( n + ' ' * (max_len - len(n)) for n in vim.current.buffer))] EOF endfunction 
+10
Apr 01 '09 at 5:13
source share

If the scripts do not do this for you, you can write actions to the register (carriage return is added for reading):

 qa 1G0 xGo<Esc>p 1G0j xGp q 

This will give you a macro that you could run against the above example, or any lines of 2 lines of the same length. You need to know the length of the string so that you can repeat the operation with the correct amount of time.

 16@a 

Pretty basic solution, but it works.

+5
Apr 01 '09 at 10:01
source share

The following function performs the necessary editing operations to "transpose" the contents of the current buffer.

 fu!T() let[m,n,@s]=[0,line('$'),"lDG:pu\r``j@s"] g/^/let m=max([m,col('$')]) exe'%norm!'.m."A \e".m.'|D' sil1norm!@s exe'%norm!'.n.'gJ' endf 

Below is its single line version,

 let[m,n,@s]=[0,line('$'),"lDG:pu\r``j@s"]|exe'g/^/let m=max([m,col("$")])'|exe'%norm!'.m."A \e".m.'|D'|exe'sil1norm!@s'|exe'%norm!'.n.'gJ' 
+1
Sep 06 2018-11-11T00:
source share

Charles Duffy's code can be shortened / improved using izip_longest instead of izip :

 function! Rotate() :py import vim, itertools :py vim.current.buffer[:] = [''.join(c) for c in itertools.izip_longest(*vim.current.buffer, fillvalue=" ")] endfunction 
0
Sep 06 2018-11-11T00:
source share

I developed a vim plugin for this. You can find it here . Run :Transpose to transpose the entire file.

0
May 6 '12 at 12:18
source share



All Articles