I have written some code to create a new buffer, it starts up in fundamental mode, the buffer is to display output from commands.

I am using this to create the buffer

(get-buffer-create "*My Buffer*")

Then I am trying to switch to and change the mode using this.

(display-buffer buffer-name)

My understanding is that this should switch to the buffer and change the mode which is what actually happens with the added side effect the the original buffer I was using also switches to special-mode so both buffers end up in special-mode.

Am i doing this completely wrong ? how can i create a buffer for command output and change the mode of just that buffer ?

This is the working answer.

(display-buffer buffer-name)
(with-current-buffer buffer-name (special-mode))

The docstring of display-buffer says (C-h f display-buffer):

display-buffer is an interactive compiled Lisp function in `window.el'.

It is bound to C-x 4 C-o.

(display-buffer BUFFER-OR-NAME &optional ACTION FRAME)

Display BUFFER-OR-NAME in some window, without selecting it.

You have to select the buffer for following operations to act on it. See eg set-buffer or with-current-buffer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.