J K
J K

Reputation: 33

vi editor not loading saved macros

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 :)

Upvotes: 0

Views: 206

Answers (3)

J K
J K

Reputation: 33

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.

Upvotes: 0

Ingo Karkat
Ingo Karkat

Reputation: 172520

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.

Upvotes: 0

D. Ben Knoble
D. Ben Knoble

Reputation: 4673

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.

Upvotes: 1

Related Questions