static void CopyBufferToClipboard (TextBuffer buf) { //get current cursor state TextIter s, e; TextIter cursorIter = TextIter.Zero; var hadSel = buf.GetSelectionBounds (out s, out e); if (!hadSel) { cursorIter = buf.GetIterAtOffset (buf.CursorPosition); } //copy text to clipboard, let the buffer handle the details buf.SelectRange (buf.StartIter, buf.EndIter); Clipboard clipboard = Clipboard.Get (Mono.TextEditor.ClipboardActions.CopyOperation.CLIPBOARD_ATOM); buf.CopyClipboard (clipboard); //restore cursor state if (hadSel) { buf.SelectRange (s, e); } else { buf.PlaceCursor (cursorIter); } }