0

Need help with vi macros on (redhat-linux 7.5)

My ~/.vimrc looks like below:

set nu

let @c='0i#^[j'
let @u='0<80>kDj'

but still when I vi files, @c or @u doesn't work. Also, when I cat ~/.vimrc, it shows as below, which I believe is due to control chars:

set nu

let @c='0i#'
let @u='0▒kDj'

However, they do work every time when I record them. Just don't load up from vimrc and work persistently. thanks in advance :)

J K
  • 13
  • 3

3 Answers3

1

If you’re already using vim (instead of pure vi), you could do

let @c = 'I#^['  " make sure to insert a literal escape

Or better yet:

" in ~/.vim/after/ftplugin/c.vim
nnoremap <buffer> gcc I#<Esc>j

The second one can be done similarly.

D. Ben Knoble
  • 3,649
  • 1
  • 18
  • 31
0

This should work just fine, as long as the original byte sequences are kept (so <80> is a single character and not four < 8 0 >) - your cat output seems to confirm this. The only issue I can think of is that Vim might use a wrong encoding when reading the .vimrc (but somehow detecting the right one when editing it). A scriptencoding utf-8 at the top might help then.

In any case, by using :help key-notation, these issues can be avoided. You'd need to use double quotes to have then interpreted (and then write \<Esc> instead of ^[, \<BS> instead of <80>kb, and so on). While you're at it, why not define proper mappings (with :map, or rather :nnoremap), to free up the registers again.

Speaking of registers, there's always the chance of accidentally overriding them, they're limited in number, and hard to memorize; all things that mappings don't suffer from. If you really want to continue to reserve registers for these shortcuts (and don't have the problem of overriding them), you could just rely on the viminfo-file to persist them; there actually is no need to explicitly initialize them in your ~/.vimrc.

Ingo Karkat
  • 154,018
  • 15
  • 205
  • 275
  • Thanks for replying Actually, I did check vimrc to ensure <80> was single character, and it indeed is. "b" or "w" pass thru it in single stroke. also, add encoding as first line in vimrc, but it still doesn't work. Studying viminfo-file and map/nnoremap functions to understand how I can use them. any further help is appreciated – J K Dec 05 '19 at 10:05
0

finally I got it working. Just tried using "vim" instead of "vi" to see if it loads up the ".vimrc" and it did. I was assuming that vi would too load .vimrc, but it seems I was wrong. Now, I am able to use the saved macros exactly as expected.

J K
  • 13
  • 3