Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Vim keyboard shortcut to replace ^M

I manually replace ^M with
:%s/<ctrl-v><ctrl-m>//g after I open a log file. I have to do this several times a day so I tried to assign F11 key to perform this action.
I tried the following in .vimrc but it did not work.

:nnoremap <silent> <F11> :let _s=@/ <Bar> :%s/^M//e <Bar> :let @/=_s <Bar> :nohl <Bar> :unlet _s <CR>

Note: I pressed ctrl+vctrl+m to get ^M in the above statement.

I have a similar assignment for F10 to remove EOL whitespace and that works.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

:nnoremap <silent> <F10> :let _s=@/ <Bar> :%s/\s\+$//e <Bar> :let @/=_s <Bar> :nohl <Bar> :unlet _s <CR>

What am I doing wrong here?

>Solution :

Check that your terminal actually forwards F11 to the commands it’s running instead of listening to the key itself. For instance, the default configuration of Windows Terminal intercepts F11 and does not forward it, so you can’t rebind the key inside Vim.

Furthermore, your command looks quite convoluted; my Vimscript knowledge is limited but if the purpose of your command is to change Windows file endings into UNIX file endings, the following, simpler command works:

:nnoremap <silent> <F11> :set fileformat=unix<CR>

There’s also a widely installed utility program, dos2unix, that does the same, and which might be even simpler for your use-case.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading