December 2006

Emulate vi’s dot (.) command in Emacs

One of the most powerful commands vi has that emacs doesn’t have is the dot (.) command that repeats the previous command. If you use emacs what will you do if you are addicted to vi’s dot command?

There is no adequate answer. Some answers are

  1. Use the key sequence C-x z if you have version 20.3. or later. Equivalent to the command repeat.
  2. Use the key sequence C-x <ESC> <ESC>, for repeat-complex-command. You can even navigate to the previous complex command using M-p and M-n.
  3. Get vi-dot.el by Will Mengarini. Check Will Mengarini’s homepage. If not found, try a google search on vi-dot.el.
  4. Use macros for repeating complex commands.
  5. Use any vi-emulating modes in emacs, like VIPER.
  6. Use vi :-)

Emacs
vi/vim

Comments (0)

Permalink

Emulating vi’s % command in Emacs

In vi/vim, The % command will match any parenthesis/bracket for its counterpart. The same thing can be achieved in emacs by one of the following ways:

  1. In all language modes, the following keys work:
       M-C-b          : Goto starting bracket ({, (, < etc.)
       M-C-f          : Goto ending bracket (}, ), > etc.)
       M-C-a          : Goto beginning of function
       M-C-e          : Goto end of function
       
  2. If you want the exact behavior of vi “%” command, matching all parens, use the following elisp code:
    (defun match-parenthesis ()
      "Go to the matching parenthesis if on parenthesis else insert %."
      (interactive)
      (cond ((looking-at "[([{]") (forward-sexp 1) (backward-char))
    		  ((looking-at "[])}]") (forward-char) (backward-sexp 1))
    		  (t (self-insert-command 1))))
    (global-set-key "%" 'match-parenthesis)
        

    It matches the parenthesis if the point is on a parenthesis, otherwise inserts the character ‘%’.

Emacs
vi/vim

Comments (0)

Permalink