How to use different .vimrc than what is the default .vimrc - vim

I share lab server with my colleagues, but want my separate .vimrc file. How to get that?
$ cat .vimrc
color desert
$ pwd
/fvs101/home
We have our separate working directory, so my directory is inside
/fvs101/home/sp
At /fvs101/home there is common .vimrc file.
I want to play with vim but do not want to touch this /fvs101/home/.vimrc file.
I want to create my own .vimrc file.
++++++++++my vimrc file
[sp]$ cat .vimrc
noremap - ddp
noremap _ dd2kp
inoremap <c-u> <esc>lviwU<esc>i
nnoremap <c-u> <esc>viwU<esc>
let mapleader = "-"
let maplocalleader = "\\"
nnoremap <leader>ev :vsplit $MYVIMRC<cr>
nnoremap <leader>sv :source $MYVIMRC<cr>
iabbrev ssig -- <cr>Sachin Pawar<cr>sachin.sp.pawar#oracle.com
vnoremap <leader>' vi<esc>`<<esc>i'<esc>`><esc>i'<esc>
nnoremap H 0
nnoremap L $
inoremap jk <esc>
inoremap <esc> <nop>
augroup filetype_js
autocmd!
autocmd FileType javascript nnoremap <buffer> <localleader>c I//<esc>
augroup END
augroup filetype_python
autocmd!
autocmd FileType python nnoremap <buffer> <localleader>c I#<esc>
augroup END
augroup filetype_sql
autocmd!
autocmd FileType sql nnoremap <buffer> <localleader>c I--esc>
augroup END
augroup filetype_shell
autocmd!
autocmd FileType shell nnoremap <buffer> <localleader>c I#esc>
augroup END
augroup filetype_html
autocmd!
autocmd FileType html nnoremap <buffer> <localleader>f Vatzf
augroup END
++++++++++
If i use my separate .vimrc file i am not able to see the mapping for some mappings like
inoremap jk
Why is this happening and how to avoid it.
In order to use my own .vimrc file i have create a separate alias.
alias vi='vim -u /fvs101/home/sp/.vimrc'

Make a different .vimrc (as you have stated in the comments, /refresh/oracle/spawar/.vimrc) and make an alias for your own user
myvi='vim -Nu /refresh/oracle/spawar/.vimrc'
Note that without the -N flag, using the -u flag will start vim in compatibility mode causing all sorts of nasty, unexpected side-effects such as mappings not working entirely.
I found this out by going to see what the -u flag really does and following from there.
Helpful links below.
u flag
N flag

Seeing as you specifically want everyone to log into the same user, the easiest way I can think of how to do this is if you make your own .vimrc (give it a short name, such as ~/.vrc) and every time you open vim, you type
:so ~/.vrc
which will load that vimrc for you. Every time you open vim it will use the defaults, until you load your own vimrc, so you will have to do this each and every time. Also remember that this way, ~/.vimrc loads automatically before you manually load ~/.vrc

You can call Vim with a different runtime configuration file than the default one with
vim -N -u path/to/your/vimrc
If you want to use this always, edit your users shell runtime configuration to define an alias for that.
alias vim="vim -N -u path/to/your/vimrc"

I think the simplest solution would be to append a single line to the end of the default vimrc which reads:
source ~/.vimrc.local " Or whatever you want to call it
Where .vimrc.local contains your custom configuration.

Related

How do I use the same keymap for different commands depending on the file type?

I have the following config, but when I enter an eruby file and then go back to a different file, F3 still executes :Autoformat.
noremap <F3> :Neoformat<CR>
autocmd FileType eruby bufdo map <F3> :Autoformat<CR>
I want it to only apply that command while in eruby buffers.
First, don’t use bufdo here; it executes a command for all buffers. Second, prefer <buffer> mappings.
With autocommands:
augroup vimrc_eruby
au!
au FileType eruby noremap <buffer> <F3> :Autoformat<CR>
augroup END
But I highly encourage reading about ftplugins, and using ~/.vim/after/ftplugin/eruby.vim. Read about setlocal, map-<buffer>, and b:undo_ftplugin in vim’s help.
I’ve written answers about using these tools on Vi & Vim StackExchange a few times: https://vi.stackexchange.com/a/22256/10604, https://vi.stackexchange.com/a/15329/10604, https://vi.stackexchange.com/a/15019/10604

How to change nnoremap for different file types?

When I hit <leader>r inside a PHP file, I want to do:
nnoremap <leader>r ifile_put_contents(ini_get('error_log'), '');<cr>error_log(print_r(, true));<esc>
But when I do the <leader>r inside a JS file, I want to do:
nnoremap <leader>r iconsole.log();<esc>
How could I do this?
I also want to position the cursor inside the console.log/print_r. Any idea how to do that as well?
You can use autocomand and map to the buffer only. Here's an example:
augroup quick_debug
autocmd!
autocmd FileType php nnoremap <buffer> <leader>r ifile_put_contents(ini_get('error_log'), '');<cr>error_log(print_r(, true));<esc>
autocmd FileType javascript nnoremap <buffer> <leader>r iconsole.log();<esc>
augroup END
To move cursor around you can use <left> and <right> before <esc>. Like this: nnoremap <buffer> <leader>r iconsole.log();<left><left><esc>.
If you don't mind using plugins you could take a look at the UltiSnips and use snippets instead of mappings.
You can define that for certain filetypes by prepending :autocmd Filetype {filetype} ..., and put that into your ~/.vimrc. But that gets unwieldy as you add mappings and other settings for various filetypes. Better put the commands into ~/.vim/ftplugin/{filetype}_mappings.vim. (This requires that you have :filetype plugin on.)

Change the mapping of F5 on the basis of specific file type

The current mapping of my F5 key is:
imap <F5> <esc>:w\|!python %<CR>
Now I want that if I'm editing any python file (it will be better if it also recognizes file other than standard .py format like .pyd etc) then this mapping works as it is. But, if I edit a Java file it is mapped to something like:
imap <F5> <esc>:w\|!javac %<CR>
And when I'm editing any .c or .cpp file then F5 is mapped to this:
imap <F5> <esc>:w\|!make %<CR>
I have no idea how to proceed.
There are problems with both given answer and original mapping. First of all, for buffer-local mappings there is *map <buffer>. Second, with <buffer> you don’t need to use BufEnter events and can instead use Filetype which are launched only once. Third, you have one error (2.), one potential problem (1.) and one place that can be optimized in original mappings:
you should not be using imap, it makes it very easy to accidentally break old mappings when adding new ones
!python % will break once file contain a special symbol (space, semicolon, quot, dollar, …)
using :update instead of :write avoids useless writes in some cases
My variant:
autocmd Filetype c,cpp inoremap <buffer> <F5> <C-o>:update<Bar>execute '!make '.shellescape(expand('%:r'), 1)<CR>
autocmd Filetype python inoremap <buffer> <F5> <C-o>:update<Bar>execute '!python '.shellescape(#%, 1)<CR>
autocmd Filetype java inoremap <buffer> <F5> <C-o>:update<Bar>execute '!javac '.shellescape(#%, 1)<CR>
Try this:
au BufEnter *.py map <F5> <esc>:w\|!python %<CR>
au BufEnter *.java imap <F5> <esc>:w\|!javac %<CR>
au BufEnter *.c, *.cpp imap <F5> <esc>:w\|!make %<CR>
I'm not 100% sure about the comma separated file types, trying to verify...
The vim docs are usually pretty nasty to try to figure out how to use commands, but these should help get you started:
:h BufEnter
:h :autocmd
Note: You may have to restart vim for these changes to overwrite the current autocommand groups.

Can you have file type-specific key bindings in Vim?

In my .vimrc file, I have a key binding for commenting out that inserts double slashes (//) at the start of a line:
" the mappings below are for commenting blocks of text
:map <C-G> :s/^/\/\//<Esc><Esc>
:map <C-T> :s/\/\/// <Esc><Esc>
However, when I’m editing Python scripts, I want to change that to a # sign for comments
I have a Python.vim file in my .vim/ftdetect folder that also has settings for tab widths, etc.
What is the code to override the keybindings if possible, so that I have Python use:
" the mappings below are for commenting blocks of text
:map <C-G> :s/^/#/<Esc><Esc>
:map <C-T> :s/#/ <Esc><Esc>
You can use :map <buffer> ... to make a local mapping just for the active buffer. This requires that your Vim was compiled with +localmap.
So you can do something like
autocmd FileType python map <buffer> <C-G> ...
The ftdetect folder is for scripts of filetype detection. Filetype plugins must be inside the ftplugin folder. The filetype must be included in the file name in one of the following three forms:
.../ftplugin/<filetype>.vim
.../ftplugin/<filetype>_foo.vim
.../ftplugin/<filetype>/foo.vim
For instance, you can map comments for the cpp filetype putting the following inside the .../ftplugin/cpp_mine.vim:
:map <buffer> <C-G> :s/^/\/\//<Esc><Esc>
:map <buffer> <C-T> :s/\/\/// <Esc><Esc>
I prefer to have my configuration in a single file so I use the autocmd approach.
augroup pscbindings
autocmd! pscbindings
autocmd Filetype purescript nmap <buffer> <silent> K :Ptype<CR>
autocmd Filetype purescript nmap <buffer> <silent> <leader>pr :Prebuild!<CR>
augroup end
Vim doesn't clear set autocmds when you source your vimrc, so starting vim, changing something in your vimrc and running :so ~/.vimrc would define autocmds twice. That's why the bindings are grouped and cleared with autocmd! group_name. You can read more here.
Since mappings are applied to every buffer by default, and you want to change them for buffers matching the filetype only, the <buffer> modifier is in there, limiting the mappings to the local buffer.
Btw... if your primary problem is about commenting... you should check out 'nerdcommenter' plugin, its the fastest way to comment/uncomment your code in java/c/c++/python/dos_batch_file/etc etc.
This is only a partial answer for people coming here having difficulties getting any ftplugin scripts working, but remember that your .vimrc (or a file that it sources) should contain
filetype plugin on
or
:filetype plugin on
for filetype-plugins to be executed when a file of a given type is loaded.
I recommend the .../ftplugin/<filetype>.vim approach that freitass suggests, but in your specific case Vim Commentary will solve all of this for you.

Vim: <leader> key sequences are interfused with normal mode mappings

I'm trying to define mappings for "cpp" filetype:
autocmd! FileType cpp map <leader>c echo "test c"
autocmd! FileType cpp map <leader>r echo "test r"
autocmd! FileType cpp map <leader>t echo "test t"
My leader key is redefined:
let mapleader = ","
When I open *.cpp file only one of the mappings works as expected: the ",t" sequence makes the "echo" happen, but the other two behaves as if there were no any mappings defined: ",r" makes Vim to switch to replace mode and ",c" brings Vim to insert mode.
What am I doing wrong?
I don't know how your example (including ,c one) could work: you forgot to prepend echo with a colon. But there is another mistake: according to the help:
:au[tocmd]! [group] {event} {pat} [nested] {cmd}
Remove all autocommands associated with {event} and {pat}, and add the command {cmd}.
So, the last one (,t) should replace all previous ones.
There are also another things you should to consider:
Don't use map, use noremap instead: it may once save you from doing a debug job after you add some mapping.
If you define filetype mapping you should normally add <buffer> as an argument to :noremap in a position before {lhs}: it will make this mapping local to the current buffer.
In case you want to re-source vimrc you should put all command into augroup:
augroup VimrcCpp
autocmd!
autocmd FileType cpp nnoremap <buffer> <leader>c :echo "c"<CR>
autocmd FileType cpp nnoremap <buffer> <leader>r :echo "r"<CR>
autocmd FileType cpp nnoremap <buffer> <leader>t :echo "t"<CR>
augroup END
I would have removed it from vimrc and added to ~/.vim/ftplugin/cpp.vim (after directory is to be used only to override options that you don't like if they are unconditionally defined by a plugin. If there are no mapping conflicts, don't use after).
I suggest you put those mapping in a file called cpp.vim placed in your ~/.vim/after/ftplugin/ (or any equivalent based on OS) directory.
Vim automatically loads it as long as you have
filetype plugin on
in your .vimrc.
This saves you writing autocommands and keeps your configuration well organized.

Resources