user1539179
user1539179

Reputation: 1885

Using elisp to move the cursor in the evil minibuffer

I am trying to create a function in elisp to open the evil ex buffer with prefilled text and place the cursor somewhere in the middle. However, all I've been able to do so far is open the buffer with prefilled text and the cursor at the end with this:

(evil-ex "HelloWorld")

If anyone could help, I would really apprecieate it.

Upvotes: 2

Views: 307

Answers (1)

Jordon Biondo
Jordon Biondo

Reputation: 4029

Use a minibuffer-setup-hook

You can use a minibuffer-setup-hook to execute some elisp after starting up the minibuffer, there is a useful macro for this that lets you add a temporary hook: minibuffer-with-setup-hook

Example:

Here is an example where I startup the minibuffer with evil-ex and move the cursor to the midpoint of the initial value:

(let ((my-string "Hello, World!"))
  (minibuffer-with-setup-hook
      (lambda () (backward-char (/ (length my-string) 2)))
    (evil-ex my-string)))

See the documentation for minibuffer-with-setup-hook:

minibuffer-with-setup-hook is a Lisp macro in `files.el'.

(minibuffer-with-setup-hook FUN &rest BODY)

Temporarily add FUN to `minibuffer-setup-hook' while executing BODY.
BODY should use the minibuffer at most once.
Recursive uses of the minibuffer are unaffected (FUN is not
called additional times).

minibuffer-setup-hook:

minibuffer-setup-hook is a variable defined in `C source code'.

Documentation:
Normal hook run just after entry to minibuffer. <- important part

Upvotes: 4

Related Questions