Where do I place .vim files? vim81 or vimfiles folders? - vim

I am trying to replace dockerfile.vim syntax, and when I do search on my machine I find these already installed:
/usr/share/vim/vimfiles/ftdetect/dockerfile.vim
/usr/share/vim/vimfiles/syntax/dockerfile.vim
/usr/share/vim/vim81/ftplugin/dockerfile.vim
/usr/share/vim/vim81/syntax/dockerfile.vim
Should I delete the .vim from both vimfiles, vim81 and replace with the new file?

When Vim sources ftplugin scripts and such it uses :h :runtime command which does search along :h 'runtimepath'. Normally all these scripts also have a guard against double inclusions (like if exists("b:current_syntax") etc.), so the first one gets an upper hand.
Hence, you only must ensure that you put your scripts in the first runtimepath directory, i.e. under ~/.vim/... etc. There's no need to delete existing files from /usr/share/vim/...

Related

How do I go to a file in vim/nerdtree?

I'm trying to transition to vim, but I'm having a hard time mapping over some functionalities in pycharm over to vim.
The first being how do I directly go to a filepath. In pycharm, I believe it is cmd-shift P. You'll type the file-path and it'll take you there. I think there's auto-complete too?
Like -- I know that there's a .css file I want to access. So I'd instinctively start typing: cmd shift p .css and this would return the .css files.
How do I do that in vim?
Thanks!
:edit is the most basic command for editing an existing file.
:edit <your file name>
To get a list of all the files ending in ".css" use :edit e *.css and then press Ctrl+d. See :help c_CTRL-D in Vim for more information.
:find <file> is a more powerful version of :edit. It searches for <file> from the directories listed in your path option. For example, if your current directory is project and the value of the path contains
/path/to/project/**, then :find file.css will search all the subdirectories of project for the "file.css".
There is also a plugin called "ctrlp.vim" that should be similar to what you used in pycharm.
For more information about file navigation, I highly recommend reading "Death by a thousand files", an excellent article by Romain Lafourcade.

How to get to long directory quickly when writting code in VIM

I am writing Bash script using VIM. I need to cd to a directory and run the command tool. The command tool is deep inside the directory. How do I quickly cd to that directory instead of manually typing the directory out in VIM ? In terminal prompt, I can get to the directory quickly using tab. It does not work in VIM.
Thanks
ffl3883
You can change to the currently edited file's directory with :cd %:h; see :help filename-modifiers. Likewise, if you trigger the tool from Vim :! % can do this quickly (and repeat with :!!). Or just :set autochdir, so that the current directory within Vim always follows the currently edited file (and you can then just reference the file via ./).
When typing file paths in vim (as I often do for shell scripts), I find filename-complete invaluable. Simply type <C-X><C-F> in insert mode.
N.B. It does not work in all cases (generally vim prefers the path to be a separate WORD), but a quick edit-complete-fixup isn’t terrible.

gvim: change the default working directory

when I open gvim using Alt+F2 it takes as its default working directory my home folder.
How can I change the working folder after or while running gvim? can i pass that folder as a parameter when open gvim?
You could use a shortcut.
The simplest way, though, would be to
:edit $MYVIMRC
append a line
cd /home/user/my/work/dir
save (optionally execute :w|source % to immediately reload)
Inside vim
use
:pwd
:cd some/other/dir
To view/change current working directory.
Use e.g.
:cd %:h
to change to the directory containing the file loaded in the active window.
If you need/want to do this often, consider just setting 'autochdir'
:se autochdir
From the docs:
When on, Vim will change the current working directory
whenever you open a file, switch buffers, delete a
buffer or open/close a window. It will change to the
directory containing the file which was opened or
selected. This option is provided for backward
compatibility with the Vim released with Sun ONE
Studio 4 Enterprise Edition.
Note: When this option is on some plugins may not work.
You can pass an a folder to gvim (when you have NERDTree then it will be a file tree) You can cd before start to begin in directory you want or use :cd <path> command to change current working directory, which can be passed to -c flag when running Vim:
$ [g]vim -c 'cd <path>'
You can also check current dir using :pwd command.
You can change the working directory with the :cd command. You can also pass this in a command-line option like this:
vim -c "cd wherever"
If you like the working directory to always be the file you're currently editing you can use the set autochdir option. Put that in your ~/.vimrc or see :help autochdir.
I know I'm late, but I started using CDargs which is a bash tool to mark certain directories as bookmarks, then use cdb and press tab to list all the bookmarked directories.
There is a vim plugin that interacts with the settingsfile of this tool: vim-cdargs.
This combo works really nice for me to switch between projects.
Or after opening gvim to go quickly to some bookmarked folder, then use Ctrl-p plugin to quickly find the file I want to edit.
extra hint: I don't even want to type :Cdb so I abbreviated c to expand to :Cdb by adding this to my vimrc:
cnoreabbrev c Cdb
after which typing :c followed by a space, will expand into :Cdb.
EDIT: I now use vim-startify which provides a start page for vim that shows the most recent used files. And with the option let g:startify_change_to_vcs_root = 1 it will change the working directory to the outermost vcs root folder of the file you opened. Which is almost always what I want.
Furthemore, I created my own 'plugin' with some key mappings that will switch to the closest or furthest directory, in the path of the current buffer, containing a .git directory or file. In order to easily switch between searching for files in the current git submodule or in the overal supermodule.
Also I switched to fzf with fzf-vim instead of Ctrl-p, which works significantly faster and is more highly configurable.

Searching with command-T

When I search some file with command-T it often failes to find it because I'm not in the right directory, so I have to change the directory.
Is it possible to set that command-T will search first in the directories that are bookmarked in Nerdtree or somewhere else?
I could change the directory to / but this search very large scope of files. When I change the dir to my home directory and I'm looking for something ordinary like .bashrc I will find rather many files that are located under .wine directory.
In 99 % of time I need to search files in project directories that I actively work with. Can I set these directories in some preferences?
According to the documentation you can exclude directories from your search:
*command-t-wildignore*
|'wildignore'| string (default: '')
Vim's |'wildignore'| setting is used to determine which files should be
excluded from listings. This is a comma-separated list of glob patterns.
It defaults to the empty string, but common settings include "*.o,*.obj"
(to exclude object files) or ".git,.svn" (to exclude SCM metadata
directories). For example:
:set wildignore+=*.o,*.obj,.git
A pattern such as "vendor/rails/**" would exclude all files and
subdirectories inside the "vendor/rails" directory (relative to
directory Command-T starts in).
So if you wanted to exclude a backup dir, you would write:
set wildignore+=project/backup
in your .vimrc
In addition, to ignore dotfiles/dotdirs you can look into these options:
g:CommandTNeverShowDotFiles
g:CommandTScanDotDirectories
g:CommandTMaxDepth
These allow you to:
- ignore dotfiles completely;
- stop searching recursively in dotdirs;
- specify at what depth should Command-T stop scanning.
I found this information in the author's git, but you can probably see this document by issuing in vim:
:help Command-T
(or a similar name)
I did not see any reference to preferences or bookmarks in the plugin.
However if you start vim by opening a file in said project directory you might want to add this line to your .vimrc:
set autochdir
This option will set on startup your directory to the current file's directory.
You could try Ctrl-P. I had the same problems as you do and making the change solved them.
I also ignore some folders (in .vimrc):
let g:ctrlp_custom_ignore = {
\ 'dir': '\.git$\|\.hg$\|\.svn$',
\ 'file': '\.exe$\|\.so$\|\.dll$' }

What is the most elegant way to deal with sourced files that themselves source (relative) source files in VIM?

I am editing a file like
/path/to/file.txt with vim, hence the current directory is
/path/to.
Now, I have a directory
/other/path/to/vim/files
that contains sourceA.vim. Also, there is a sourceB.vim file in
/other/path/to/vim/files/lib/sourceB.vim
In sourceA.vim, I want to source sourceB.vim, so I put a
so lib/sourceB.vim
into it.
Now, in my file.txt, I do a
:so /other/path/to/vim/files/sourceA.vim
which fails, because the sourcing system is obviously not prepared for relative path names along with sourcing from another directory.
In order to fix this, I put a
execute "so " . expand("<sfile>:p:h") . "/lib/sourceB.vim"
into sourceA.vim which does what I want.
However, I find the solution a bit clumsy and was wondering if there is a more elegant solution to it.
I cannot put the sourceA.vim nor sourceB.vim into vim's plugin folder.
Maybe you could modify your runtimepath in your vimrc or elsewhere:
set runtimepath+=/other/path/to/vim/files
Then use :runtime instead of :source in your sourceA.vim file:
runtime lib/sourceB.vim
You can then use the same ":so /../../../sourceA.vim" command as before...

Resources