0

I code CSS and Python and for CSS I have the following little time saver

inoremap :      :;<Left>

This is great until I start coding in Python. Every time I hit : I get an unwanted ; I should mention that to make editing in Python pleasant with proper indentation I added

~/.vim/ftplugin/python.vim

python.vim contains the following

setlocal tabstop=4
setlocal softtabstop=4
setlocal shiftwidth=4
setlocal textwidth=80
setlocal smarttab
setlocal expandtab  

What code would I put into python.vim to override inoremap : :;<Left> from my .vimrc so that when I press : all I get is a single :?

4 Answers4

1

All your filetype-specific settings should go into:

~/.vim/after/ftplugin/<language>.vim

With this setup, your settings are "guaranteed" to be applied, cleanly, after any default ftplugin.

Put your Python-specific settings:

setlocal tabstop=4
setlocal softtabstop=4
setlocal shiftwidth=4
setlocal textwidth=80
setlocal smarttab
setlocal expandtab

into this file:

~/.vim/after/ftplugin/python.vim

and your CSS-specific mapping:

inoremap <buffer> : :;<Left>

into this file:

~/.vim/after/ftplugin/css.vim
romainl
  • 186,200
  • 21
  • 280
  • 313
0

You should check out :help map-local.

You could put this in python.vim:

iunmap <buffer> :

Or alternatively, only put the : mapping in css.vim:

inoremap <buffer> : :;<Left>
Daan Bakker
  • 6,122
  • 3
  • 24
  • 23
0
You can put this in your python.vim:
   iunmap :

As mentioned by Daan, you can also put the mapping for : in css specific vim file ( for example ~/.vim/syntax/css.vim )

Thava
  • 1,597
  • 17
  • 13
  • I liked this method because I use `inoremap : :;` for actually .scss .css .js files. Its nice to define that mapping in one place because of this. `iumap :` worked. –  Jan 08 '13 at 21:23
0

You can do this with autocommands in your .vimrc

autocmd FileType css inoremap <silent> <buffer> : :;<Left>

This will only take effect when editing a file Vim knows is CSS (:set ft? gives you back CSS)

If you're new to autocommands, the Vim docs on autocommands are a recommended read.

mattr-
  • 5,333
  • 2
  • 23
  • 28
  • Does not work properly for me - if I set up e.g. autocommands for sh and python filetypes that map keys, and I open a new python file, the python mappings work fine. If I then open a shell script, the mappings change as expected to the `sh` filetype. But then switching back to the python buffer, the `sh` mappings remain active. – Michael Beer Sep 10 '20 at 15:49