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>

This post is also available on DEV.