I'm using Ubuntu13.10 My $HOME is /home/mario.
I execute vim in the terminal and used :e $MYVIMRC to set up my own preference.
I've test it inside that VIMRC file by typing :so $MYVIMRC, it works!
But later when I changed my directory to /home/mario/LPTH and use vim ex45.py to open my python exercises.
Then I found I can't apply it to my python file by typing :so $MYVIMRC or :source $MYVIMRC
I even restart my computer, still can't work.
What happend? How can I apply the VIMRC to my python docuemnt?
Thanks for your help.
---------------------------addtional information part 1---------------------------------
Yesterday when I raised up this question, I've already set several configurations. such as:
set number
set expandtab
set tabstop=8
set shiftwidth=4
set softtabstop=4
set autoindent
:syntax on
I can apply my configurations for this MYVIMRC file. which shows code numbers,for example. But when I open my python files in ~/LPTH. nothing happens. I checked this morning, still the same.
--------------------addtional information part2--------------------------
Obstacle hacked!!!!!
Previously: I followed tutor from http://vim.wikia.com/wiki/Vimrc,my execution is:
cd ~ ---> vim ---> :e $MYVIMRC ---> add my setting.
And it caused the problem.
This morning: I did the following executions.
cd ~ ---> vim .vimrc ---> add my setting.
And it works. Just that simple.
Thank you guys for your help
You don't (need to) apply your .vimrc to a file. The configuration and customization commands that you put in there are automatically executed by Vim on startup. You should only put general stuff like :set listchars=... backupdir=... in there, and any general :mappings.
Some settings are specific to a programming language like Python; Vim has buffer-local options (like :setlocal expandtab) and mappings; these can be set by filetype plugins. Some people define those via :autocmd FileType python setlocal ... directly in their ~/.vimrc, but it's better to just configure :filetype plugin on there and then place the specific options in ~/.vim/after/ftplugin/python.vim, for example.
As pointed out already you dont need to apply your .vimrc to a file. You just need to have your .vimrc file in your $HOME directory, it will automatically be executed by vim.
This is the .vimrc i would reccomend for Python
set nobackup
set nowritebackup
set noswapfile
set lines=40
set columns=80
set tabstop=4
set shiftwidth=4
set softtabstop=4
set autoindent
set smartindent
set smarttab
set retab
set autowrite
set autochdir
filetype indent on
filetype on
filetype plugin on
Related
I frequntly open many files in vim. The problem is some files have their own options. For example:
settings for make files
set noexpandtab tabstop=4 shiftwidth=4 softtabstop=4
set list listchars=eol:¬,tab:→⠀,trail:~,extends:>,precedes:<
hi SpecialKey ctermfg=243
hi NonText ctermfg=243
settings for C files
set colorcolumn=100 tabstop=4 shiftwidth=4 softtabstop=4
So, if I open makefile first and then :tabe main.c I can see invisble character that were turned on from makefile.
I understand that I can :set nolist but imagine if I have many other options inherit from many other files. How can I tell vim not to inherit them when I :tabe anotherFile?
Well there is setlocal to set this things for a buffer only.
But that does of course not work for a .vimrc. There you have two possibilities, autocmds or filetype plugins:
autocmd BufRead,BufNewFile *.{c} setlocal colorcolumnt=100
This does set the settings local to any oppened buffer of a file ending with .c
Or you can add the settings to a filetype-plugin. See :h filetype-plugin for more information on that. Basically just add the settings to a specific file. In your case most likely under .vim/ftplugin/c.vim
Here's my ~/.vimrc on some machine:
set softtabstop=0 noexpandtab nosmarttab
set shiftwidth=4
set tabstop=4
colorscheme murphy
... but when I edit files, tabs show up as 8 spaces, and typing in a tab produces for spaces. I looked around and found this question:
set expandtab in .vimrc not taking effect
but I don't have set paste in my ~/.vimrc, so that can't be the problem. What's causing this? And how can I enforce my tab preferences?
Edit: It seems my settings are only ignored for some filetypes, not for simple text files. How can I force my settings to apply to all filetypes? Or at least to specific types?
Solution for a specific file type (rather than for all file types):
For file type foo Add the following to your ~/.vimrc:
augroup foo
autocmd!
autocmd FileType foo setlocal softtabstop=0 noexpandtab nosmarttab shiftwidth=4 tabstop=4
augroup END
and replace foo with the file type in both occurrences.
Note: foo is not necessarily a file extension. For example, the Python language (.py typically), file type is python.
when I run
vim good.html
and
:verbose set et?
expandtab
Last set from ~/.vimrc
but when I run
vim good.py
and
:verbose set et?
expandtab
Last set from /usr/share/vim/vim74/ftplugin/python.vim
I want apply ~/.vimrc file in .py, not python.vim
Yesterday I all is fine but today suddenly path was changed
please someone teach me how can I change the path
Put this into your .vimrc:
autocmd VimEnter *.py set expandtab
or if you want to have the configuration of .vimrc to be executed after all plugins being loaded - in case they have changed some settings -, you can add this line:
autocmd VimEnter * source ~/.vimrc
Note: It could have a side-effect depending on the content of your .vimrc because the latter actually will be executed twice (at the begining and at the end of vim startup) so you need to consider that.
Concerning Plugins now, if they are logged in some specific folders like .vim or vim installation path they will be loaded automatically unless you removed them or run some specific commands to be ignored.
Vim has also the ability to detect the type of file which is being edited, and this occurs when the option filetype is activated and probably this is what happened to you.
So typing :filetype will confirm that. Maybe you can desactivate it for some specific files if you wish. It is up to you !
:help VimEnter
VimEnter
VimEnter After doing all the startup stuff, including
loading .vimrc files, executing the "-c cmd"
arguments, creating all windows and loading
the buffers in them.
I have a stupid question but I am stuck.
I do set set ts=4 in my .vimrc file, but it looks not work. Open a new file, tab is still extended as 3 spaces. And under command mode, "set ts" got a "tabstop=3". Is there a final file like .vimrc that overwrite my ts?
Even I type :set ts=4, in vim, when I type tab, it only has 3 spaces in length.
Here is my suspicious options that cause this annoying result in my .vimrc
set autoindent
set shiftwidth=4
set softtabstop=4
set backspace=2
Thanks.
Try this:
:verbose set tabstop?
in vim, it will tell you where the tabstop option value is coming from.
For some reason, Vim keeps using soft-tabs (tabs as spaces) everytime I'm working on Haml files. I prefer regular tabs and have the following in my vimrc:
set autoindent
set noexpandtab
set tabstop=4
set shiftwidth=4
Which seems to work great for every file type except for Haml. When editing Haml files Vim uses two spaces rather than a tab, any suggestions as to how this can be reverted back to my default settings (ie. regular tabs)?
It was because the indent script for haml sets expandtab for you automatically when VIM has detected the current file is in haml format. The script is located at $VIMRUNTIME/indent/haml.vim. It contains:
setlocal autoindent sw=2 et
To disable that, you can put this line in your ~/.vimrc to clear the expandtab setting again:
au! FileType haml set noet