loading...
Cover image for Vim to the rescue: Attached Terminal

Vim to the rescue: Attached Terminal

l04db4l4nc3r profile image Angad Sharma ・1 min read

Vim to the rescue (9 Part Series)

1) Vim to the rescue: Prologue 2) Vim to the rescue: Tree to Markdown 3 ... 7 3) Vim to the rescue: Repetition Made Easy 4) Vim to the rescue: Human Readable Processes 5) Vim to the rescue: Multiple Cursors 6) Vim to the rescue: Sessions 7) Vim to the rescue: Attached Terminal 8) Vim to the rescue: Subduing the Shell 9) Vim to the rescue: PDF Preview

Introduction

I was recently working on some C++ code using vim and had to switch to a terminal to compile the code everytime I wanted to test it. Granted that this problem can be solved by installing a plugin which compiles on save, but I wanted an intuitive attached terminal instead.

The problem with terminals inside vim is that they open either in a new tab or in a new split pane. By default, a terminal in vim looks like this:

:term

Alt Text


What can be better

In the screenshot above, there are a few problems:

  • The split pane is too big, and occupies half of the screen
  • You have to enter a command everytime you want a terminal, it is slow
  • The split pane opens up in the top part of the screen by default

What we want to achieve

Alt Text

The benefits we get here are:

  • The terminal opens at the bottom (like it should)
  • The size of the attached terminal is ideal
  • The whole action is bound to a key, so it is fast

To configure your attached terminal this way, you can add the following lines in your .vimrc:

" SpawnTern function
" To add a terminal at the bottom
function SpawnTern()
    " spawn terminal below
    bel term

    " Decrease split size by 15 words
    15winc -
endfunction

" We bind the control+x keys to spawn the terminal
noremap <c-x> :call SpawnTern()<CR>

Vim to the rescue (9 Part Series)

1) Vim to the rescue: Prologue 2) Vim to the rescue: Tree to Markdown 3 ... 7 3) Vim to the rescue: Repetition Made Easy 4) Vim to the rescue: Human Readable Processes 5) Vim to the rescue: Multiple Cursors 6) Vim to the rescue: Sessions 7) Vim to the rescue: Attached Terminal 8) Vim to the rescue: Subduing the Shell 9) Vim to the rescue: PDF Preview

Posted on by:

l04db4l4nc3r profile

Angad Sharma

@l04db4l4nc3r

Backend Developer and DevOps SysAdmin

Discussion

markdown guide