0

Aloha,

I took the plunge to start using vim as my main editor. I'm a week in, and beginning to see the beauty of it. It's now the only editor I use already.

Anyhoot - I found a very useful mapping on stackoverflow that lets me run python files within vim. The only issue is that it only works on the first file I open. If I use :tabe myfile.py to open another python file, pressing the F7 key binding doesn't work on the newly opened tab. Any way to make F7 work for all open tabs?

Edit: And just in case it's needed, here is my complete .vimrc file:

""""""""""' VUNDLE SETTINGS AND PLUGINS '""""""""""
            """""""""""""""""""""""""""

set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'

Plugin 'davidhalter/jedi-vim'

Plugin 'scrooloose/nerdtree'

Plugin 'tpope/vim-fugitive'

Plugin 'jmcantrell/vim-virtualenv'

call vundle#end()

""""""""""' COLOR SCHEME SETTINGS '"""""""""
        """""""""""""""""""""
syntax on
colorscheme skittles_autumn


""""""""""' PYTHON SETTINGS '""""""""""
            """""""""""""""
let python_highlight_all=1
syntax on

"Run python script with F7
nnoremap <buffer> <F7> :exec '!python' shellescape(@%, 1)<cr>

""""""""""' KEY BINDINGS VANILLA VIM '"""""""""
            """"""""""""""""""""""""
"split screen navigations
nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>
inoremap jj <ESC>

""""""""""' SETTINGS VANILLA VIM '""""""""""
            """"""""""""""""""""
set number
set undofile
let mapleader=" "

" Set tabs to 4 spaces and stuff
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab

set wrap
set textwidth=79
set formatoptions=qrn1
set colorcolumn=79

set list
"Use the same symbols as TextMate for tabstops and EOLs
set listchars=tab:▸\ ,eol:¬

" Disable arrow keys
nnoremap <up> <nop>
nnoremap <down> <nop>
nnoremap <left> <nop>
nnoremap <right> <nop>
inoremap <up> <nop>
inoremap <down> <nop>
inoremap <left> <nop>
inoremap <right> <nop>

" j,k move by screen line instead of file line
nnoremap j gj
nnoremap k gk
nmap <silent> <F3> :NERDTreeToggle<CR>

"Show hidden files in NerdTree
let NERDTreeShowHidden=1

" Open up current file in web browsers
nnoremap <C-g> :!google-chrome %<CR> " browser preview with ctrl-p
nnoremap <C-f> :!firefox %<CR> " browser preview with ctrl-o
nnoremap <C-c> :!chromium-browser %<CR> " browser preview with ctrl-c

" Go to tab by number
noremap <leader>1 1gt
noremap <leader>2 2gt
noremap <leader>3 3gt
noremap <leader>4 4gt
noremap <leader>5 5gt
noremap <leader>6 6gt
noremap <leader>7 7gt
noremap <leader>8 8gt
noremap <leader>9 9gt
noremap <leader>0 :tablast<cr>

1 Answer 1

3

Your mapping use <buffer> which means that it creates a mapping which is local to a buffer (and thus doesn't exists in other buffers). As the command is in your vimrc the mapping only exists on the first opened buffer.

So to solve your problem you have two solutions:

The first one is to remove <buffer> this way the mapping will work on all buffers. The problem with this method is that the mapping will be available also for non python buffers.

The second solution is to use an autocommand to create the mapping on all python buffers like this (Actually that's what @Kent suggested on the answer of the question you linked):

autocmd! FileType python nnoremap <buffer> <F7> :exec '!python' shellescape(@%, 1)<cr>

You might want to read the following help topics:


EDIT AS @Octaviour pointed it out in the comment there is a better alternative to the autocommand: using the ftplugin.

The filetype plugins are files which are sourced when the filetype of a buffer is set. Here the ftplugin to modify would be the file ~/.vim/ftplugin/python.vim. One would simply need to add the line to this file:

nnoremap <buffer> <F7> :exec '!python' shellescape(@%, 1)<cr>

For reference see :h ftplugin.

3
  • 1
    Option 2b would be to do the mapping in a filetype plugin (:h ftplugins), which is basically the file ~/.vim/ftplugin/python.vim in your case. I find this to be a nice way to put filetype specific plugins. From what I heard it is also a bit faster to load than the autocommands.
    – Octaviour
    Jan 19, 2017 at 14:41
  • @Octaviour option 2b is indeed the best solution. Do you mind if I edit my answer to add it?
    – statox
    Jan 19, 2017 at 14:43
  • 1
    @statox thank you. Will be great to see an update with option 2b. Jan 20, 2017 at 2:55

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.