Errors while sourcing .vimrc - vim

I am using bash terminal and in my .vimrc I have the following lines:
set number
"set tags+=/users/vinod/ctags_related/tags
imap 2s <SPACE><SPACE>
When I save and then try to source my .vimrc, I get the following errors:
bash: /users/vinod/.vimrc: line 2: unexpected EOF while looking for matching `"'
bash: /users/vinod/.vimrc: line 4: syntax error: unexpected end of file
The line number shown in the second error doesn't exist in my .vimrc
Why am I getting these errors? How can I fix them?
TIA
Vinod

If you are doing source ~/.vimrc from the terminal prompt, then you are getting errors you should expect. The .vimrc file contains vimscript, which only the vim program understands. Bash is giving you errors because it is not supposed to source that file. When you start vim, normally by just typing vim and hitting enter, vim will automatically source your .vimrc

Related

Syntax for .vimrc not being understood by bash

I've written multiple .vimrc files before, and every one of them has worked great - until this one. I'm currently running LMDE 4 (Linux Mint Debian Edition 4), and some very odd behavior is coming up. Here's a few examples of some test cases:
~/.vimrc
" Test comment
Terminal
usr#computer:~$ source ~/.vimrc
bash: /home/usr/.vimrc: line 1: unexpected EOF while looking for matching ':'
bash: /home/usr/.vimrc: line 2: syntax error: unexpected end of file
usr#computer:~$
In other words, even if the .vimrc is nothing but a single comment line, it doesn't seem to be understood.
Here's another fun example...
.vimrc
" Test comment 1
set number
" Test comment 2
set expandtab
Terminal
usr#computer:~$ source .vimrc
Test comment 1
set number
: command not found
usr#computer:~$
And, finally, if I exclude all comments...
.vimrc
set colorscheme default
Terminal
usr#computer:~$ source .vimrc
usr#computer:~$ vim .vimrc
Error detected while processing /home/usr/.vimrc:
line 1:
E518: Unknown option: colorscheme
Press ENTER or type command to continue
Note that it seems to source fine here, but when I go to reopen the file with vim, it spews out this error. Also note that this seems to potentially be specific to colorscheme (does not happen if I were to do set number or set expandtab, presumably some other commands are also readable).
Any thoughts/ideas? I'm seriously stumped. Thanks in advance.
.vimrc is a Vim script, not a shell script. Both Vim and shell have separate source commands which executes the commands in a file in Vim or shell, respectively.
.vimrc is sourced by Vim when Vim starts; it has nothing to do with the shell.

error when trying to implement vim-plug in .vimrc

I am trying to install vim-plug and I followed your instructions in your tutorial. I have written in .vimrc like so:
call plug#begin('~/.vim/autoload')
call plug#end()
but I get the following errors:
-bash: /Users/user/.vimrc: line 18: syntax error near unexpected token `('
-bash: /Users/user/.vimrc: line 18: `call plug#begin('~/.vim/autoload')'
Do you know what I am doing wrong?
Note how the error messages you're receiving start with -bash:, which indicates these lines are being parsed by bash, not Vim.
Can you somehow explain how that is happening?
Are you by any chance sourcing this file from your shell? Are you referring to .vimrc from somewhere? How are you starting Vim exactly? Do you have some commands in your .vimrc around these lines that would cause them to be passed to a shell?
Figuring out how this is happening should make it easy for you to figure out how to fix it.

.vimrc "set pastetoggle=<F2>" giving error while running "source .vimrc"

I'm using CentOS 7 and added set pastetoggle=<F2> in .vimrc to toggle the auto-indentation.
While running source .vimrc, it is throwing the below error
-bash: .vimrc: line 4: syntax error near unexpected token `newline'
-bash: .vimrc: line 4: `set pastetoggle=<F2>'
Here's my .vimrc file for reference
set ai
set tabstop=2
set expandtab
set pastetoggle=<F2>
Please help me and let me know, how to resolve the issue?
You are trying to source a vim config file in your shell, which would indeed throw an error. If you are trying to update the vim config on the go, then after updating your ~/.vimrc file(assuming you are using vim to edit the vimrc file) , from vim open the command line mode by pressing : and type in source % (where % denotes current file). Then your new config gets updated on the go.
If you're lazy like me then bind it to a key for sourcing, like so in normal mode :
nnoremap <leader>so :w<cr>:source %<cr>
If you are using some other text editor to edit ~/.vimrc, then save and exit the file, then open a new instance of vim and your changes get updated automatically.

Regex in .vimrc throwing "Not an editor command"

I am trying to create a keybinding to delete all the commented lines in the file.
The following gives me the desirable result :g/^\(#\|$\)/d
I am declaring the binding as following in ~/.vimrc
nnoremap <leader>dcl :g/\v^(#|$)/d<cr>
It is throwing the following error:
E492: Not an editor command: $)/d<cr>
What is the proper way to declare a keybinding with regex?
Replace your binding by:
nnoremap <leader>dcl :g/\v^(#<bar>$)/d<cr>
Indeed, in scripts or in the command line, vim considers | characters as a command delimiter; so you have to replace it by <bar>.

syntax error near unexpected token `newline' for my nnoremap in .vimrc?

Here is the configuration of my .vimrc.
For the 12 th line.
nnoremap <F12> :!/opt/google/chrome/chrome '%:p'<CR>
source .vimrc
bash: /home/debian9/.vimrc: line 12: syntax error near unexpected token `newline'
bash: /home/debian9/.vimrc: line 12: `nnoremap <F12> :!/opt/google/chrome/chrome '%:p'<CR>'
How to fix it ?
Don't use source in your Bash shell; the right command is :source .vimrc inside Vim!
Please note that reloading your Vim configuration may work, or not (depending what you do, how you define :autocmds, whether the changed configuration affects already loaded plugins). If in doubt, it's easier to just :quit Vim and restart it; the new .vimrc will then be loaded automatically.
The syntax of your Vim configuration in ~/.vimrc is Vimscript; this is only understood by Vim itself. Your shell configuration would be put in (for example) ~/.bashrc; you can source that in your shell.

Resources