1

When I'm in the terminal and I press <C-w>s the screen is split vertically and the terminal buffer is shown twice. Then I have to do :terminal if I want a separate terminal instance.

Typing something in one window is in real time shown in the other. This is a feature if I was editing a file but is an inconvenience when working with terminal buffers.

How do I conditionally run <C-w>S:terminal depending on whether the current window is a terminal buffer?

1
  • You could probably setup terminal mappings with tnoremap
    – D. Ben Knoble
    Dec 20, 2018 at 15:57

3 Answers 3

1

The following function will :h :split the current buffer and create a new terminal buffer if the current buffer is a terminal buffer. Otherwise it will just :h :split. Bind this function to both :h :nnoremap and :h :tnoremap variants of mappings to get the required results.

function! TermSplit() abort
    if exists('b:term_title')
        split
        terminal
    else
        split
    endif
endfunction

tnoremap <C-w>s <C-\><C-N>:call TermSplit()<CR>
nnoremap <C-w>s :call TermSplit()<CR>

PS You can create similar function and bind it to <C-w>v for :h :vsplit

1

This is a documented 'feature' of :tnoremap as tnoremap effectively only work for insert mode style actions and thus any variant using tmap will not work.

Adding the below to init.vim will remap the action of Ctrl-w + s in normal mode only for the terminal buffer

autocmd BufWinEnter,WinEnter term://* nnoremap <buffer> <C-w>s <C-\><C-n><C-w><C-v> :terminal <CR>

as mentioned in the comments, it is better to use augroup NAME before this command (and augroup END) so that you don't add this command multiple times when your rc file is being sourced. It's also suggested practice that you place all the relevant terminal-specific commands in this augroup.

1
  • You should mention :h augroup and why OP should wrap autocommands in augroups.
    – 3N4N
    Apr 1, 2019 at 9:00
0

Adding this to ~/.config/init.vim solved it:

" Split into a new terminal instance                       
tnoremap <C-w>s <C-\><C-n><C-w><C-s> :terminal <CR>
tnoremap <C-w>v <C-\><C-n><C-w><C-v> :terminal <CR>
set splitbelow                                     
set splitright                                     

It maps the split commands (<C-w>s and <C-w>v) to the following action:

  1. <C-\><C-n> - exit insert mode in terminal
  2. <C-w><C-v> - run the actual split command
  3. :terminal - unter the terminal command
  4. <CR> - press enter

The additional two commands make the newly opened terminal doesn't appear on the location of the previous one.

2
  • This works if the terminal is in insert mode, but is still splits into the same terminal if it's executed from normal mode. Dec 22, 2018 at 13:11
  • Create a function that checks if the current buffer is terminal, and act accordingly. Then map the keys to that function. I added an answer with such a function.
    – 3N4N
    Apr 1, 2019 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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