I'm looking for a way to configure my vimrc file or set variables that can search external file for auto completion in Vim
For example:
I want the words completion in following directory:
/home/code/mycompletionfile.txt
By the way:
I do know I can use dictionary to include a file, but I'm looking for a way to use directory so that all the files under the directory can be searched for auto completion.
I knew there are many awesome plugins out there and I did install some plugins before, but some plugins slows down my Vim significantly.
This is what I did so far:
I try set path variable in vim:
Add my path that contains all my files so that Vim can search all the file in the path for auto completion
Try:
:set path+=/home/code/
Try:
:set path+=/home/code/*
Try:
:set path+=/home/code/.
None of them are working.
I do know complete variable has many options in Vim:
complete: .,w,b,u,t,i
The 'path' option is completely irrelevant.
Here is the relevant part of :help 'complete':
k{dict} scan the file {dict}. Several "k" flags can be given,
patterns are valid too. For example:
:set cpt=k/usr/dict/*,k~/spanish
Related
I'm using VIM as my primary code editor for Laravel projects. While I'm in VIM, I want to search for a file that I can open up as a tabnew or as a new vsplit pane. I was told there's a find command. So I tried something like this:
:find ~/development.project1.com/ -name *Controller.php
But that only gave me the error E345: Can't find file "/var/www/development.project1.com/ -name *Controller.php" in path
What did I do wrong? How can I quickly search for other files in VIM and open them up as a tabnew or as a new vsplit pane?
The vim find command is not the same as the unix find command. To find out what find does, use the online help!
:h find
This will give you an answer:
:fin[d][!] [++opt] [+cmd] {file}
Find {file} in 'path' and then :edit it.
In other words, :find is like :edit but looks in your path instead of just the current directory. Note that the vim path is not the same as the operating system shell variable PATH. You can find out what is in your path with
set path?
Most likely you don't have every subdirectory of your project in your path (or in your PATH). Neither should you.
If you want to edit a file with a name ending in Controller.php, a simple solution to search through every subdirectory is to specify ** before the filename to wildcard-match against every subdirectory:
:e **/*Controller.php
Note that doing the above will only open the first file matching the wildcards. If there are several matching files, and that wasn't the file you wanted, no luck.
If you want to choose a file among several matches, and don't want to use plugins, you can read a list using the unix file command
:r! find . -name \*Controller.php
You will end up with a buffer with a list of files. To open one of the files, move the cursor above the file name, and use the gf command to open it.
While not really an answer to your question, with vanilla vim, there's wildmode command line completion. If wildmode is enabled, vim will complete filenames when you open a new file with :e.
Finally, there are lots of different fuzzy finder plugins for vim. If you don't need windows, I recommend fzf.
vim find and find command are different as noted. Perhaps, you might like ctrlp.
But a easier vanilla vim replacement is to go to the folder which contains your files and in vim
:set path+=**
:find file_name
This will find and edit file_name. Nice thing of this is that it can auto-complete the file name but this will not be in split or tab.
So in my .vimrc I have this:
let g:syntastic_python_pylint_post_args="--max-line-length=80"
I want to have something like:
let g:syntastic_python_pylint_post_args="$(cat .line_length.txt)"
And I want this cat command to run whenever I open vim.
For example if I have a file in a project
myproject/.line_length.txt
And the contents of the .line_length.txt is 120 for example and I run
vim
inside that directory, it should read that file and set the contents of the file to the variable.
...
Is this possible somehow?
I have tried using project specific .vimrc files but it does not seem to read
let g:syntastic_python_pylint_post_args="--max-line-length=120"
It will read set ... lines though, but not let g: ... lines.
A literal translation of your attempted $(...) syntax would be this:
let g:syntastic_python_pylint_post_args = substitute(system('cat .line_length.txt'), '\n\+$', '', '')
But there's actually no need to run an external command for this. Vim has the low-level :help readfile() function:
let g:syntastic_python_pylint_post_args = readfile('.line_length.txt')[0]
Caveats
Depending on the location you start Vim in, the configuration may not exist. You need to account for that, probably using a filereadable() conditional around it, or just ignoring any errors with :silent!.
Some plugins only read their configuration variables during startup, and after that ignore any changes to it. This should be fine for your plan to run in ~/.vimrc, but it may affect your ability to "reload" a different project during runtime (see below).
Refresher on local configuration
Your solution depends on Vim being started inside the "project directory". You've also mentioned project-specific vimrc as an alternative. Here are some options for that:
Central configuration
If it's okay to configure the specific commands / local exceptions centrally, you can put such autocmds into your ~/.vimrc:
:autocmd BufRead,BufNewFile /path/to/dir/* setlocal ts=4 sw=4
It is important to use :setlocal instead of :set, and likewise :map <buffer> ... and :command! -buffer ....
On the other hand, if you want the specific configuration stored with the project (and don't want to embed this in all files via modelines), you have the following two options:
Local config with built-in functionality
If you always start Vim from the project root directory, the built-in
:set exrc
enables the reading of a .vimrc file from the current directory. You can place the :set ts=4 sw=4 commands in there.
Local config through plugin
Otherwise, you need the help of a plugin; there are several on vim.org; I can recommend the localrc plugin (especially with my own enhancements), which even allows local filetype-specific configuration.
Note that reading configuration from the file system has security implications; you may want to :set secure.
currently there are a few paths where .vimrc files are being searched. (as can be seen in :scriptnames command).
How do I add another path?
You're mistaken. :scriptnames tells you which scripts were loaded. It will be:
.vimrc,
possibly .gvimrc,
plus all plugins in 'runtimepath',
plus all ftplugins, syntax files and indent files in runtimepath that apply for all the buffers you have edited,
plus the autoload plugins loaded by the previous files,
plus the files you've sourced manually.
If you want to change the places where to search for your .vimrc, it will be more complex as vim has a very specific heuristic to search for a .vimrc. See :h startup.
Any way. If you really want to add a path where the .vimrc file will be searched, it's not possible unless you define an alias to vim that use the -u flags.
If you want to add other paths where to look for plugins, you'll have to set the 'runtimepath' option in your .vimrc. For instance, :set rtp+=~/.vim/addon/foobar will have all plugins named $HOME/.vim/addon/foo/plugin/*.vim and $HOME/.vim/addon/foo/after/plugin/*.vim loaded automatically, plus the ftplugin/syntax file/indent files loaded automatically as well if you enable them, and when you enter a buffer related to them.
The 'runtimepath' option specifies the locations of the Vim configuration subdirectories (i.e. directories containing autoload/, plugin/, syntax/, etc.) The Pathogen plugin made it popular to extend this so that each plugin is installed into a separate such subdirectory, and other plugin managers (like Vundle) do that as well.
Now, there's only one .vimrc (and you can change its location via the -u command-line argument), but nothing prevents you from using :source path/to/another/script.vim to load other Vim scripts during startup.
TL;DR
To execute a separate Vimscript file during startup, just :source it from your ~/.vimrc. If you have a plugin(s) that you want to install in a separate location, use :set runtimepath+=path/to/pluginroot in your ~/.vimrc, or just use Pathogen or another plugin manager.
I'm on a system (linux) that always recognizes cpp files (*.cc) as tcl files. I don't know what file type that is, but I wanted to override it. The correct syntax highlighting is chosen if I manually do :set ft=cpp. However, I'm having troubles setting that automatically and I don't want to use the modeline option. My own .vimrc doesn't interfere (same result if I rename it).
From the vim help (:help ftplugin-override)
*ftplugin-overrule*
If a global filetype plugin does not do exactly what you want, there are three
ways to change this:
1. Add a few settings.
You must create a new filetype plugin in a directory early in
'runtimepath'. For Unix, for example you could use this file:
vim ~/.vim/ftplugin/fortran.vim
You can set those settings and mappings that you would like to add. Note
that the global plugin will be loaded after this, it may overrule the
settings that you do here. If this is the case, you need to use one of the
following two methods.
I have used this option before on another machine and that worked. I've tried
<file> .vim/ftplugin/tcl.vim
set filetype=cpp
"au BufRead,BufNewFile * set filetype=cpp
The first line correctly sets the filetype (:set ft? returns cpp), but syntax highlighting is not the same as if I said :set ft=cpp. It's still the tcl syntax highlighting. The second line does nothing.
2. Make a copy of the plugin and change it.
You must put the copy in a directory early in 'runtimepath'. For Unix, for
example, you could do this:
cp $VIMRUNTIME/ftplugin/fortran.vim ~/.vim/ftplugin/fortran.vim
Then you can edit the copied file to your liking. Since the b:did_ftplugin
variable will be set, the global plugin will not be loaded.
A disadvantage of this method is that when the distributed plugin gets
improved, you will have to copy and modify it again.
There seems to be no file in my $VIMRUNTIME directory /usr/share/vim/vim72/ftplugin/ called tcl.vim.
3. Overrule the settings after loading the global plugin.
You must create a new filetype plugin in a directory from the end of
'runtimepath'. For Unix, for example, you could use this file:
vim ~/.vim/after/ftplugin/fortran.vim
In this file you can change just those settings that you want to change.
Has the same effect as 1. Is there anything else I can try? Thanks a lot in advance.
cpp is the default filetype for *.cc and *.cpp files.
The tcl filetype is only set for *.tcl, *.tk, *.itcl, *.itk and *.jacl.
I see no reason why Vim would default to tcl when loading a *.cc file but you could check if theses files are installed:
/usr/share/vim/vim7x/ftplugin/cpp.vim
/usr/share/vim/vim7x/indent/cpp.vim
/usr/share/vim/vim7x/syntax/cpp.vim
and if the correct checks are done in:
/usr/shhare/vim/vim7x/filetype.vim
Are you the only person working on this machine? Do you use modelines?
I usually have to read .txt files with long lines, and at the same time edit some source file, and I like to see word wrap on the .txt files, and not in the ones that aren't.
Of course I can :set wrap and :set linebreak, but is there any way to make it automatucally, and dependent of the file extension?
There are two options that I can think of. Firstly, you can use an autocmd as suggested by Tassos:
:au BufNewFile,BufRead *.txt set wrap
See:
:help autocmd
An alternative (that is probably more applicable if you've got multiple settings as you have suggested): create a file in the after/ftplugin directory of your vim configuration folder (see below) called txt.vim and it will be sourced whenever you open a .txt file. You can put it in the plain ftplugin directory (rather than after/ftplugin), but it any built-in settings for .txt files will then not be loaded.
Put any commands you want in this file:
" This is txt.vim in the ftplugin directory
set wrap
set linebreak
See:
:help after-directory
:help ftplugin
Vim Configuration Folder
On Windows this would typically be something like:
C:\Documents and Settings\%USERNAME%\vimfiles\after\ftplugin\txt.vim
(I think), or
C:\Program Files\Vim\vimfiles\after\ftplugin\txt.vim
or even:
C:\vim\vimfiles\after\ftplugin\txt.vim
On Linux, it is:
~/.vim/after/ftplugin/txt.vim
For more info, see:
:help runtimepath
I guess :autocmd BufNewFile,BufRead *.txt set wrap should do the trick
you can do lot more with autocommand, refer here: http://www.thegeekstuff.com/2008/12/vi-and-vim-autocommand-3-steps-to-add-custom-header-to-your-file/