[GH-ISSUE #164] Cooperate with Vim #130

Closed
opened 2026-03-04 01:02:13 +03:00 by kerem · 5 comments
Owner

Originally created by @JesseAldridge on GitHub (Sep 8, 2018).
Original GitHub issue: https://github.com/rivo/tview/issues/164

Hi, I'm using tview to build a shared note-taking tool: https://github.com/JesseAldridge/dentist
I'm wondering if it's possible to get tview to work well with Vim.
Below is a test script. Vim gets launched successfully but the input seems to be conflicting with tview somehow and it doesn't work well.

I'm new to Go and tview so maybe I'm doing something wrong.

package main

import (
	"os"
	"os/exec"

	"github.com/gdamore/tcell"
	"github.com/rivo/tview"
)

func write_and_open(note_name string) {
	cmd := exec.Command("vim", note_name)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Run()
}

func main() {
	app := tview.NewApplication()

	app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		k := event.Key()
		if k == tcell.KeyEnter {
			write_and_open("test name")
		}
		return event
	})

	box := tview.NewBox().SetBorder(true).SetTitle("Hello, world!")
	if err := app.SetRoot(input_field, true).Run(); err != nil {
		panic(err)
	}
}
Originally created by @JesseAldridge on GitHub (Sep 8, 2018). Original GitHub issue: https://github.com/rivo/tview/issues/164 Hi, I'm using tview to build a shared note-taking tool: https://github.com/JesseAldridge/dentist I'm wondering if it's possible to get tview to work well with Vim. Below is a test script. Vim gets launched successfully but the input seems to be conflicting with tview somehow and it doesn't work well. I'm new to Go and tview so maybe I'm doing something wrong. ``` package main import ( "os" "os/exec" "github.com/gdamore/tcell" "github.com/rivo/tview" ) func write_and_open(note_name string) { cmd := exec.Command("vim", note_name) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Run() } func main() { app := tview.NewApplication() app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { k := event.Key() if k == tcell.KeyEnter { write_and_open("test name") } return event }) box := tview.NewBox().SetBorder(true).SetTitle("Hello, world!") if err := app.SetRoot(input_field, true).Run(); err != nil { panic(err) } } ```
kerem closed this issue 2026-03-04 01:02:13 +03:00
Author
Owner

@JesseAldridge commented on GitHub (Sep 8, 2018):

Actually, looks like I just need to wrap the vim call with Stop and Run:

		if k == tcell.KeyEnter {
			app.Stop()
			write_and_open("test name")
			app.Run()
		}
<!-- gh-comment-id:419599011 --> @JesseAldridge commented on GitHub (Sep 8, 2018): Actually, looks like I just need to wrap the vim call with `Stop` and `Run`: ``` if k == tcell.KeyEnter { app.Stop() write_and_open("test name") app.Run() } ```
Author
Owner

@rivo commented on GitHub (Sep 10, 2018):

I know you closed this already but while Stop() and Run() should probably work, there is a function dedicated to these kind of situations: Suspend().

I thought this may be useful to you.

<!-- gh-comment-id:420033731 --> @rivo commented on GitHub (Sep 10, 2018): I know you closed this already but while `Stop()` and `Run()` should probably work, there is a function dedicated to these kind of situations: [`Suspend()`](https://godoc.org/github.com/rivo/tview#Application.Suspend). I thought this may be useful to you.
Author
Owner

@JesseAldridge commented on GitHub (Sep 10, 2018):

Oh cool. Thanks for letting me know.

<!-- gh-comment-id:420064521 --> @JesseAldridge commented on GitHub (Sep 10, 2018): Oh cool. Thanks for letting me know.
Author
Owner

@JesseAldridge commented on GitHub (Sep 10, 2018):

Actually I'm still seeing some odd behavior. Everything works well when I use vim as the editor. But if I use subl instead, after I finish editing the file and come back to the terminal my next keystroke seems to get dropped.

In the demo below I run the script, hit return to open sublime, close the text file, select the terminal, press return again (nothing happens), press return again (sublime opens again). I get similar behavior if I use subl -w instead of subl.

subl is configured like so: https://www.sublimetext.com/docs/3/osx_command_line.html

Any idea what the issue could be?

package main

import (
	"os"
	"os/exec"

	"github.com/gdamore/tcell"
	"github.com/rivo/tview"
)

func write_and_open(note_name string) {
	cmd := exec.Command("subl", note_name)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Run()
}

func main() {
	app := tview.NewApplication()

	app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		k := event.Key()
		if k == tcell.KeyEnter {
			app.Suspend(func() { write_and_open("test name") })
		}
		return event
	})

	box := tview.NewBox().SetBorder(true).SetTitle("Hello, world!")
	if err := app.SetRoot(box, true).Run(); err != nil {
		panic(err)
	}
}
<!-- gh-comment-id:420085379 --> @JesseAldridge commented on GitHub (Sep 10, 2018): Actually I'm still seeing some odd behavior. Everything works well when I use `vim` as the editor. But if I use `subl` instead, after I finish editing the file and come back to the terminal my next keystroke seems to get dropped. In the demo below I run the script, hit return to open sublime, close the text file, select the terminal, press return again (nothing happens), press return again (sublime opens again). I get similar behavior if I use `subl -w` instead of `subl`. `subl` is configured like so: https://www.sublimetext.com/docs/3/osx_command_line.html Any idea what the issue could be? ``` package main import ( "os" "os/exec" "github.com/gdamore/tcell" "github.com/rivo/tview" ) func write_and_open(note_name string) { cmd := exec.Command("subl", note_name) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Run() } func main() { app := tview.NewApplication() app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { k := event.Key() if k == tcell.KeyEnter { app.Suspend(func() { write_and_open("test name") }) } return event }) box := tview.NewBox().SetBorder(true).SetTitle("Hello, world!") if err := app.SetRoot(box, true).Run(); err != nil { panic(err) } } ```
Author
Owner

@JesseAldridge commented on GitHub (Sep 10, 2018):

I should probably open a separate issue for this: https://github.com/rivo/tview/issues/165

<!-- gh-comment-id:420087919 --> @JesseAldridge commented on GitHub (Sep 10, 2018): I should probably open a separate issue for this: https://github.com/rivo/tview/issues/165
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#130
No description provided.