What is the most elegant way to deal with sourced files that themselves source (relative) source files in VIM? - 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...

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.

How to prevent syntastic from creating a directory for every vim instance?

When using the syntastic plugin with vim, I see a new /tmp/vXXXXXXX directory every time I open a new vim instance. When the syntastic plugin gets disabled, no such directories are created.
When I ran inotify, I found that a numeric file is created in that directory every time I save a file. Is it possible to make syntastic (or vim) create a temporary directory on demand? Failing on that, can I make it use a single directory instead? For example, /tmp/vim-syntastic/vXXXXXXX/?
According to the developer, syntastic does not create temporary directories by itself, that is handled by vim. Looking a bit further, I found that vim uses $TMPDIR to set a temporary directory. If the directory is unwritable, then it gets ignored.
So, as a solution, the following lines set the temporary directory to /tmp/vim-USERNAME, and then create it (ignoring errors that normally occur when the directory exists):
" Keep all vim-related temp files in a single directory
let $TMPDIR = '/tmp/vim-' . $USER
silent! call mkdir($TMPDIR, '', 0700)
Now, I do not have a lot of /tmp/vXXXXXX/ directories anymore. Instead, they appear in /tmp/vim-peter/vXXXXXX/ which is great.
If you look in the syntastic helpfiles, you'll see that syntastic uses a 'tail' file for storing the output of a given make program. You can override the default tail for a given filetype and subchecker by adding the following to your vimrc:
let g:syntastic_<filetype>_<subchecker>_tail = "> /tmp/vim-syntastic/your-file-here"
So for example if you wanted mri to output to /tmp/vim-syntastic/ruby-mri, you would write:
let g:syntastic_ruby_mri_tail = "> /tmp/vim-syntastic/ruby-mri"
See :help syntastic-config-makeprg for more info. Here's a direct link on git. As far as I know there's no built-in way to set the default directory for all syntastic output, unfortunately.
Edit: Lekenstein found another solution, which he posted in the linked Github issue.
let $TMPDIR = '/tmp/vim-' . $USER
silent! call mkdir($TMPDIR, '', 0700)
This will make a special directory for all vim-related temporary files. That means it will also affect temporary files not related to syntastic.

Vim use a variable interpolated within a String?

I have a .zshrc and .vimrc file
In my .zshrc file I have syncfolder="$HOME/Google Drive/Dropbox"
I now want to reference syncfolder within my .vimrc file, is this possible (if so, how)?
Also, within my .vimrc file I'm sourcing lots of other files and I want to make that more efficient.
The following doesn't work:
let vimfolder="~/Google Drive/Dropbox/Fresh Install/Shell/vim"
source "$vimfolder/settings.vim"
source $vimfolder/vundle.vim
source $vimfolder/mapping.vim
source $vimfolder/filetypes.vim
source $vimfolder/commands.vim
...I've tried multiple variants where vimfolder included $HOME and in another I've tried exporting a variable from my .zshrc file, like so...
export SYNCFOLDER=$syncfolder
...in the hope that I could access it from within my .vimrc but it didn't work.
Any ideas?
You need to use :execute to evaluate a variable in most Ex commands. Vim variables do not have a $ prefix, those are environment variables (which you can reference from Vimscript, too):
execute 'source' vimfolder . '/settings.vim'
However, in this particular case, I'd rather add the location to 'runtimepath' and then use :runtime:
set runtimepath+=~/Google\ Drive/Dropbox/Fresh\ Install/Shell/vim
runtime settings.vim
Another alternative is symbolic links, so you don't have change anything in your Vim setup.

How to get ctags working inside vim

I'm new to vim and wanted to get ctags integration working so I can more easily navigate a large java project.
I've pulled down the zip from source forge and extracted it but from here I'm not sure how to get it working with vim
Any help for a novice vim user would be great!
As nobody has given one critical function in these answers, I'll provide one more slightly superior answer.
The easiest way to use ctags with vim is by calling:
ctags -R *
from the root of your source repository. This will generate a tags file in that same directory.
In your ~/.vimrc file, add this short block:
" ctags optimization
set autochdir
set tags=tags;
" denotes a comment. set autochdir tells vim that if it doesn't find a tags file in the $PWD it will look in the directory parent for the tags file, recursively. set tags=tags; tells vim that the name of your tags file will always be the same as the default tags file generated by ctags.
So long as you run ctags -R * in your root source directory the first time and occasionally to update it (if you pull new changes from others) then you'll always have a fast and intuitive ctags symbol lookup in vim.
Using exuberant ctags, I use something like this in my project's base directory (excluding the "log" directory):
ctags -R --exclude=log *
You have to run the ctags command with the source files as arguments. This will create a tags file containing all information. Then you can open a file with vim, and e.g. press Ctrl-] when on a line with a function to jump to the code of that function. If vi isn't started in the same directory as the tag file, you can set it with :set tags=<file>
This is what I'm doing:
ctags -n -f [OUTPUT] [SOURCE] to generate the tags (NOTE: the -n applies to me but may not be necessary for your usage)
exec "set tags=" . [OUTPUT] inside of .vimrc to let vim become of aware of the tags
EDIT: I'm using
Exuberant Ctags 5.5.2
VIM 6.1
Additional info:
See ctags usages here
Tips and tricks from SO
look at this article: vim-easytags. i haven't tried this, but it looks quite good. manually creating and updating tags was really annoying. hope this will help. :)

Resources