I'm getting pretty annoyed of vim automatically generating swap files when I press control-z. Is there any way to disable this feature (and not just delete them)?
Put this in your .vimrc:
set noswapfile
Edit: You could also consider moving backup files to a location where they won't get in your way. I have this in my .vimrc:
set noswapfile
" Alternatively, store your swap files in your local .vim folder
" call system('mkdir ~/.vim/swap')
" set dir=~/.vim/swap/
if has('persistent_undo')
set undolevels=5000
call system('mkdir ~/.vim/undo')
set undodir=~/.vim/undo
set undofile
endif
call system('mkdir ~/.vim/backups')
set backupdir=~/.vim/backups/
" The 'n' here is a prefix specifying which viminfo property is being set -
" in this case, the Name of the viminfo file.
" :h 'viminfo'
set viminfo+=n~/.vim/viminfo
I really like the persistent undo, meaning even after quitting vim, restarting the computer etc., you can just undo/redo changes from previous sessions.
Note that if you are using both console vim under Cygwin and windows gvim, you'll need to wrap all of this in an if v:progname != "gvim.exe" block, since windows won't understand paths like ~/.vim. I have this section duplicated in my _gvim file using windows paths, for the odd occasion where I use gvim.
Related
I am new to using Vim, and have been using "The Ultimate Vim configuration" on GitHub. It uses 4 spaces wide tabs as default, so to change that, I changed my .vimrc file to the following:
set runtimepath+=~/.vim_runtime
source ~/.vim_runtime/vimrcs/basic.vim
source ~/.vim_runtime/vimrcs/filetypes.vim
source ~/.vim_runtime/vimrcs/plugins_config.vim
source ~/.vim_runtime/vimrcs/extended.vim
" Own settings
set number
set tabstop=2
set softtabstop=2
set shiftwidth=2
set expandtab
set smarttab
try
source ~/.vim_runtime/my_configs.vim
catch
endtry
The my_configs.vim file is deleted, so I know that the indentation settings are not being overwritten there.
The problem is, if i source the vimrc file from vim with :source ~/.vimrc The indentation works perfectly, but if I close Vim and open again, then the indentation is back to 4 spaces wide instead of 2 as specified in the vimrc... All other settings from the Ultimate Vim Configuration get loaded from vimrc but not the indentation settings, which is really frustrating, as I need to source the vimrc file every time i want to use vim.
Any help is greatly appreciated, I really would like to exclusively use Vim but if this cannot be solved then I need to keep using Visual Studio Code :(
Thank you in advance.
EDIT: Here is the output of running command :verbose set tabstop? softtabstop? shiftwidth?:
tabstop=2, Last set from ~/.vimrc line 10
softtabstop=2, Last set from ~/.vimrc line 11
shiftwidth=2, Last set from ~/.vimrc line 12
When you're editing a Python file in Vim, it will auto-detect the file type and apply Python specific settings. Amongst those, it will set the soft tab stop recommended by PEP 8.
You can find these settings in ftplugin/python.vim in your Vim runtime:
if !exists("g:python_recommended_style") || g:python_recommended_style != 0
" As suggested by PEP8.
setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8
endif
You can override those settings with a global variable, which you can set in your vimrc. To disable the Python tab stop automatically set by Vim, include the following somewhere in your vimrc:
let g:python_recommended_style = 0
Note that those settings are there for a reason, they're pretty standard for Python and it's highly unusual to see Python code indented by a different number of spaces... But you have the knob to override it if you really want to.
I want to create different vim files for different task in vim. I know you you can create different vim files, which can be loaded on the fly based on the extension of the file. My problem is I am using vundle to maintain plugins and I really don't know how to separate these plugins in different files.
I searched about separating vim and I found you can use ftplugin, something like ftplugin/python.vim or ftplugin/matlab.vim. But I don't know should I write vundle part in each .vim file or everything should be in one vim file.
Please let me know if you need more information. Below is my current .vimrc file.
" Configuration file for vim
set modelines=0 " CVE-2007-2438
" Normally we use vim-extensions. If you want true vi-compatibility
" remove change the following statements
set nocompatible " Use Vim defaults instead of 100% vi compatibility
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')
"===================================================================
"Plugins
" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'
" For autocomplete
Bundle 'Valloric/YouCompleteMe'
" For folding
Plugin 'tmhedberg/SimpylFold'
" For indent python
Plugin 'vim-scripts/indentpython.vim'
" For syntax
Plugin 'w0rp/ale'
" Check Python files with flake8 and pylint.
let b:ale_linters = ['flake8', 'pylint']
" Fix Python files with autopep8 and yapf.
let b:ale_fixers = ['autopep8', 'yapf']
" Disable warnings about trailing whitespace for Python files.
let b:ale_warn_about_trailing_whitespace = 0
syntax on
" For color Schemes
"Plugin 'jnurmine/Zenburn'
Plugin 'flazz/vim-colorschemes'
Plugin 'morhetz/gruvbox'
" For PowerLine
"Plugin 'powerline/powerline', {'rtp': 'powerline/bindings/vim/'}
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'
"For the nerd tree
Plugin 'scrooloose/nerdtree'
" add all your plugins here (note older versions of Vundle
" used Bundle instead of Plugin)
" ...
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
"===================================================================
" For UTF-8
set encoding=utf-8
"System Clipboard
if has('mac')
set clipboard=unnamed
elseif has('unix')
set clipboard=unnamedplus
endif
"set Line Numbering
set nu
"to handle the backspace problem
set bs=2
"Set up mouse
set mouse=a
"For Highlighting searched text
set hlsearch
"For confirming before exit (save)
set confirm
"Maping Ctrl+A for select all
map <C-a> <esc>ggVG<CR>
"===================================================================
" Mapping NERDtree toggling
nmap <F6> :NERDTreeToggle<CR>
"===================================================================
"Few settings for plugins
" colorscheme
colorscheme py-darcula
" to see the docstrings for folded code
let g:SimpylFold_docstring_preview=1
let mapleader=" "
"The first line ensures that the auto-complete window goes away when you’re
"done with it, and the second defines a shortcut for goto definition (second
"one I need to learn)
let g:ycm_autoclose_preview_window_after_completion=1
let g:ycm_min_num_of_chars_for_completion = 1
"map <leader>g :YcmCompleter GoToDefinition<CR>
"To handle vitural env for YCM
let g:ycm_python_binary_path = 'python3'
Using separate configuration files is possible (you can override the default .vimrc via the -u {vimrc} command-line argument; a shell alias can make this very easy). However, this complexity should not be necessary for most users.
In particular, Vim has built-in scoping of settings for different types of files in the form of buffer-local options and filetype plugins. So, if you want different indentation for Python vs. text files, that should work out of the box. Even though you're globally installing a plugin like vim-scripts/indentpython.vim (via Vundle in your case), it will only activate on files where :setlocal filetype? returns python. (By the way, that plugin was last updated 9 years ago, and Vim now ships with a $VIMRUNTIME/indent/python.vim that's actively maintained.)
For global plugins, it usually doesn't harm to have them installed all the time, even though you might only use them with certain projects. Vim's autoload mechanism dynamically includes the main plugin functionality on demand to keep Vim startup time short. Activating different plugin sets only makes sense for very few advanced users (and these would already have very deep knowledge of Vim's plugin mechanisms and not need to ask such questions).
You'll find more information about this at :help filetypes and :help local-options.
As I understand, the existed plugins work fine but you want to know the proper way to write your own ones. Am I right?
If so, then you don’t have to write your plugins (or other vim files) in any relation to Vundle or any other plugin manager. All you need is:
Read :help rtp.
Create a folder for your files wherever your want.
Set up the runtimepath option to the new folder. Typically, it’s just adding the new path with += operator.
Knowing about the list of directories which are searched for runtime files, create the functionality you want to get.
Yet another time: leave Vundle if you’re writing your own vim file. Vundle,
like any other plugin manager, is just a convenient tool to retrieve the code from a github repository and to set up the rtp option according to the new data.
Here is my vimfiles.
I write
se tabstop=4
in my vimrc. But sometimes I read some project source codes, they use TAB to indent and set tab equals 8 space. So I must
:set tabstop=8
to make the code indent right.
If I can make sure the path of source code (eg. /home/foo/erlang/),
is there a method to make my life better?
I mean to write some code in vimrc, like
if file in path "home/foo/erlang"
then set tabstop=8
end
or some other vim settings.
Thanks!
If you want to add that stuff to your own .vimrc file then one thing you can do is just autocmd file opens to call a function to set things up like you'd like... The docs talk about doing this, see :help autocmd-patterns
autocmd BufRead */home/foo/erlang/* set tabstop=8
I actually have a few autocmds which do "big" setup on filetypes depending on their directory and other attributes. One such might look like this:
autocmd BufRead *.txt call SetUpTextFileBuffer(expand("<afile>:p"))
Then that function does path matching and so forth to configure a whole range of options (autoformatting, text width, etc.). Modelines are certainly better if you can insert them but sometimes you're working with others' code and you don't want to leave a huge footprint.
You can create a .vimrc file in the directory where you want the settings to apply. It would contain your tabstop and other Erlang-specific settings.
% cd ~/erlang
% ls
... .vimrc
% vim somefile.erl
Vim will automatically see and use the .vimrc in $PWD. (For this to work, you should have set exrc in your ~/.vimrc). See :help 'exrc' for details.
If you don’t want to cd and open files from that directory, you can always invoke Vim with an explicit startup file:
vim -u /path/to/erlang.vimrc somefile.erl
You can use a modeline in your file.
(You need to have set modeline in your .vimrc for this to work.)
In the first or last lines of your file, put the following comment;
% vim: tabstop=8:
By default, the first and last 5 lines are scanned for modelines.
Whenever I open txt files (and some others) I get my textwidth set to 80. I think this is coming from syntax or ftplugin. I'd like to fix this in my _vimrc so I don't have to call "set tw=0" every time I open a file that has this setting.
My guess is that you are getting the defaults, not a setting from an ftplugin. Check
:verbose set tw? ft?
to confirm. See the examples under
:help autocmd-patterns
for one way to set your own defaults for *.txt files.
On second thought, the default for 'tw' is zero, so you are not getting defaults. Perhaps some ftplugin used :set instead of :setlocal and you are getting the global value of the option. I think the rest of what I wrote is still on target.
The problem was in vimrc_example.vim the line
autocmd FileType text setlocal textwidth=78
sets the textwidth on txt files. Also my formatoptions get reset and no longer have l or lv (verbose doesn't give any detail who did it)
/etc/vimrc can also contain a set tw command - it certainly did in my case, thereby forcing all my files to a text width of 78 until I changed it manually.
I am using Vim 7.3.154 cli on Linux Mint 12 "Lisa"(x64).
I prefer using a tabstop and shiftwidth of 2 columns.
My .vimrc has
:set ts=2
:set sw=2
to accomplish the same.
Whenever I open a new instance of Vim, tabstop value is retained, so existing tabs are rendered according to .vimrc. But somehow the shiftwidth value gets changed to 3. This results in 3 column indention while auto-indenting.
Running :set sw=2 on the new instance of Vim fixes this issue.
Can anyone tell me why Vim is ignoring/changing shiftwidth value from .vimrc?
I tried disabling all plugins to no avail.
Vim compiled options | .vimrc
This answer just summarizes what we discussed in the comments. :)
This is most likely caused by the fact that you have file specific settings enabled. Check :h modeline for more info on that. Make sure that those files you have the problems with don't have this line in them.
Instead of only setting tabstop and shiftwidth you should also setup a couple more settings regarding whitespace and tabs.
set noexpandtab " Make sure that every file uses real tabs, not spaces
set shiftround " Round indent to multiple of 'shiftwidth'
set smartindent " Do smart indenting when starting a new line
set autoindent " Copy indent from current line, over to the new line
" Set the tab width
let s:tabwidth=4
exec 'set tabstop=' .s:tabwidth
exec 'set shiftwidth=' .s:tabwidth
exec 'set softtabstop='.s:tabwidth
Check this video for some extra info: http://vimcasts.org/episodes/tabs-and-spaces/
So this is the answer to the actual problem that you were able to solve. That php.php file was in the /plugin directory. That directory gets loaded once, each and every time Vim starts up. Check this: http://learnvimscriptthehardway.stevelosh.com/chapters/42.html#vimplugin
If what you want is that file to load only on PHP files then you should put it in the /ftplugin folder: http://learnvimscriptthehardway.stevelosh.com/chapters/42.html#vimftplugin
Read the documentation there, it should be a .vim file, in other words in this case it would be called php.vim.
What Pathogen or Vundle do is modify the runtimepath (:h runtimepath), nothing more, nothing less.
So you can now accept this answer, by clicking the little green arrow to the left of this answer. :)
You can run:
verbose set shiftwidth ?
to check what is setting the value of shiftwidth. It might coming from a plugin. You can also choose to modify the value there.