vimvim-macros

vim, duplicate whole line to the end of each line?


How to copy the current line content to the end of each line for the whole file ? For example:

From

hello 
world 
!

To

hello     hello 
world     world 
!         !

Solution

  • :%s/^\(.*\)$/\1 \1

    There may be a problem with formatting those lines in columns. Here is an example:

    hello           hello
    hello           hello
    world           world
    !           !
    

    This can be solved with addition of trailing spaces, the the shorter lines, like this: (. - denotes trailing space)

    hello           hello
    hello           hello
    world           world
    !....           !....    
    

    As mentioned by @mattb in the comment, column formatting issue can be fixed with column command (it has to be on your PATH though): :%s/^\(.*\)$/\1 \1/ | %!column -t