# How to watch minibuffer input?

**URL:** <https://forum.systemcrafters.net/t/how-to-watch-minibuffer-input/1075>\
**Category:** Emacs\
**Created:** [October 26, 2024, 1:14am UTC](https://forum.systemcrafters.net/t/how-to-watch-minibuffer-input/1075 "2024-10-26T01:14:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mate8](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@mate8](https://forum.systemcrafters.net/u/mate8)\
**Post date:** [October 26, 2024, 1:14am UTC](https://forum.systemcrafters.net/t/how-to-watch-minibuffer-input/1075/1 "2024-10-26T01:14:08Z")

</div>

hello everyone

i want to write a function to watch minibuffer input which is similar like company.

here is the details:

when i type four chars in minibuffer, then run a function named “test” automatically.

i’m a emacs beginner and i hope get help for this.

thank you all!

---

<div class="post-metadata">

**Author:** ![BigEatie](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.systemcrafters.net/bigeatie/32/247_2.png) [@BigEatie](https://forum.systemcrafters.net/u/BigEatie)\
**Post date:** [October 26, 2024, 1:44am UTC](https://forum.systemcrafters.net/t/how-to-watch-minibuffer-input/1075/2 "2024-10-26T01:44:37Z")

</div>

I’m not entirely sure what you mean, but I wonder if you’re looking for a minibuffer completion framework like [vertico](https://github.com/minad/vertico)?

---

<div class="post-metadata">

**Author:** ![mate8](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@mate8](https://forum.systemcrafters.net/u/mate8)\
**Post date:** [October 26, 2024, 3:00am UTC](https://forum.systemcrafters.net/t/how-to-watch-minibuffer-input/1075/3 "2024-10-26T03:00:37Z")

</div>

sorry, it’s not about completion in minibuffer.

actually my need is:

minibuffer runs a custom function which is named “test or whatever” automatically after i type four chars in it.

---

<div class="post-metadata">

**Author:** ![BigEatie](https://yyz1.discourse-cdn.com/flex029/user_avatar/forum.systemcrafters.net/bigeatie/32/247_2.png) [@BigEatie](https://forum.systemcrafters.net/u/BigEatie)\
**Post date:** [October 26, 2024, 5:13am UTC](https://forum.systemcrafters.net/t/how-to-watch-minibuffer-input/1075/4 "2024-10-26T05:13:03Z")

</div>

I’m not sure if there is a more targeted hook, but post-command hook will probably work, i.e.

```auto
(defun run-test-if-four-chars ()
  (when (and (minibufferp)
             (= (length (minibuffer-contents)) 4))
    (test)))

(defun test ()
  (message "Foo"))

(add-hook 'post-command-hook 'run-test-if-four-chars)

```

---

<div class="post-metadata">

**Author:** ![mate8](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@mate8](https://forum.systemcrafters.net/u/mate8)\
**Post date:** [October 26, 2024, 5:37am UTC](https://forum.systemcrafters.net/t/how-to-watch-minibuffer-input/1075/5 "2024-10-26T05:37:03Z")

</div>

thank you so much firstly.

i’ll have a try at once.

---

<div class="post-metadata">

**Author:** ![mate8](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@mate8](https://forum.systemcrafters.net/u/mate8)\
**Post date:** [October 26, 2024, 5:54am UTC](https://forum.systemcrafters.net/t/how-to-watch-minibuffer-input/1075/6 "2024-10-26T05:54:37Z")

</div>

you are right and the code works well using emacs -Q

one more thing, i add this

```auto
(defun run-test-if-four-chars ()
  (when (and (minibufferp)
             (= (length (minibuffer-contents)) 4))
    (expand-abbrev)
    (read-only-mode 1)
    (exit-minibuffer)))

```

now how to limit the four chars only inside abbrev file to activate the function?

---

<div class="post-metadata">

**Author:** ![mate8](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@mate8](https://forum.systemcrafters.net/u/mate8)\
**Post date:** [October 28, 2024, 9:25am UTC](https://forum.systemcrafters.net/t/how-to-watch-minibuffer-input/1075/7 "2024-10-28T09:25:48Z")

</div>

thanks for BigEatie’s great help firstly.  
i use these codes to get it work:

```auto
(defun run-test-if-four-chars ()
  (when (and (minibufferp)
             (= (length (minibuffer-contents)) 4))
    (expand-abbrev)
    (read-only-mode 1)
    (exit-minibuffer)))

(defun custom-execute-extended-command ()
  (interactive)
  (add-hook 'post-command-hook 'run-test-if-four-chars)
  (execute-extended-command nil)
  (remove-hook 'post-command-hook 'run-test-if-four-chars)

(global-set-key (kbd "C-c s c") 'custom-execute-extended-command)

```

and is there a better way to do this?
