4

I could create a terminal with vim by running :term in the vim normal mode. However, this terminal is opened at the top of the window and I would like to open it in the bottom. How could I open it at the bottom? By the way, how could I change the height of this window?

  • 1
    Have a look at the great documentation: :help window-resize and read up on the comand modifiers, start reading at :help :vertical – Christian Brabandt Jul 19 '18 at 6:33
  • In neovim :term doesn't open terminals in split buffer; I don't know about vim. And you could look at splitbelow option. – klaus Jul 19 '18 at 7:25
7

In your ~/.vimrc add the line

set splitbelow

This will cause all splits to happen below (including terminal).

To change the height of the terminal (row x col)

set termsize=10x0
| improve this answer | |
  • Thanks a lot, how could I change the height of the terminal, and I seem to observe that I could not copy contents in the vim-terminal as I did in pure terminal ? – coin cheung Jul 20 '18 at 3:35
  • Read :help termwinsize to answer the first question. It will tell you how to modify that. You can always look up things by :help command in this case term and scroll. For the second I don't actually know the answer but the help page would likely help. – Steven Walton Jul 20 '18 at 14:08
  • I tried but failed before I asked. I simply add the following lines in my vimrc: set termwinsize = "10*0" and when I open vim and run command :ter, the terminal still occupies half the screen. What mistake did I made then? – coin cheung Jul 21 '18 at 2:34
  • 2
    See also the :below command, e.g. :below terminal – Rich Sep 13 '18 at 13:15
  • @coincheung 10*0 means at least ten rows high. If you want 10 rows exactly, try :set termwinsize=10x0. – Rich Sep 13 '18 at 13:17
1

This question is a couple of years old now but I found it when I was looking for a way to open a terminal inside vim below a bunch of vertical splits. What I want looks something like this:

+----+----+----+
¦    ¦    ¦    ¦
¦ f1 ¦ f2 ¦ f3 ¦
+----+----+----+
¦   terminal   ¦
+--------------+

What you get with :below terminal or with :set splitbelow and then :terminal is:

+----+----+----+
¦ f1 ¦    ¦    ¦
¦    ¦    ¦    ¦
+----+ f2 ¦ f3 ¦
¦term¦    ¦    ¦
+----+----+----+

After a bunch of googling, I pieced this together. To get what I want above is actually pretty simple. Type :botright terminal or :bo term. (See :help :botright for more details.)

You can save a few keystrokes by putting this in your vimrc:

" open terminal below all splits
cabbrev bterm bo term

Then just type :bterm to open a terminal at the bottom.

I hope someone finds this useful.

| improve this answer | |
  • Welcome to Vi and Vim and thanks for writing the answer! I did some small edits, please suggest a further edit if you disagree with any of the changes... Again, welcome! – filbranden Jul 10 at 4:16
  • 1
    Better might be :command Bterm botright terminal—the abbreviation also triggers when searching, where the command does not. – D. Ben Knoble Jul 10 at 12:18
1

I wanted a way to easily toggle a single terminal that would keep the same size and content.

My solution lies in two functions: the main one controls whether a terminal should be opened, the second opens a terminal split where we want on the screen.

If one or more terminal splits exist, they are hidden. Else, a terminal split is opened, either a new one or the first hidden terminal.

function! PutTermPanel(buf, side, size) abort
  " new term if no buffer
  if a:buf == 0
    term
  else
    execute "sp" bufname(a:buf)
  endif
  " default side if wrong argument
  if stridx("hjklHJKL", a:side) == -1
    execute "wincmd" "J"
  else
    execute "wincmd" a:side
  endif
  " horizontal split resize
  if stridx("jkJK", a:side) >= 0
    if ! a:size > 0
      resize 6
    else
      execute "resize" a:size
    endif
    return
  endif
  " vertical split resize
  if stridx("hlHL", a:side) >= 0
    if ! a:size > 0
      vertical resize 6
    else
      execute "vertical resize" a:size
    endif
  endif
endfunction

function! s:ToggleTerminal(side, size) abort
  let tpbl=[]
  let closed = 0
  let tpbl = tabpagebuflist()
  " hide visible terminals
  for buf in filter(range(1, bufnr('$')), 'bufexists(bufname(v:val)) && index(tpbl, v:val)>=0')
    if getbufvar(buf, '&buftype') ==? 'terminal'
      silent execute bufwinnr(buf) . "hide"
      let closed += 1
    endif
  endfor
  if closed > 0
    return
  endif
  " open first hidden terminal
  for buf in filter(range(1, bufnr('$')), 'bufexists(v:val) && index(tpbl, v:val)<0')
    if getbufvar(buf, '&buftype') ==? 'terminal'
      call PutTermPanel(buf, a:side, a:size)
      return
    endif
  endfor
  " open new terminal
  call PutTermPanel(0, a:side, a:size)
endfunction

" Toggle terminal - bottom
nnoremap <silent> yot :call <SID>ToggleTerminal('J', 6)<CR>

" Toggle terminal - right
nnoremap <silent> yo<c-t> :call <SID>ToggleTerminal('L', 60)<CR>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.