Loading ...
Sorry, an error occurred while loading the content.
 

strip whitespace and carriage returns from end of file

Expand Messages
  • Chris Lott
    I have the following shortcut to strip whitespace from the end of a file: nnoremap W :%s/ s +$// :let @/= How can I have that command also
    Message 1 of 6 , Nov 25, 2011
      I have the following shortcut to strip whitespace from the end of a file:
      nnoremap <leader>W :%s/\s\+$//<cr>:let @/=''<CR>

      How can I have that command also strip all trailing carriage returns?

      c
      --
      Chris Lott

      --
      You received this message from the "vim_use" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php
    • Thilo Six
      Chris Lott wrote the following on 25.11.2011 18:30 ... You might also have a look a my au below. It additionaly keeps cursor postion. ... ,----[ ]--------
      Message 2 of 6 , Nov 25, 2011
        Chris Lott wrote the following on 25.11.2011 18:30

        > I have the following shortcut to strip whitespace from the end of a file:
        > nnoremap <leader>W :%s/\s\+$//<cr>:let @/=''<CR>
        :%s/[\r \t]\+$//

        You might also have a look a my au below. It additionaly keeps cursor postion.

        > How can I have that command also strip all trailing carriage returns?
        >
        > c
        > --
        > Chris Lott
        >

        ,----[ ]--------

        " automatically delete trailing whitespace & Dos-returns
        fun! <SID>MyDeleteTrailingWhitespace()
        if ! &bin
        let l:cpo_orig = &cpo
        set cpo&vim
        let l:l = line(".")
        let l:c = col(".")
        silent! :%s/[\r \t]\+$//
        call histdel("search", -1)
        call cursor(l:l, l:c)
        let &cpo = l:cpo_orig
        endif
        endfun
        autocmd BufWritePre,FileWritePre * call <SID>MyDeleteTrailingWhitespace()
        command! -bar DeleteTrailingWhitespace call <SID>MyDeleteTrailingWhitespace()

        `---------------------------------------------




        --
        Regards,
        Thilo

        4096R/0xC70B1A8F
        721B 1BA0 095C 1ABA 3FC6 7C18 89A4 A2A0 C70B 1A8F


        --
        You received this message from the "vim_use" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php
      • John Beckett
        ... There is a slightly superior mapping at: http://vim.wikia.com/wiki/Remove_unwanted_spaces ... You could muck about with a search pattern using r to match
        Message 3 of 6 , Nov 25, 2011
          Chris Lott wrote:
          > I have the following shortcut to strip whitespace from the
          > end of a file:
          > nnoremap <leader>W :%s/\s\+$//<cr>:let @/=''<CR>

          There is a slightly superior mapping at:
          http://vim.wikia.com/wiki/Remove_unwanted_spaces

          > How can I have that command also strip all trailing
          > carriage returns?

          You could muck about with a search pattern using \r to match CR,
          however it would be better to investigate why there are trailing
          CRs. Procedures are discussed at:

          http://vim.wikia.com/wiki/File_format

          John

          --
          You received this message from the "vim_use" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php
        • Thilo Six
          John Beckett wrote the following on 25.11.2011 23:26 -- -- ... Thanks for the hints. I updated my au. If you find any issue please let me know. --
          Message 4 of 6 , Nov 26, 2011
            John Beckett wrote the following on 25.11.2011 23:26

            -- <snip> --
            > You could muck about with a search pattern using \r to match CR,
            > however it would be better to investigate why there are trailing
            > CRs. Procedures are discussed at:
            >
            > http://vim.wikia.com/wiki/File_format
            >
            > John

            Thanks for the hints. I updated my au. If you find any issue please let me know.

            --
            Regards,
            Thilo

            4096R/0xC70B1A8F
            721B 1BA0 095C 1ABA 3FC6 7C18 89A4 A2A0 C70B 1A8F

            --
            You received this message from the "vim_use" maillist.
            Do not top-post! Type your reply below the text you are replying to.
            For more information, visit http://www.vim.org/maillist.php
          • Chris Lott
            ... I know I m putting the ending carriage returns in there. I write mostly prose and due to editing and shifting text I end up with blank lines at the end of
            Message 5 of 6 , Nov 26, 2011
              On Sat, Nov 26, 2011 at 10:03 AM, Thilo Six <T.Six@...> wrote:
              > John Beckett wrote the following on 25.11.2011 23:26
              >
              > -- <snip> --
              >> You could muck about with a search pattern using \r to match CR,
              >> however it would be better to investigate why there are trailing
              >> CRs. Procedures are discussed at:
              >>
              >> http://vim.wikia.com/wiki/File_format

              I know I'm putting the ending carriage returns in there. I write
              mostly prose and due to editing and shifting text I end up with blank
              lines at the end of the file... so their appearance is my fault, but
              stripping them away doesn't hurt and I'd like a quicker way of
              cleaning them up...

              c
              --
              Chris Lott

              --
              You received this message from the "vim_use" maillist.
              Do not top-post! Type your reply below the text you are replying to.
              For more information, visit http://www.vim.org/maillist.php
            • Thilo Six
              Thilo Six wrote the following on 26.11.2011 20:03 Hello -- -- ... Just for completeness. This modified version actually works. -- Regards, Thilo
              Message 6 of 6 , Nov 27, 2011
                Thilo Six wrote the following on 26.11.2011 20:03

                Hello

                -- <snip> --
                > Thanks for the hints. I updated my au. If you find any issue please let me
                > know.

                Just for completeness. This modified version actually works.

                --
                Regards,
                Thilo

                4096R/0xC70B1A8F
                721B 1BA0 095C 1ABA 3FC6 7C18 89A4 A2A0 C70B 1A8F

                --
                You received this message from the "vim_use" maillist.
                Do not top-post! Type your reply below the text you are replying to.
                For more information, visit http://www.vim.org/maillist.php
              Your message has been successfully submitted and would be delivered to recipients shortly.