Skip to main content

How do I convert dos files to linux files in vim?


If I open files I created in windows, the lines all end with ^M.





How do I delete them all in once?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. dos2unix is a commandline utility that will do this, or


    :%s/^M//g



    will if you use ctrl-v ctrl-m to input the ^M. Or you can:


    :set ff=unix



    and vim will do it for you. Docs on the 'fileformat' setting are here, and the vim wiki has a comprehensive page on line ending conversions.

    Alternately, if you move files back and forth a lot, you might not want to convert them, but rather to do


    :set ff=dos


    so vim will know it's a DOS file and use DOS conventions for line endings.

    ReplyDelete
  2. Change the lineendings in the view:

    :e ++ff=dos
    :e ++ff=mac
    :e ++ff=unix


    This can also be used as saving operation (:w alone will not save using the lineendings you see on screen):

    :w ++ff=dos
    :w ++ff=mac
    :w ++ff=unix


    And you can use it from the command-line:

    for file in $(ls *cpp)
    do
    vi +':w ++ff=unix' +':q' ${file}
    done

    ReplyDelete
  3. I prefer to use the following command :

    :set fileformat=unix


    You can also use mac or dos to respectively convert your file to macintosh or MS-DOS/MS-Windows file convention. And it does nothing if the file is already in the correct format.

    For more information, see the vim help :

    :help fileformat

    ReplyDelete
  4. :%s/\r+//g

    In Vim, that strips all carriage returns, and leaves only newlines.

    ReplyDelete
  5. I typically use

    :%s/\r/\r/g


    which seems a little odd, but works because of the way that vim matches linefeeds. I also find it easier to remember :)

    ReplyDelete
  6. from: http://vim.wikia.com/wiki/Change_end-of-line_format_for_dos-mac-unix

    [Esc] :%s/\r$//

    ReplyDelete
  7. :set fileformat=unix to convert from dos to unix.

    ReplyDelete
  8. Usually there is a dos2unix command you can use for this, just make sure you read the manual as the GNU and BSD versions differ on how they deal with the arguments.

    # BSD version
    dos2unix $FILENAME $FILENAME_OUT
    mv $FILENAME_OUT $FILENAME

    #GNU version
    dos2unix $FILENAME


    Alternatively, you can create your own dos2unix with any of the proposed answers here, for example:

    function dos2unix(){
    [ "${!}" ] && [ -f "{$1}" ] || return 1;

    { echo ':set ff=unix';
    echo ':wq';
    } | vim "${1}";
    }

    ReplyDelete
  9. You can use the following command:
    :%s/^V^M//g
    where the '^' means use "Ctrl" key.

    ReplyDelete
  10. With the following command:

    :%s/^M$//g


    Get the ^M to appear type Ctrl-V then Ctrl-M. Ctrl-V tells Vim to take the next character entered literally.

    ReplyDelete
  11. dos2unix can directly modify the file contents.

    you can diretly use on the file. no need for temp file redirection

    dos2unix input.txt input.txt

    the above uses the assumed US keyboard
    use -437 option to use the UK keyboard.

    dos2uinx -437 input.txt input.txt

    in solaris we can use dd command also.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.