How to set new doc content

You can create a change spec that covers the whole existing document:

view.dispatch({changes: {
  from: 0,
  to: view.state.doc.length,
  insert: 'my new content'
}})

See also CodeMirror 6: Setting the contents of the editor

Or you can call view.setState(...) to complete replace the current state:

view.setState(EditorState.create({doc: 'my new content'}))

(but you also need to pass any extensions you are using)

1 Like