History
Article Edit this page Discussion

Set options or named registers with let

From Vim Tips Wiki

Jump to: navigation, search

Tip 1520 Previous Next Created: October 8, 2007 Complexity: basic Author: Fritzophrenic Version: 7.0


Contents

[edit] Setting registers

It isn't immediately obvious, but let can be used for far more than setting variables. It can also be used, for example, to set a register.

For example, to set register "r" to hold a really long variable name:

let @r = "reallyReallySuperLongVariableNameWithSpamAndEggs"

Use "@@" to set the unnamed register, for example:

let @@ = "example contents of the unnamed register"

Using this technique, you can duplicate the setreg function for use in older versions of Vim, as follows:

if v:version < 602
  " Vim 6.2 introduced the setreg function. Make our own otherwise
  function Setreg(regname, regval)
    exe "let @".a:regname." = '".a:regval."'"
  endfunction
  " set up registers with 'call Setreg', for example
  call Setreg('a', 'yawP`[j')
else
  " set up registers with 'call setreg', for example
  call setreg('a', 'yawP`[j')
endif

Sourcing the above script sets register a. In normal mode, put the cursor in a word and press @a to execute the commands: yaw (yank a word), P (paste), `[ (jump to start of last yank), j (down).

That duplicates the initial word, and is an example of how a macro can be placed in a register.

[edit] Setting options

let can also be used to set options, which can be useful in a script.

For example, to save the current value of the 'readonly' option, and restore it later:

let oldro=&readonly
...
...
let &readonly=oldro

[edit] References

[edit] Comments

Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Doctor Who
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Mystery Science Theater 3000
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions
Send this article to a friend
"Set options or named registers with let"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation