Phillip Hamnett
Phillip Hamnett

Reputation: 298

How to make a saved vim macro work when it has ^M in it?

I wrote a macro to delete certain parts of some text, starting by getting rid of the preamble and the end of the text, and then removing some lines in the middle.

This works when I write and use the macro directly, but then I tried to save the macro in my vimrc file, and then it doesn't work anymore.

How can I fix this to make the carriage return behave as I expect it to?

The macro, as saved in my .vimrc file is:

let @r='/+\/-^Mdd'
let @y='v/A \\ Z\^M<80>kuxGvN/*\^Mxgg100@r'

The result when I use it based on the .vimrc file is that the search for A \ Z starts correctly, but the ^M doesn't cause a new line to happen, it is considered part of the search, as shown below: Output of running the macro

The file in question (shortened):

**** Residual nuclei distribution  **** ****           (Bq/cmc)            ****



  A \ Z   57         58         59         60         61         62         63         64         65         66         67
 154   0.00E+00   0.00E+00   0.00E+00   0.00E+00   0.00E+00   0.00E+00   5.64E+00   0.00E+00   0.00E+00   0.00E+00   0.00E+00
      +/- 0.0 %  +/- 0.0 %  +/- 0.0 %  +/- 0.0 %  +/- 0.0 %  +/- 0.0 %  +/- 8.4 %  +/- 0.0 %  +/- 0.0 %  +/- 0.0 %  +/- 0.0 %
 152   0.00E+00   0.00E+00   0.00E+00   0.00E+00   0.00E+00   0.00E+00   4.45E+01   0.00E+00   0.00E+00   0.00E+00   0.00E+00
      +/- 0.0 %  +/- 0.0 %  +/- 0.0 %  +/- 0.0 %  +/- 0.0 %  +/- 0.0 %  +/- 3.8 %  +/- 0.0 %  +/- 0.0 %  +/- 0.0 %  +/- 0.0 %
  A \ Z    1
   3   4.50E+02
      +/- 1.6 %


  **** Isomers (Bq/cmc)     ****
      A        Z       mth
       34      17       1    1.37E+04 +/- 35.2 %
       42      21       1    5.96E-12 +/- 26.1 %
       44      21       1    6.90E+03 +/- 11.9 %

The expected output

A \ Z   57         58         59         60         61         62         63         64         65         66         67
     154   0.00E+00   0.00E+00   0.00E+00   0.00E+00   0.00E+00   0.00E+00   5.64E+00   0.00E+00   0.00E+00   0.00E+00   0.00E+00
     152   0.00E+00   0.00E+00   0.00E+00   0.00E+00   0.00E+00   0.00E+00   4.45E+01   0.00E+00   0.00E+00   0.00E+00   0.00E+00
A \ Z    1
3   4.50E+02

(Ignore the white space issues in the output, that is just because of stack overflow)

Upvotes: 2

Views: 687

Answers (1)

Peter Rincker
Peter Rincker

Reputation: 45117

I would use double quotes and key-notation.

let @r = "/+\\/-\<cr>dd"
let @y = "v/A \\\\ Z\\<cr>kuxGvN/*\\\<cr>xgg100@r"

Basically all those ^M which represent a return will translate to key notation, <cr>. All key notation will need to be escaped with \ as well as the \ character of course.

If you find yourself using these all the time, I would suggest you create mappings instead of using registers.

For more help see:

:h key-notation
:h expr-quote

Upvotes: 2

Related Questions