DEV Community

Kay Gosho
Kay Gosho

Posted on • Updated on

Edit tmux output with Vim (or any text editor)

image

We can scroll tmux output with C-b [ by default, and it provides vim like key binding.

However, some bindings are different from my customized vim.

So I add some scripts and settings to open tmux output with Vim (or any other text editor).

Fortunately, tmux has a nice command capture-pane!



#!/bin/bash

# $HOME/bin/vim-edit-tmux-output

file=`mktemp`.sh
tmux capture-pane -pS -32768 > $file
tmux new-window -n:mywindow "vim '+ normal G $' $file"


Enter fullscreen mode Exit fullscreen mode

chmod +x $HOME/bin/vim-edit-tmux-output



# $HOME/.tmux.conf

setenv -g PATH "$HOME/bin:$PATH"
set-option -g history-limit 20000

bind-key C-e run-shell "vim-edit-tmux-output"


Enter fullscreen mode Exit fullscreen mode

Then press C-b C-e and you can open your tmux output!

Another nice feature is that tmux automatically closes the new pane for Vim after quiting vim.

Top comments (5)

Collapse
 
genkobar profile image
Valdimar Bjorn Asgeirsson

Great tip!

I modified the script to open with the editor currently set via $EDITOR, because I wanted to edit with neovim instead of vim:

#!/bin/bash

# $HOME/bin/vim-edit-tmux-output

file=`mktemp`.sh
tmux capture-pane -pS -32768 > $file
tmux new-window -n:mywindow "$EDITOR '+ normal G $' $file"
Collapse
 
masaeedu profile image
Asad Saeeduddin

Neat, thanks for sharing! I found you can also do -S - to get the entire scrollback buffer (regardless of size) in case that's what the objective of the large -32768 number is.

Collapse
 
acro5piano profile image
Kay Gosho

Thanks!
I also found I have to add the following option to .tmux.conf to see the entire scroll buffer:

set-option -g history-limit 20000

And I added '+ normal G $' to vim launching option to open the file at the bottom:

tmux new-window -n:mywindow "vim '+ normal G $' $file"
Collapse
 
micahriggan profile image
Micah Riggan • Edited

That's pretty neat. Sometimes I do C-b [ then <space> move around to select text with arrows then <enter> to copy it to your clipboard

Collapse
 
acro5piano profile image
Kay Gosho

Yes, tmux has the ability by default.
Sometimes I want to edit, join, and search the text before copy ;)