emacs: command to insert date or random number

perm url with updates: Emacs Misc Tips.

2010-03-27

Another short elisp tip. I needed a fast way to insert random numbers. So i wrote:

(defun insert-random-number ()
  "Insert a 4 digits random number."
  (interactive)
  (insert
   (number-to-string (random 9999))
   )
  )

If you are new to lisp... notice that elisp does not automatically convert number to string. So, number-to-string is very convenient. There's also string-to-number.

Once i defined this function, i can either give it a Keyboard Shortcut, or define a alias. I have already too many shortcuts, so i just define a alias, by “(defalias 'irn 'insert-random-number)”. Then, when i need to have a random number inserted, i just type “Alt+x irn”.

Exercise: write a command that insert current date/time. (answer can be found at: How To Update Web Feed With Elisp)

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs