I'm not sure if this is the right place to ask about this, but I figured it couldn't hurt to ask here. I am using a plugin called auto-close so that I don't have to close my own parentheses. It has a very nice feature that does the following:
This is a great feature, but I don't like how far it indents for me.
I have the following line in my .vimrc:
" for filetype "js", tab = insert 4 spaces, backspace will delete all 4
autocmd Filetype javascript setlocal expandtab softtabstop=4
In editing a javascript file, it automatically did an 8-space indentation instead of a 4-space indenation, as I've specified in my .vimrc. Can anybody help me figure out how I can make it automatically indent 4-space tabs instead of 8-space tabs? I can't find it in the documentation either. Thanks!
If you get shiftwidth=8, softtabstop=0, tabstop=8, that means that your autocmd FileType didn't take effect. You'd have to troubleshoot that.
I would recommend putting any settings, mappings, and filetype-specific autocmds into ~/.vim/ftplugin/{filetype}_whatever.vim (or {filetype}/whatever.vim; cp. :help ftplugin-name) instead of defining lots of :autocmd FileType {filetype}; it's cleaner and scales better; requires that you have :filetype plugin on, though. Settings that override stuff in default filetype plugins should go into ~/.vim/after/ftplugin/{filetype}.vim instead. The change of indent settings would fit the latter, after directory location.
Related
When writing code in vim, I've noticed that it sometimes messes up the indentation of the current line after pressing return. (In the gif, I go into insert mode, with the cursor at the end of the date function. When I press return, the whole "echo date('Y');" part looses its indentation when it should not).
I have a ton of directives in my vimrc to try and stop all kinds of autoindentation, but much to my irritation, the problem persists.
filetype plugin indent off
filetype plugin off
set noautoindent
set nosmartindent
set nocindent
set indentexpr=''
let b:did_indent = 1
set ft?
filetype indent off
I wish there was a way to have vim indent files much like your "more standard" text editors, whereby they don't try to be smart, but just maintain the current indentation of the current line in the new line. Like this:
Is this possible?
set smartindent is only needed on my VIM to get the effect you shown by gedit.
So it seems that this line was causing the first issue displayed in the vim. Its part of the Vundle plugin manager inclusion code:
filetype plugin indent on
Ensuring that my indent rules were after this fixed the problem completely.
I found this nice plugin for distraction free writing named Goyo, which is really well done.
I setup autocmds to enable Goyo based on the filetype, so if I work on a markdown or textfile Goyo gets initialized automatically. If I leave the buffer or change the filetype then Goyo gets closed. Below is how I implemented the behaviour:
autocmd FileType * :Goyo!
autocmd FileType markdown :Goyo
autocmd FileType text :Goyo
That seems to work fine. The question is, whether or not this is the way to go or if there is a better approach to solve the problem?
That's just fine and how I would implemented it, too. As you only hook into the FileType event, the toggling is only triggered when you :edit a new file, not when you recall an existing buffer with another filetype. You could do that with BufWinEnter, but it may cause too many inadvertent togglings. I guess the plugin comes with a quick toggle mapping to manually do this, anyway.
Alternative
An alternative to the autocmd FileType commands is filetype plugins (i.e. ~/.vim/ftplugin/markdown.vim etc.), which have the benefit of separating things neatly. But as you need a catch-all autocmd to turn off Goyo, and the list of filetypes is small, I would also prefer keeping things together, just like you did.
Improvements
Note that your set of commands would add a duplicate set of autocmds if you re-:source your ~/.vimrc (or whichever script you've put them in). To avoid that, you could wrap them in
augroup AutomaticGoyo
autocmd!
...
augroup END
After I installed Vundle, my vim no longer obeys the expandtab settings I had. My tabs were set to 2 spaces, but now in python files it no longer does that. The problem is being called by this line:
filetype plugin on
What does this line do (It is required by vundle)? Also, what can I do to make sure my settings are obeyed?
Thanks!
VIMRC: pastebin.com/tGmfCi78
The problem is that your settings are being overridden by a filetype plugin that's part of Vim. The issue is in ftplugin/python.vim:
" As suggested by PEP8.
setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8
The python plugin attempts to setup your source code to be PEP8 compliant by default, so it's adjusting the tabstop. You'll want some of what these plugins have to offer, but you may need to setup your own autocommands to fixup anything you don't like.
There are two ways to go about doing this. If you have a ~/.vim folder, the easiest way is to add the file ~/.vim/after/ftplugin/python.vim:
" Here, you can set the setting directly, or call a command or function
" to help you. We'll call a command, and then implement that command in
" your top-level vimrc to help keep things in one place.
SetupPython
In your .vimrc, add:
function! SetupPython()
" Here, you can have the final say on what is set. So
" fixup any settings you don't like.
setlocal softtabstop=2
setlocal tabstop=2
setlocal shiftwidth=2
endfunction
command! -bar SetupPython call SetupPython()
The latter bit just allows you to call the function as SetupPython rather than call SetupPython() in the after file.
The other way, is to keep everything in your .vimrc, but you use the VimEnter autocommand to setup a FileType autocommand for python to set your preferences. By waiting until VimEnter is triggered, all the other plugins will have had time to setup their autocommands, so your's will be added to the end of the list. This allows you to run after the python plugin's FileType autocommand and set your own settings. This is a bit of a mess though, and the after/ mechanism above is the preferred way of doing this.
FWIW, many common settings I keep in a SetupSource() function to be called from a number of different FileTypes. Then I'd have SetupPython() call SetupSource(). This helps to keep the specific functions a little cleaner and reduce some duplication. If it helps, take a look at the functions in my vimfiles here: https://github.com/jszakmeister/vimfiles/blob/master/vimrc#L5328
Overridden settings
It could be that the settings are being overridden by language-specific settings. See http://vim.wikia.com/wiki/Keep_your_vimrc_file_clean for more information:
The quick way to get started is to move all the language-specific stuff from your .vimrc file into a file named .vim/ftplugin/language.vim (or $HOME/vimfiles/ftplugin/language.vim on Windows).
Check in those locations for a python specific .vim file.
Filetype on
Vundle appears to require filetype off, and I'm not sure if you should turn it back on. There's a thread on the github issues page for Vundle explaining why filetype on is required. Perhaps this will provide some insight.
I also think having filetype plugin indent on followed by filetype on is redundent. According to the vim help docs, the former turns detection, plugin and indent on, and the latter turn detection on and leaves the plugin and indent unchanged:
Overview: *:filetype-overview*
command detection plugin indent
:filetype on on unchanged unchanged
:filetype off off unchanged unchanged
:filetype plugin on on on unchanged
:filetype plugin off unchanged off unchanged
:filetype indent on on unchanged on
:filetype indent off unchanged unchanged off
:filetype plugin indent on on on on
:filetype plugin indent off unchanged off off
I am new to vim and I want to use it for scripting Python3. anyone knows how to customize vim for editing Python 3 scripts? (mainly for indentations, coloring and tab suggestions... )
Thanks
Python wiki suggests putting the following in your ~/.vimrc:
syntax on
filetype indent plugin on
set tabstop=8
set expandtab
set shiftwidth=4
set softtabstop=4
Edit: the above does have the side-effect of using the tab settings in all files edited with vim. Other approaches are discussed in the wiki as well.
There's also the python.vim script for syntax highlighting and such.
You should put customized settings in to ~/.vim/ftplugin/python.vim This will be sourced when vim sees a file with a filetype python.
To make sure the settings only affect the current buffer use setlocal.
To make sure that mappings only affect the current buffer use noremap <buffer>
Just make sure to have filetype plugin indent on in ~/.vimrc
What is the difference between placing:
autocmd FileType ruby setlocal ts=2
in my ~/.vimrc and placing:
setlocal ts=2
in ~/.vim/ftplugin/ruby.vim?
If there is no difference, where should I place commands that are specific to one filetype?
As far as I know, there isn't really a difference between the two.
I prefer to put commands like that in ftplugin and keep my main .vimrc language-agnostic… But there are no hard-and-fast rules. An advantage to keeping it in your vimrc would be that your vim settings would be easier to move around (ex, you could get all your vim settings on a new machine by simply copying your .vimrc, instead of .vimrc + .vim). Obviously putting them in your .vimrc will also have a minor performance penalty (ie, they will be loaded + executed for every file, not just ruby files)… But I wouldn't really worry about that.