Start vim without vimrc / change vimrc with open files - vim

I have 2 .vimrc configuration files ~/.vimrc and ~/.vimsqlrc.
Is there a way I can source either of them (switch from one to another) while I have some files already opened?
As an extension, how do I turn off the loading of vimrc (i.e, don't use any vimrc) while I have files open?

Your ~/.vimrc is read and executed only once. If you want to nullify it with another file, you'll have to change the value of every single option and unmap every single mapping in, of course, both files. This sounds like a very bad and unnecessarily complex idea.
If you want another environment, just use another environment:
$ vim <-- starts Vim normally, reading ~/.vimrc
$ vim -u ~/.vimsqlrc <-- starts Vim using your alternative vimrc
$ vim -u NONE <-- starts Vim without any vimrc
$ vim -u NORC <-- starts Vim without any vimrc, but with plugins
but I'm afraid you'll have to stop and restart Vim for that.
Anyway, your question has a very strong XY problem smell. Do you want to have specific settings for *.sql files?
If that's your goal, you can put your settings in ~/.vim/after/ftplugin/sql.vim like this:
setlocal autoindent
nnoremap <buffer> <F6> :echo "F6"<CR>
Using setlocal for options and <buffer> for mappings ensures that your settings are only applied for *.sql files.

Related

vim filetype settings lost when resourcing vimrc

Following the instructions here I have an ftdetect file, ~/.vim/ftdetect/cheat.vim with this line:
au BufNewFile,BufRead *.cheat/* set filetype=cheat
This loads a simple config file at ~/.vim/ftplugin/cheat.vim:
set statusline=%t
set statusline+=\ %P
set statusline+=%#todo#
set nonumber
It loads fine, but when I source ~/.vimrc the settings for cheat.vim are lost.
The best long-term solution is to avoid having your vimrc overwrite filetype settings if executed directly by using local options and similar, but the simplest fix is often to re-edit the file. Type
:edit
And hit Enter.
This can be shortened to just :e in interactive use, and a mapping is easily created:
nnoremap <silent> <leader>e :edit<CR>
I suggest reading the help pages on vim’s startup, init files, source command, edit command, and the various ways to tune things local to a single buffer (e.g., setlocal, map-<buffer>, autocmd pattern <buffer>).

Mapping Keys with directory type in VIM

I wonder if I can quickly edit some specific files based on project structures. For example if the folder identified as Laravel project this keymap will activated:
" Laravel framework
if(artisan_exist)
nmap <leader>lr :e routes/web.php<cr>
nmap <leader>lca :e config/app.php<cr>
nmap <leader>lc :e composer.json<cr>
nmap <leader>len :e .env<cr>
endif
if(webpack_exist)
nmap <leader>js :e webpack.mix.js<cr>
#endif
so what is the correct function for artisan_exist or webpack_exist so I don't need to use ctrlp or nerdtree because I think isn't necessary to tap many keys only to open those common files.
If you start Vim in a certain (project) directory, you can test relative file paths. For example, to check whether the file webpack.mix.js exists in the project root, use
if filereadable('webpack.mix.js')
nmap ...
endif
There's also isdirectory(), and glob() if you need to search entire subdirectories.
If this is for certain files, opened from any running Vim instance, and the mappings should be local to the file (and not only dependent on the filetype, for which Vim has a built-in detection and handling), you need a more fine-granular approach, usually called a local vimrc:
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.

force vim to overwrite external changes

I use Vim 7.4 (Mac OS) to edit and run Lua scripts. I've mapped a key in my .vimrc to save the current buffer and run an external script.
The key map in .vimrc:
map V :w!<CR> :!python "$HOME/tools/client/concli.py" --lua %<CR>
It works fine but every once in a while the files are 'touched' by Xcode (touch shell command). Then when I hit the mapped key vim warns me that the file has been changed externally and I have to confirm to write to it.
This is quite annoying since the files are often touched. How could I force vim to overwrite external changes without prompting? I tried 'w!' without success.
Thank you, Laurent
Indeed, the overwrite confirmation cannot be turned off with :w!, and :set autoread doesn't help in this case, neither. What does work is instructing Vim to explicitly check for changes before the write:
:checktime | w
I believe
set autoread
should do it. It tells Vim to automatically re-reads the file changed outside Vim.
I saw this in a mailing list. Apparently it is called if the file has changed timestamp, after a call to an external shell command.
function! ProcessFileChangedShell()
if v:fcs_reason == 'mode' || v:fcs_reason == 'time'
let v:fcs_choice = ''
else
let v:fcs_choice = 'ask'
endif
endfunction
autocmd FileChangedShell call ProcessFileChangedShell()
But it did not consistently fire for me. (Depending whether or not I had edited the file since the change, which in my case was external.)
There are some more tricks on the VimTips wiki which may help.
Add this to your ~/.vimrc file:
set autoread
nnoremap <C-u> :checktime<CR>
Now whenever you want vim to reload external changes, just click CTRL-U :)

ctags not working as expected with Vim plus general setup problems (C programming)

I have installed cvim and NodeTree plugins and generated an exuberant ctags file for my build tree.
This is what my ~/.vim/.vimrc file looks like:
:noremap :TlistToggle
:let Tlist_Show_One_File = 1
:let Tlist_Exit_OnlyWindow = 1
:let Tlist_Use_Right_Window = 1
set tags=./tags;/
set number
set tabstop=4
set incsearch
When I start editing a file, I notice that Ctrl ] does not work and I have to resort to typing ta: funcname - which gets tiring after a while. Interestingly enough, Ctrl T pops me off the tag stack as expected - I don't understand whats going on - how do I fix this?
Incidentally, vim (appears to) completely ignores the contents of my .vimrc file and I always have to type the same commands in the editor, so as to get the settings I want - very annoying.
Last but not the least, I used to be able to type :make in the editor window, drop to the console and then have the build results displayed in a little window which I can then go to and select a line (with an error or warning say), and then have the editor automagically take me to the offending line - unfortunately, I don't remember the plugin (or commands) I used to allow me to build from within vim.
So, how do I:
Fix my vim setup so that I can move to definitions/declarations using Ctrl-]
Fix my .vimrc file so that contents are actually applied to my vim session.
Find the appropriate plugin to install to allow builds (using make) from within vim
You're asking about a weird mix of problems.
Fix my vim setup so that I can move to definitions/declarations using Ctrl-]
The tags functionality is working; I suspect that you have a mapping blocking Ctrl-]. Try
:verbose nmap <C-]>
and
:nunmap <C-]>
Fix my .vimrc file so that contents are actually applied to my vim session.
:echo $MYVIMRC
will tell you the location of the .vimrc that Vim uses. Also, check the output of :scriptnames which scripts get loaded, and read :help vimrc to understand the logic Vim applies.
Find the appropriate plugin to install to allow builds (using make) from within vim
That's built into Vim. With the appropriate 'makeprg' set (it defaults to make), you can run :make. Vim parses the output (through the 'errorformat' option), and you can open the quickfix list via :copen.
Your vimrc is:
~/.vim/.vimrc
If you run Vim 7.4, it should be:
~/.vim/vimrc
or
~/.vimrc
If you run Vim 7.3 or older, it should be:
~/.vimrc
And... what Ingo said.

Show the list of the files inside the folder of the current file

Imagine I'm editing file, and I want to show the list of the files inside the folder who belongs the file I'm editing, to edit one of them.
How can I do that? Is there any way using FuzzyFinder?
Did you even read FuzzyFinder's documentation (:help fuzzyfinder)? Quickly opening nearby files is one of that plugin's main features.
Without installing anything, you can do:
:Ex[plore]
to open the netrw file tree. See :help netrw.
You can also do:
:e <Tab>
Add these lines to your ~/.vimrc to make command line completion even better:
set wildmenu
set wildmode=list:full
and read :help wildmenu and :help commandline-completion.
set autochdir is a useful option to add to your ~/.vimrc, by the way.
change vim current directory to current file's:
:cd %:h
then
FuzzyFinder can do what you want (pick and edit). (:FufFile) I have mapping :
nmap <Leader>ff :FufFile<cr>
NERDTree can do that as well.
Depends on what you mean by showing the file.
To include the list of files in the currently edited files, you can do something like:
:read !ls /path/to/file
(it can be shortened to :cd %:h | read !ls if you don't mind if vim changes it's current directory...)
If you want to pick another file to edit, I'd suggest to take a look at NerdTree plugin (here is a little intro). Or you can simply issue:
:cd %:h | e .

Resources