I am trying to create my own IDE like setup inside of VIM. This is what I have. I am also using Tmux here if you wonder about the bottom terminal screen.

enter image description here

If I change file to another CPP within NERDTree, I get the following:

enter image description here

How am I meant to close the rightmost window when editing a new file?

I have tried a few options however it remove the rightmost window whenever I change window, instead of opening a new file.

Here is my vimrc:

set nocompatible              " be iMproved, required
filetype off                  " required

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'

Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
Plugin 'dart-lang/dart-vim-plugin'

Plugin 'MarcWeber/vim-addon-mw-utils'
Plugin 'tomtom/tlib_vim'
Plugin 'garbas/vim-snipmate'
Plugin 'vim-scripts/a.vim'
Plugin 'scrooloose/nerdtree'
Plugin 'altercation/vim-colors-solarized'

Plugin 'honza/vim-snippets'

call vundle#end()            " required
syntax on
filetype plugin indent on    " required
filetype on
let g:solarized_termcolors = 256
colorscheme solarized

set number

set tabstop=2
set shiftwidth=2
set expandtab

autocmd FileType python setlocal noexpandtab shiftwidth=2 softtabstop=2

set background=dark

let mapleader = ":"

"Map keys to create new window
nmap nvw <c-w>v
nmap nhw <c-w>s

"Mak keys to change window
nmap + <C-w><Up>
nmap = <C-w><Down>
nmap _ <C-w><Left>
nmap - <C-w><Right>

"Map key to Open NERDTree
map <leader>fs :NERDTreeToggle<CR>

"Close VIM if NERDTree is final window
autocmd BufEnter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif

"Run NERDTree automaticly on VIM start

let g:DoingCppSplit=0
function CppSplit()
    if (g:DoingCppSplit == 0)
        let g:DoingCppSplit = 1
        vertical resize 50
        wincmd x
        let g:DoingCppSplit = 0

autocmd VimEnter * NERDTree
autocmd VimEnter * wincmd l
autocmd FileType cpp nested :call CppSplit()

If it helps, I am using a.vim to open the header split.

This is what I have tried:

let g:hasOpenedFirstFile = 0
function g:QuitThisFile()
  if (g:hasOpenedFirstFile == 1)
    wincmd l

function g:SetHasOpenedFile()
  let g:hasOpenedFirstFile = 1

nmap <leader>x :call QuitThisFile()<cr>

autocmd FileType cpp nested :call QuitThisFile()
autocmd FileType cpp nested :call SetHasOpenedFile()

This is at the end of the vimrc and some reason, it closes one window when vim opens up. I thought the hasOpenedFirstFile would have fixed that.

Edit: (no NERDTree)

As NERDTree is apparently difficult to script with as it's a draw instead of split window spaced, I disabled NERDTree and started to use the built in vim editor via :e..

The problem I am getting now is although I only have my cpp and header file open, the above code closes the right split and the cpp file but keeps a new blank buffer open instead of quitting the window. Why is this?

Edit 2:

For now, I have set up a map to open the hpp file in another tab and another to write and save all tabs.

Here is what I have done:

function g:OpenHeadderFile()
  tabnew %:t:r.hpp

function g:WriteAllTabs()

function g:QuitAllTabs()

function g:SaveAndQuitTabs()

autocmd BufAdd *.cpp call OpenHeadderFile()

imap <leader>hpp <esc>:call OpenHeadderFile()<cr>
nmap <leader>hpp :call OpenHeadderFile()<cr>

nmap tn :tabNext<cr>
nmap tp :tabprevious<cr>

nmap qat :call QuitAllTabs()<cr>
nmap wat :call WriteAllTabs()<cr>
nmap saq :call SaveAndQuitTabs()<cr>

This is a doable work around as I can still easily access my hpp files without having to leave vim or having to manually open my vim file. I can also quit vim with one command. If possible, I would still like to have the hpp side by side and be able to quit vim when I exit from the cpp file. I would also still like to close the hpp slit when editing a new file.

  • @D.BenKnoble What do you mean? I have it there. function g:QuitThisFile() – iProgram Nov 13 '18 at 13:58
  • 1
    Ah i see it i misread. – D. Ben Knoble Nov 13 '18 at 14:03
  • 3
    I know this is not the answer you are looking for, but we often see people struggling with NERDTree and spending hours trying to configure or debug it. Each time I feel like reading Drew Neil's "Oil & Vinegar" might save a lot of time. Good luck with NERDTree if you insist on using it :) – statox Nov 13 '18 at 15:31
  • 3
    IMHO, you shouldn't try to configure Vim to become an IDE (at least not, if you don't know Vim very well yet). – Christian Brabandt Nov 14 '18 at 7:10
  • @ChristianBrabandt I’ve been using VIM for a long time and prefer it to a standard editor. I just miss some IDE features. I know VIM, just not vimscript. – iProgram Nov 14 '18 at 9:18

Your Answer

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

Browse other questions tagged or ask your own question.