Setting up Vim for FreeBasic

Linux specific questions.
Post Reply
Rickmeister
Posts: 18
Joined: Mar 23, 2013 1:03

Setting up Vim for FreeBasic

Post by Rickmeister »

A little guide for those of you that want to try a real text editor ;) Most of this should work on any OS if Vim is compiled with +python settings (vim --version | grep python). It also needs Exuberant Ctags, GNU make and some lines might need a minor edit as I have a habit of saving intermidiate compile files in /tmp.

Never had any use for any other text editor or IDE for my projects, but to be fair, if you just downloaded Vim and started it up you will get dissapointed as most of Vim;s power is the plugin system. I haven't used FreeBasic for more then three weeks now, and my first impression of it was a bit shaded by the fact that there aren't any good editors to use if you're running Linux and Vim is quite biased towards C/C++. Now, there isn't that great difference between C and FB, so I took some time to set up Vim to achieve the functionality I'm useed to when coding C or Python, and this is what I have so far.

Plugins:
NERDTree
A filemanager Vim addon, I use it as a project manager
git://github.com/scrooloose/nerdtree.git

SuperTab
Makes your Tab enter Supermode! Use it for code completion, completing snippets and much more
git://github.com/ervandew/supertab.git

Fat-finger
Do yuo havw the same problm as I have to tyoe fast AND spell corectly? Fat fingers help you with that
git://github.com/vim-scripts/fat-finger.git

snipMate
Insert and edit snippets, really really useful and speeds up your coding
git://github.com/msanders/snipmate.vim.git

SingleCompile
Compile your file with a simple keypress
git://github.com/vim-scripts/SingleCompile.git

A whole lot of colorschemes
Really not needed, most of them are crap except from Mustang, Colorful256 and a few others
git://github.com/flazz/vim-colorschemes.git

Auto-Pairs
Inserts paired quotationmarks, square-brackets, apostrophes and so on. Highly configurable
git://github.com/vim-scripts/Auto-Pairs.git

ErrorMarker
Highlights the compile errors in the editor
git://github.com/vim-scripts/errormarker.vim.git

Python plugins
This is here because I just did a cut'n'paste from my vimscript repo list, but if you ever do Python scripting these are THE addons to have - atleast IMHO
git://github.com/davidhalter/jedi-vim.git
git://github.com/davidhalter/jedi.git
git://github.com/gotcha/vimpdb.git

Fugitive
Makes it easy to use Github without ever leaving Vim
git://github.com/tpope/vim-fugitive.git

Minibufexpl
Keeps like a tab list of buffers in each tab.. If you get it? Nice to have for easy file management
git://github.com/vim-scripts/minibufexpl.vim.git

Taglist
Navigating source made easy
git://github.com/vim-scripts/taglist.vim.git

Vim.tags
Rebuilds tags on file save, or if you're inactive for a few minutes
git://github.com/szw/vim-tags.git


Pathogen
One way to manage your plugins and keep the plugin folder nice and tidy
git://github.com/vim-scripts/pathogen.vim.git

Vundle
Even easier way to manage plugins
git://github.com/gmarik/vundle.git

Please see posts below for setting up your .vimrc/_vimrc or .vim/_vimfiles in Linux or Windows
Last edited by Rickmeister on Apr 06, 2013 17:49, edited 1 time in total.
Rickmeister
Posts: 18
Joined: Mar 23, 2013 1:03

Re: Setting up Vim for FreeBasic

Post by Rickmeister »

Linux

For linux, you have two options:

Vimogen
I assume that you already have git, ctags and gvim installed, if you're running a ubuntu linux distro (kubuntu/xubuntu/mint etc) it's easy to install them:
$> sudo apt-get install exuberant-ctags git vim

First of all, open up a terminal and navigate to your home folder (cd ~) and then issue the following commands:
$>mkdir -p .vim/bundle
$>mkdir -p .vim/autoload
$>git clone git://github.com/tpope/vim-pathogen.git ~/.vim/autoload
$ >git clone git://github.com/vim-scripts/vimogen.git ~/vimogen
$>vim .vimogen_repos

Now edit the .vimogen_repos file to this:

git://github.com/vim-scripts/taglist.vim.git
git://github.com/scrooloose/nerdtree.git
git://github.com/ervandew/supertab.git
git://github.com/vim-scripts/fat-finger.git
git://github.com/msanders/snipmate.vim.git
git://github.com/vim-scripts/SingleCompile.git
git://github.com/flazz/vim-colorschemes.git
git://github.com/vim-scripts/Auto-Pairs.git
git://github.com/vim-scripts/errormarker.vim.git
git://github.com/vim-scripts/minibufexpl.vim.git
git://github.com/szw/vim-tags.git

save it (:w), exit vim (:q) and cd to ~/vimogen

$> chmod +x vimogen.sh
$> ./vimogen.sh

Now just press '1', lean back and enjoy the show..

[Vundle]
Again, I assume you have git, ctags and gvim installed, if not install them using sudo apt-get install exuberant-ctags git vim (or consult your man pages for your package manager). This is imho a better way to install plugins and it's easier to manage, plus that you don't have to execute scripts outside vim.

$>cd ~
$>mkdir -p .vim/bundle/vundle
$>git clone git://github.com/gmarik/vundle.git ~/.vim/bundle/vundle

That's it, the rest is taken care of by your .vimrc file which we will start to edit shortly

Another way is to manually download each repo, extract it to ~/.vim/bundle/plugin-name/, but that's just tedious and time consuming, plus the fact that it's super easy to keep your plugins updated.

Now cd back to your home folder, it's time to edit your vim configuration file!

See post 4 for the pathogen/vimogen way
see post 5 for the vundle way
Last edited by Rickmeister on Apr 06, 2013 17:45, edited 1 time in total.
Rickmeister
Posts: 18
Joined: Mar 23, 2013 1:03

Re: Setting up Vim for FreeBasic

Post by Rickmeister »

Windows
(note: I don't have much experience with Vim and Windows, but I'll do my best. Please report errors if you see them)

For Windows things are a little more complicated, as you need to install git, ctags, mingw tools and possibly python aside from the obvious gvim installation. You also need to make sure that all those tools are in your $PATH. To make it easy, you could just download any linux distro and install it alon your windows installation :)

If that's not an option, then continue reading as a blind man will try to walk you through the steps of making this work with Windows7

First, download the essentials:
vim: http://www.vim.org/download.php#pc
ctags; http://sourceforge.net/projects/ctags/f ... or=surfnet
git and curl: https://code.google.com/p/msysgit/downl ... ficial+git (see https://github.com/gmarik/vundle/wiki/V ... or-Windows for installation guide)
mingw: http://www.mingw.org/

Now you need to make sure that your the mingw tools along with ctags and fbc are on your path, see http://www.computerhope.com/issues/ch000549.htm#0 for more detals on how to set it. To explain it easily you need to edit the Path variable to point to the folder of the mingw executables, the ctags executable and the FB compiler.

When you've completed the above steps, open the commandprompt and make sure that you are in your home folder (/user/whatsyourname/).

$> cd /user/whatsyourname
$> git clone http://github.com/gmarik/vundle.git .vim/bundle/vundle
$> gvim _vimrc

Now the worst part is done, see post 5 for how to edit your _vimrc configuration file
Last edited by Rickmeister on Apr 06, 2013 18:22, edited 1 time in total.
Rickmeister
Posts: 18
Joined: Mar 23, 2013 1:03

Re: Setting up Vim for FreeBasic

Post by Rickmeister »

Pathogen/Vimogen

You've gotten this far! Kudos! Now let's open vim/gvim and edit your configuration file
$>cd ~
$>vim .vimrc
OR
$>gvim .vimrc

Code: Select all

" Execute pathogen. This alters the runtimepath (rtp) so that all plugins in .vim/bundle/ are loaded
execute pathogen#infect()

" Make sure so that compatible mode is off, Vi isn't as great as Vim
set nocompatible

" Enable filetype plugins
filetype plugin on
filetype indent on

" Automatically read files when changes outside Vim
set autoread

" Set scrolloff to 2 lines
set so=2

" Show line numbers and position
set ruler
set number

" Set the height of the command bar
set cmdheight=3

" Hide buffers when abandoning them
set hid

" Configure the backspace key
set backspace=eol,start,indent

" Ignore case for searches
set ignorecase

" When searching try to be smart with cases
set smartcase

" Highlight searches
set hlsearch

" Make better searches
set incsearch

" Highlight matching brackets
set showmatch

" How many tenths of a seconds to blink when matching brackets
set mat=2

" Turn off annoying bells and flashes
set noerrorbells
set novisualbell
set t_vb=
set tm=500

" Enable syntax highlighting
enable syntax

" I don't use menus, but if you want to show them edit it here.
" In console mode you can use <F3>  to show the menu
if has("gui_running")
    set guifont=DejaVu\ Sans\ Mono\ 10 
    set guitablabel=%M\ %f
    set guioptions=agirL
    colorscheme mustang
else
    set t_Co=256
    colorscheme colorful256
    source $VIMRUNTIME/menu.vim
    set wildmenu
    set cpo-=<
    set wcm=<C-Z>
    map <F3> :emenu <C-Z>
endif

" Set utf8 as standard encoding
set encoding=utf8

" Use Unix as the standard file type
set ffs=unix,dos,mac

" Set options for the completion popup menu
set completeopt=menu,menuone,preview
set pumheight=15

" If you prefer to use spaces instead of tabs, uncomment this code
" I keep this per file, and I like tabs
"set expandtab

" Be smart with tabs
set smarttab

" 1 tab = 4 spaces
set shiftwidth=4
set tabstop=4

" Set Auto indent, smart indent and line wrap
set ai
set si
set wrap

" Visual mode pressing * or # searches for the current selection
" Super useful! From an idea by Michael Naumann
vnoremap <silent> * :call VisualSelection('f')<CR>
vnoremap <silent> # :call VisualSelection('b')<CR>

" Treat long lines as break lines
map j gj
map k gk

" Map <Space> to / (search) and Ctrl-<Space> to ? (backwards search)
map <space> /
map <c-space> ?

" With a map leader it's possible to do extra key combinations
let g:mapleader = ","

" Fast saving using ',w' in normal mode
nmap <leader>w :w!<cr>

" Shortcut to edit .vimrc
nmap <leader>ev :e $MYVIMRC<cr>

" Disable highlight when <leader><cr> is pressed
map <silent> <leader><cr> :noh<cr>

" Smart way to move between windows
map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l

" Close the current buffer
map <leader>bd :Bclose<cr>

" Close all the buffers
map <leader>ba :1,1000 bd!<cr>

" Switch CWD to the directory of the open buffer
map <leader>cd :cd %:p:h<cr>:pwd<cr>

" Add FreeBasic include path. You might want to edit this
set path+=/usr/local/include/freebasic/**,./**

" Return to last edit position when opening files
autocmd BufReadPost *
     \ if line("'\"") > 0 && line("'\"") <= line("$") |
     \   exe "normal! g`\"" |
     \ endif

" Remember info about open buffers on close
set viminfo^=%

" Always show the status line
set laststatus=2

" Format the status line
set stl=%f\ %m\ %y\ %r\ Line:\ %l/%L[%p%%]\ Col:\ %c\ Buf:\ #%n\ [%b][0x%B]\ %q

" Move a line of text using ALT+[jk] or Comamnd+[jk] on mac. Super useful
nmap <M-j> mz:m+<cr>`z
nmap <M-k> mz:m-2<cr>`z
vmap <M-j> :m'>+<cr>`<my`>mzgv`yo`z
vmap <M-k> :m'<-2<cr>`>my`<mzgv`yo`z

if has("mac") || has("macunix")
  nmap <D-j> <M-j>
  nmap <D-k> <M-k>
  vmap <D-j> <M-j>
  vmap <D-k> <M-k>
endif

" Delete trailing white space on save, useful for Python
func! DeleteTrailingWS()
  exe "normal mz"
  %s/\s\+$//ge
  exe "normal `z"
endfunc
autocmd BufWrite *.py :call DeleteTrailingWS()

" Don't close window, when deleting a buffer
command! Bclose call <SID>BufcloseCloseIt()
function! <SID>BufcloseCloseIt()
   let l:currentBufNum = bufnr("%")
   let l:alternateBufNum = bufnr("#")

   if buflisted(l:alternateBufNum)
     buffer #
   else
     bnext
   endif

   if bufnr("%") == l:currentBufNum
     new
   endif

   if buflisted(l:currentBufNum)
     execute("bdelete! ".l:currentBufNum)
   endif
endfunction

" FreeBASIC settings
augroup freebasic
    au! 
    au BufRead,BufNewFile *.bas,*.bi set ft=freebasic
    " Set completefunc to syntaxcomplete, complete keywords and such
    au BufRead,BufNewFile *.bas,*.bi setlocal omnifunc=syntaxcomplete#Complete
    " Set min lenght to 3 chars before trying to complete.. really, if you need a tip on "for", "as" or "dim" this is wrong for you
    au BufRead,BufNewFile *.bas,*.bi let g:omni_syntax_minimum_length=3 
    " Set the error format so it reports back in a way Vim can understand
    au BufRead,BufNewFile *.bas,*.bi setlocal errorformat=%f(%l)\ error\ %n:\ %m,
    au BufRead,BufNewFile *.bas,*.bi setlocal errorformat+=%f(%l)\ warning\ %n(0):\ %m,
    " I don't like having ' autopaired, turn on if you want
    au BufRead,BufNewFile *.bas,*.bi let b:AutoPairs={'(':')','[':']','{':'}','"':'"'}
    " Configure TagList
    au BufRead,BufNewFile *.bas,*.bi let tlist_freebasic_settings='basic;c:constants;f:functions;t:types;g:enums'
    " Settings for SCCompile to use fbc compiler
    au BufRead,BufNewFile *.bas,*.bi call SingleCompile#SetCompilerTemplate('freebasic','fbc','','fbc','-w all','')
    au BufRead,BufNewFile *.bas,*.bi call SingleCompile#SetOutfile('freebasic', 'fbc', '<%')
    au BufRead,BufNewFile *.bas,*.bi call SingleCompile#ChooseCompiler('freebasic', 'fbc')
    " I keep a 'global' tagfile names fbtags in my home folder, and local will be created in the project folder
    au BufRead,BufNewFile *.bas,*.bi set tags+=./tags,~/fbtags
    " Ignores case
    au BufRead,BufNewFile *.bas,*.bi setlocal ignorecase
    " Autowrite buffer/s before compile or make
    au BufRead,BufNewFile *.bas,*.bi set autowrite
augroup END

" Make Taglist use right window, default to left side and that's where I keep my NERDTree
let Tlist_Use_Right_Window=1
" Set F9 to toggle Taglist
map <F9> :TlistToggle<cr>
" Set F12 to toggle NERDTree
map <F12> :NERDTreeToggle<cr>
" set F5 to compile current file and dump output file into /tmp
map <F5> :SCCompileAF -c -w all -o /tmp/%<.o<cr>
" make project
map <F6> :make<cr>:<cr>

" man-pages opens in split window. Shortcut <K>
" Really useful as most library functions has the same name as their C siblings,
" just put the cursor in the name of the function and press shift-k (in normal mode) and the manpage
" opens in a split window
let $GROFF_NO_SGR=1
source $VIMRUNTIME/ftplugin/man.vim
nmap K :Man <cword><CR>

" Minibufexplorer settings
let g:miniBufExplMapWindowNavVim=1
let g:miniBufExplMapWindowNavArrows=1
let g:miniBufExplMapCTabSwitchBufs=1
let g:miniBufExplModSelTarget=1

" Shameless cut'n'paste from vim wiki, only good for linux and xxd
" ex command for toggling hex mode - define mapping if desired
command! -bar Hexmode call ToggleHex()
map <F2> :Hexmode<cr>
" helper function to toggle hex mode
function! ToggleHex()
  " hex mode should be considered a read-only operation
  " save values for modified and read-only for restoration later,
  " and clear the read-only flag for now
  let l:modified=&mod
  let l:oldreadonly=&readonly
  let &readonly=0
  let l:oldmodifiable=&modifiable
  let &modifiable=1
  if !exists("b:editHex") || !b:editHex
    " save old options
    let b:oldft=&ft
    let b:oldbin=&bin
    " set new options
    setlocal binary " make sure it overrides any textwidth, etc.
    let &ft="xxd"
    " set status
    let b:editHex=1
    " switch to hex editor
    %!xxd
  else
    " restore old options
    let &ft=b:oldft
    if !b:oldbin
      setlocal nobinary
    endif
    " set status
    let b:editHex=0
    " return to normal editing
    %!xxd -r
  endif
  " restore values for modified and read only state
  let &mod=l:modified
  let &readonly=l:oldreadonly
  let &modifiable=l:oldmodifiable
endfunction
When finished editing
:w
:so %

The 'so %' is short for 'source' and '%' is the name of the current file - vim loads the file and 'executes' it. note that some changes might not occur before you restart vim.
Last edited by Rickmeister on Apr 06, 2013 19:32, edited 2 times in total.
Rickmeister
Posts: 18
Joined: Mar 23, 2013 1:03

Re: Setting up Vim for FreeBasic

Post by Rickmeister »

Ignore this post....
Last edited by Rickmeister on Apr 06, 2013 19:33, edited 1 time in total.
Rickmeister
Posts: 18
Joined: Mar 23, 2013 1:03

Re: Setting up Vim for FreeBasic

Post by Rickmeister »

Vundle my bundles!

We keep most of the vimrc shown in post 4, we only need to alter it a little bit

Code: Select all

" Shameless rip from vundle's github README
set nocompatible
filetype off

set rtp+=~/.vim/bundle/vundle/
call vundle#rc()

" let Vundle manage Vundle
" required! 
Bundle 'gmarik/vundle'

" My Bundles here:
"
" original repos on github
Bundle 'tpope/vim-fugitive'
Bundle 'msanders/snipmate.vim'
Bundle 'scrooloose/nerdtree'
Bundle 'ervandew/supertab'
Bundle 'flazz/vim-colorschemes'
Bundle 'szw/vim-tags'
" vim-scripts repos
Bundle 'taglist.vim'
Bundle 'Auto-Pairs'
Bundle 'SingleCompile'
Bundle 'fat-finger'
Bundle 'errormarker.vim'
Bundle 'minibufexpl.vim'

" The rest of the file is the same as the post above

" Enable filetype plugins
filetype plugin on
filetype indent on

" Automatically read files when changes outside Vim
set autoread

" Set scrolloff to 2 lines
set so=2

" Show line numbers and position
set ruler
set number

" Set the height of the command bar
set cmdheight=3

" Hide buffers when abandoning them
set hid

" Configure the backspace key
set backspace=eol,start,indent

" Ignore case for searches
set ignorecase

" When searching try to be smart with cases
set smartcase

" Highlight searches
set hlsearch

" Make better searches
set incsearch

" Highlight matching brackets
set showmatch

" How many tenths of a seconds to blink when matching brackets
set mat=2

" Turn off annoying bells and flashes
set noerrorbells
set novisualbell
set t_vb=
set tm=500

" Enable syntax highlighting
enable syntax

" I don't use menus, but if you want to show them edit it here.
" In console mode you can use <F3>  to show the menu
if has("gui_running")
    set guifont=DejaVu\ Sans\ Mono\ 10 
    set guitablabel=%M\ %f
    set guioptions=agirL
    colorscheme mustang
else
    set t_Co=256
    colorscheme colorful256
    source $VIMRUNTIME/menu.vim
    set wildmenu
    set cpo-=<
    set wcm=<C-Z>
    map <F3> :emenu <C-Z>
endif

" Set utf8 as standard encoding
set encoding=utf8

" Use Unix as the standard file type
set ffs=unix,dos,mac

" Set options for the completion popup menu
set completeopt=menu,menuone,preview
set pumheight=15

" If you prefer to use spaces instead of tabs, uncomment this code
" I keep this per file, and I like tabs
"set expandtab

" Be smart with tabs
set smarttab

" 1 tab = 4 spaces
set shiftwidth=4
set tabstop=4

" Set Auto indent, smart indent and line wrap
set ai
set si
set wrap

" Visual mode pressing * or # searches for the current selection
" Super useful! From an idea by Michael Naumann
vnoremap <silent> * :call VisualSelection('f')<CR>
vnoremap <silent> # :call VisualSelection('b')<CR>

" Treat long lines as break lines
map j gj
map k gk

" Map <Space> to / (search) and Ctrl-<Space> to ? (backwards search)
map <space> /
map <c-space> ?

" With a map leader it's possible to do extra key combinations
let g:mapleader = ","

" Fast saving using ',w' in normal mode
nmap <leader>w :w!<cr>

" Shortcut to edit .vimrc
nmap <leader>ev :e $MYVIMRC<cr>

" Disable highlight when <leader><cr> is pressed
map <silent> <leader><cr> :noh<cr>

" Smart way to move between windows
map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l

" Close the current buffer
map <leader>bd :Bclose<cr>

" Close all the buffers
map <leader>ba :1,1000 bd!<cr>

" Switch CWD to the directory of the open buffer
map <leader>cd :cd %:p:h<cr>:pwd<cr>

" Add FreeBasic include path. You might want to edit this
set path+=/usr/local/include/freebasic/**,./**

" Return to last edit position when opening files
autocmd BufReadPost *
     \ if line("'\"") > 0 && line("'\"") <= line("$") |
     \   exe "normal! g`\"" |
     \ endif

" Remember info about open buffers on close
set viminfo^=%

" Always show the status line
set laststatus=2

" Format the status line
set stl=%f\ %m\ %y\ %r\ Line:\ %l/%L[%p%%]\ Col:\ %c\ Buf:\ #%n\ [%b][0x%B]\ %q

" Move a line of text using ALT+[jk] or Comamnd+[jk] on mac. Super useful
nmap <M-j> mz:m+<cr>`z
nmap <M-k> mz:m-2<cr>`z
vmap <M-j> :m'>+<cr>`<my`>mzgv`yo`z
vmap <M-k> :m'<-2<cr>`>my`<mzgv`yo`z

if has("mac") || has("macunix")
  nmap <D-j> <M-j>
  nmap <D-k> <M-k>
  vmap <D-j> <M-j>
  vmap <D-k> <M-k>
endif

" Delete trailing white space on save, useful for Python
func! DeleteTrailingWS()
  exe "normal mz"
  %s/\s\+$//ge
  exe "normal `z"
endfunc
autocmd BufWrite *.py :call DeleteTrailingWS()

" Don't close window, when deleting a buffer
command! Bclose call <SID>BufcloseCloseIt()
function! <SID>BufcloseCloseIt()
   let l:currentBufNum = bufnr("%")
   let l:alternateBufNum = bufnr("#")

   if buflisted(l:alternateBufNum)
     buffer #
   else
     bnext
   endif

   if bufnr("%") == l:currentBufNum
     new
   endif

   if buflisted(l:currentBufNum)
     execute("bdelete! ".l:currentBufNum)
   endif
endfunction

" FreeBASIC settings
augroup freebasic
    au! 
    au BufRead,BufNewFile *.bas,*.bi set ft=freebasic
    " Set completefunc to syntaxcomplete, complete keywords and such
    au BufRead,BufNewFile *.bas,*.bi setlocal omnifunc=syntaxcomplete#Complete
    " Set min lenght to 3 chars before trying to complete.. really, if you need a tip on "for", "as" or "dim" this is wrong for you
    au BufRead,BufNewFile *.bas,*.bi let g:omni_syntax_minimum_length=3 
    " Set the error format so it reports back in a way Vim can understand
    au BufRead,BufNewFile *.bas,*.bi setlocal errorformat=%f(%l)\ error\ %n:\ %m,
    au BufRead,BufNewFile *.bas,*.bi setlocal errorformat+=%f(%l)\ warning\ %n(0):\ %m,
    " I don't like having ' autopaired, turn on if you want
    au BufRead,BufNewFile *.bas,*.bi let b:AutoPairs={'(':')','[':']','{':'}','"':'"'}
    " Configure TagList
    au BufRead,BufNewFile *.bas,*.bi let tlist_freebasic_settings='basic;c:constants;f:functions;t:types;g:enums'
    " Settings for SCCompile to use fbc compiler
    au BufRead,BufNewFile *.bas,*.bi call SingleCompile#SetCompilerTemplate('freebasic','fbc','','fbc','-w all','')
    au BufRead,BufNewFile *.bas,*.bi call SingleCompile#SetOutfile('freebasic', 'fbc', '<%')
    au BufRead,BufNewFile *.bas,*.bi call SingleCompile#ChooseCompiler('freebasic', 'fbc')
    " I keep a 'global' tagfile names fbtags in my home folder, and local will be created in the project folder
    au BufRead,BufNewFile *.bas,*.bi set tags+=./tags,~/fbtags
    " Ignores case
    au BufRead,BufNewFile *.bas,*.bi setlocal ignorecase
    " Autowrite buffer/s before compile or make
    au BufRead,BufNewFile *.bas,*.bi set autowrite
augroup END

" Make Taglist use right window, default to left side and that's where I keep my NERDTree
let Tlist_Use_Right_Window=1
" Set F9 to toggle Taglist
map <F9> :TlistToggle<cr>
" Set F12 to toggle NERDTree
map <F12> :NERDTreeToggle<cr>
" set F5 to compile current file and dump output file into /tmp
map <F5> :SCCompileAF -c -w all -o /tmp/%<.o<cr>
" make project
map <F6> :make<cr>:<cr>

" man-pages opens in split window. Shortcut <K>
" Really useful as most library functions has the same name as their C siblings,
" just put the cursor in the name of the function and press shift-k (in normal mode) and the manpage
" opens in a split window
let $GROFF_NO_SGR=1
source $VIMRUNTIME/ftplugin/man.vim
nmap K :Man <cword><CR>

" Minibufexplorer settings
let g:miniBufExplMapWindowNavVim=1
let g:miniBufExplMapWindowNavArrows=1
let g:miniBufExplMapCTabSwitchBufs=1
let g:miniBufExplModSelTarget=1

" Shameless cut'n'paste from vim wiki, only good for linux and xxd
" ex command for toggling hex mode - define mapping if desired
command! -bar Hexmode call ToggleHex()
map <F2> :Hexmode<cr>
" helper function to toggle hex mode
function! ToggleHex()
  " hex mode should be considered a read-only operation
  " save values for modified and read-only for restoration later,
  " and clear the read-only flag for now
  let l:modified=&mod
  let l:oldreadonly=&readonly
  let &readonly=0
  let l:oldmodifiable=&modifiable
  let &modifiable=1
  if !exists("b:editHex") || !b:editHex
    " save old options
    let b:oldft=&ft
    let b:oldbin=&bin
    " set new options
    setlocal binary " make sure it overrides any textwidth, etc.
    let &ft="xxd"
    " set status
    let b:editHex=1
    " switch to hex editor
    %!xxd
  else
    " restore old options
    let &ft=b:oldft
    if !b:oldbin
      setlocal nobinary
    endif
    " set status
    let b:editHex=0
    " return to normal editing
    %!xxd -r
  endif
  " restore values for modified and read only state
  let &mod=l:modified
  let &readonly=l:oldreadonly
  let &modifiable=l:oldmodifiable
endfunction
When finished editing
:w
:so %

The 'so %' is short for 'source' and '%' is the name of the current file - vim loads the file and 'executes' it. note that some changes might not occur before you restart vim.
Rickmeister
Posts: 18
Joined: Mar 23, 2013 1:03

Re: Setting up Vim for FreeBasic

Post by Rickmeister »

Snippets

SnipMate is a really powerful tool, and when searching for documentation for writing these posts I found an even better plugin. More on that later. But it isn't as useful if you don't supply some snippets for it to do it's magic on, so here it is: freebasic.snippet

Code: Select all

snippet type User defined Type
	type ${1:}
		${2}
	end type
snippet typee User defined Type2 Extends Type1
	type ${1:} extends ${2:}
		${3}
	end type
snippet dim
	dim ${1:sym} as ${2:type}${3}
snippet byv ByVal
	byval ${1:sym} as ${2:type}${3}
snippet byr ByRef
	byref ${1:sym} as ${2:type}${3}
snippet as Declare variables on the fly
	${1:sym} as ${2:type}${3}
snippet if
	if ${1:condition} then
		${2}
	endif
snippet enum
	enum ${1:}
		${2}
	end enum
snippet union
	union ${1}
		${2}
	end union
snippet ifelse
	if ${1:condition} then
		${3:}
	else ${2}
		${4}
	endif
snippet elif
	if ${1:condition} then
		${3:}
	elseif ${2:condition}
		${4}
	endif
snippet sel
	select case ${1:}
	
		case ${2:}
			${3}
		case else
	end select
snippet case
	case ${1:}
		${2}
snippet dow Do-While loop
	do While ${1}
		${2}
	loop
snippet dou Do-Until loop
	do until ${1}
		${2}
	loop
snippet while While-Wend loop
	while ${1:}
		${2}
	wend
snippet dsub
	declare sub ${1:}(${2:})${3}
snippet dfun
	declare function ${1:}(${3:}) as ${2:}${4}
snippet fun
	function ${1:}(${3:}) as ${2:}${4}
		${4}
	end function
snippet sub
	sub ${1:name}(${2:})
		${3}
	end sub
snippet bv
	byval ${1:} as ${2:}
snippet br
	byref ${1:} as ${2:}
snippet for For-Next loop
	for ${1:i}=${2:0} to ${3:}
		${4}
	next $1
snippet fors For-Next-Step loop
	for ${1:i}=${2:0} to ${3} step ${4:1}
		${5}
	next $1
snippet once Include Guard
	#ifndef ${1:`toupper(Filename('$1_BI', 'UNTITLED_BI'))`}
	#define $1

	${2}

	#endif
snippet inc
	#include "${1:`Filename("$1.bi")`}"${2}	
snippet const
	const ${1:} as ${2:} = ${3}
snippet static
	static ${1:} as ${2:}${3}
snippet dprop
	declare property ${1:}($2)
snippet prop
	property ${1:Type}.${2:prop}(${3})

	end property
snippet dcons
	declare constructor(${1})
snippet constr
	constructor ${1:Type}(${2})
	end Constructor
snippet ddest
	declare destructor()${1}
snippet dest
	destructor ${1:Type}()
		${2}
	end destructor
snippet license
	''  	
	''	${1:project}
	''	${2:short_desc}
	''	
	''	Copyright 2013 ${3:Your name and email}
	''	This work is free. You can redistribute it and/or modify it under the
	''	terms of the Do What The #%$@ You Want To Public License, Version 2,
	''	as published by Sam Hocevar. See http://www.wtfpl.net/ for more details
	'' 
	${4}
Save the file as .vim/bundle/snipmate/snippets/freebasic.snippets

Indentation
Vim's built-in indentation offers much to ask for, I edited the VB one to suit my needs

Code: Select all

" Vim indent file
" Language:	VisualBasic (ft=vb) / Basic (ft=basic) / SaxBasic (ft=vb)
" Author:	Johannes Zellner <johannes@zellner.org>
" Last Change:	Fri, 18 Jun 2004 07:22:42 CEST
"		Small update 2010 Jul 28 by Maxim Kim
"	    Small update 2013 Mar 22 by Rickard Isaksson for use with FreeBasic
if exists("b:did_indent")
    finish
endif
let b:did_indent = 1

setlocal autoindent
setlocal indentexpr=FreeBasicGetIndent(v:lnum)
setlocal indentkeys&
setlocal indentkeys+==~else,=~elseif,=~end,=~wend,=~case,=~next,=~select,=~loop,<:>

let b:undo_indent = "set ai< indentexpr< indentkeys<"

" Only define the function once.
if exists("*FreeBasicGetIndent")
    finish
endif

fun! FreeBasicGetIndent(lnum)
    " labels and preprocessor get zero indent immediately
    let this_line = getline(a:lnum)
    let LABELS_OR_PREPROC = '^\s*\(\<\k\+\>:\s*$\|#.*\)'
    if this_line =~? LABELS_OR_PREPROC
	return 0
    endif
    " Find a non-blank line above the current line.
    " Skip over labels and preprocessor directives.
    let lnum = a:lnum
    while lnum > 0
	let lnum = prevnonblank(lnum - 1)
	let previous_line = getline(lnum)
	if previous_line !~? LABELS_OR_PREPROC
	    break
	endif
    endwhile

    " Hit the start of the file, use zero indent.
    if lnum == 0
	return 0
    endif

    let ind = indent(lnum)

    " Add
    if previous_line =~? '^\s*\<\(type\|begin\|\%(\%(private\|public\|friend\)\s\+\)\=\%(function\|sub\|property\)\|select\|case\|if\|else\|elseif\|do\|for\|while\|enum\|with\)\>'
	let ind = ind + &sw
    endif

    " Subtract
    if this_line =~? '^\s*\<end\>\s\+\<select\>'
	if previous_line !~? '^\s*\<select\>'
	    let ind = ind - 2 * &sw
	else
	    " this case is for an empty 'select' -- 'end select'
	    " (w/o any case statements) like:
	    "
	    " select case readwrite
	    " end select
	    let ind = ind - &sw
	endif
    elseif this_line =~? '^\s*\<\(end\|else\|elseif\|until\|loop\|next\|wend\)\>'
	let ind = ind - &sw
    elseif this_line =~? '^\s*\<\(case\)\>'
	if previous_line !~? '^\s*\<select\>'
	    let ind = ind - &sw
	endif
    endif

    return ind
endfun

" vim:sw=4
Save it as "freebasic.vim" in .vim/indent
Rickmeister
Posts: 18
Joined: Mar 23, 2013 1:03

Re: Setting up Vim for FreeBasic

Post by Rickmeister »

Reserved for more Vim-tastic tips and tricks
Rickmeister
Posts: 18
Joined: Mar 23, 2013 1:03

Re: Setting up Vim for FreeBasic

Post by Rickmeister »

Currently working on a FreeBasic project manager plugin for Vim that will have the following features:

* Set main module with a simple keypress
* Add or remove sourcefiles/targets from the build in an easy way, and provide a tree-view of the project
* Add sub projects or other dependencies, in short all that has a makefile can be made a sub project
* Automatically (and recursivly) build makefile/s for the project, walking the source tree and resolving dependecies for each sourcefile in the folder and setting up one build target per file.
* Easily manage debug and release build configurations.

Still in it's early days, and right now it's all but user friendly, buggy and not mature enough for public use.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Setting up Vim for FreeBasic

Post by TJF »

Dear Rickmeister,

thanks for your effort and thanks for sharing you work.

Do you know that you'll get all of these features out-of-the-box by using Geany (and some important additional features as well -- not buggy and mature enough for public use).

I hardly can imagine why anybody should use a text-only editor in our days. One have to study the documentation just to exit this program.
Rickmeister
Posts: 18
Joined: Mar 23, 2013 1:03

Re: Setting up Vim for FreeBasic

Post by Rickmeister »

TJF wrote:Dear Rickmeister,

thanks for your effort and thanks for sharing you work.

Do you know that you'll get all of these features out-of-the-box by using Geany (and some important additional features as well -- not buggy and mature enough for public use).

I hardly can imagine why anybody should use a text-only editor in our days. One have to study the documentation just to exit this program.
You must be thinking of Emacs..

I can hardly see why I would use Geany over Vim. We've been close for 20 years, hard to part with it.
I use it to read and write emails, clone git repos, blogging, twitter, and ofcourse when I'm programming in C, C++, Python, Java, JavaScript, Groovy - and just recently for FreeBasic - infact whenever I do something that involves text editing. And I belive that the usebase of Vim is a bit larger then Geany's...
zelurker
Posts: 1
Joined: Nov 22, 2018 23:53

Re: Setting up Vim for FreeBasic

Post by zelurker »

A very long time after the 1st post in this thread, but anyway...
Since I didn't know SingleCompile, it took me a while to find out while SCCompileRun command didn't actually run anything after the compilation.
Solution is in the compiler template in .vimrc :
au BufRead,BufNewFile *.bas,*.bi call SingleCompile#SetCompilerTemplate('freebasic','fbc','FreeBasic','fbc','-w all','./%:r')
that's what you should have, for linux at least.
With this the f10 key indeed runs the program !

(it's nice to be able to use vim as a gui to get to know freebasic since the guys who created fbide is windows only... !
With this, I confirm, it's very usable, thanks particularly for the finding about indentation !)
Post Reply