Vim Editor .vimrc error during customizaiton [duplicate] - vim

This question already has answers here:
error while running "source .vimrc"
(2 answers)
Closed 2 years ago.
I has suffered while customizing my vim editor.
Specifically, I add function which store cursor location.
Below is my .vimrc file.
au BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "norm g`\"" |
\ endif
And Error content is below :
au: command not found
-bash: /home/ubuntu/.vimrc: line 24: syntax error near unexpected token `('
-bash: /home/ubuntu/.vimrc: line 24: `\ if line("'\"") > 0 && line("'\"") <= line("$") |'
Could you give me the solution ?
Take care of your health during COVID-19.
Thanks !
I found solution that change file authority.
chmod +777 ~/.viminfo
.vimsrc file compiled still suffering error.
But function that restore cursor position is working.
Reference : https://askubuntu.com/questions/202075/how-do-i-get-vim-to-remember-the-line-i-was-on-when-i-reopen-a-file

You don't need to source .vimrc from the command line, this will be done automatically by vim. Just make sure it's in the right place ($HOME/.vimrc)
Then when you open vim, it will source automatically.
You can view how to debug, where you will see if your file has been sourced in this answer How to see which plugins are making Vim slow?

Related

Jump to last position when using vim netrw to open a file

I am looking to jump automatically to the last position in any file that I open.
I have in my ~/.vimrc file:
autocmd BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
This works fine if I open a file using the vim command.
But if I write vim . in a directory to load netrw and use t to open a file in a new tab, the cursor appears at the top of the file and not at the last position.
I then have to use `" to jump to the last position.
Is there any way to jump to the last position when using netrw and t?
Thanks.
I found an easy workaround. Just adding to ~/.vimrc
let g:netrw_browse_split = 3
allows me to hit enter to open the file in a new tab and jump to the last position.
It is technically not a solution to the problem that I posted, but it works well enough for me.
I have also incorporated this: https://vi.stackexchange.com/questions/13344/open-multiple-files-in-tabs-from-explore-mode.
I just added
`\"
to a couple of lines to yield
let command .= "tgT`\":" . ( a:firstline + i ) . "\<CR>:+tabmove\<CR>"
and
let command .= "t`\"gT"
This allows me to open multiple files at once and to jump to the last position in each file.

Vim Last Position Mark Only Works with Sudo

on a new install of Ubuntu 16.04.3 LTS, I am having trouble with vim jumping to the last position on file reopen. For some reason the mark '" only works when I do sudo vim file, otherwise it doesn't. Because of this, the following in my vimrc file (/etc/vim) isn't doing anything
if has("autocmd")
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif
But this line works fine with sudo vi file? I've been at this for a few hours now and it's really bothering me. This works on other machines just fine though (without sudo) such as a Mac I have.
The last position in a file is stored in a mark. Marks are saved in ~/.viminfo. If you have a .viminfo in your home directory that is owned by a different user, vim can't write to it.
In your case you had a ~/.viminfo owned by root. Running sudo vim worked because vim was running as root, but your normal user didn't have permissions to update the file.
By deleting the root-owned ~/.viminfo you clear the way for vim to re-create the file on the next run, this time as your normal user.

Error detected message while using Vim editor

When I try to open a file for editing using vim I get this following message and error detected message.
Jashs-MacBook-Pro:hello jashjacob$ vim hello.rb
--- Auto-Commands ---
filetypedetect BufRead
*if !did_filetype() && expand("<amatch>") !~ g:ft_ignore_pat | runtime! scripts.vim | endif
*if !did_filetype() && expand("<amatch>") !~ g:ft_ignore_pat && (getline(1) =~ '^#' || getline(2) =~ '^#' || getline(3) =~ '^#'^I|| getline(4) =~ '^#' || getline(5) =~ '^#') | setf conf | endif
Error detected while processing /usr/share/vim/vimrc:
line 18:
E116: Invalid arguments for function line("'"") > 0 && line ("'"") <= line("$") |
E15: Invalid expression: line("'"") > 0 && line ("'"") <= line("$") |
Press ENTER or type command to continue
Have my vim editor files been damaged/modified ? How can i fix the error detected while processing /usr/share/vim/vimrc ?
/usr/share/vim/vimrc config file contains the following
" Configuration file for vim
set modelines=0 " CVE-2007-2438
" Normally we use vim-extensions. If you want true vi-compatibility
" remove change the following statements
set nocompatible " Use Vim defaults instead of 100% vi compatibility
set backspace=2 " more powerful backspacing
set ai " auto indenting
set history=100 " keep 100 lines of history
set ruler " show the cursor position
syntax on " syntax highlighting
set hlsearch " highlight the last searched term
filetype plugin on " use the file type plugins
" When editing a file, always jump to the last cursor position
autocmd BufReadPost *
if ! exists("g:leave_my_cursor_position_alone") |
if line("'"") > 0 && line ("'"") <= line("$") |
exe "normal g'"" |
endif |
endif
" Don't write backup file if vim is being called by "crontab -e"
au BufWrite /private/tmp/crontab.* set nowritebackup
" Don't write backup file if vim is being called by "chpass"
au BufWrite /private/etc/pw.* set nowritebackup
I'm not a vimscript guru, so perhaps someone will be able to offer more of a fix, but I can suggest that if you just want to get vim working again you could comment out the function that's causing the trouble. Precede each line with a double-quote symbol (") and you should be able to get on with things.
Like so:
" When editing a file, always jump to the last cursor position
"autocmd BufReadPost *
" if ! exists("g:leave_my_cursor_position_alone") |
" if line("'"") > 0 && line ("'"") <= line("$") |
" exe "normal g'"" |
" endif |
" endif
If you want to preserve this functionality, you might have to learn a little vimscript. :)
I think these error messages could also come from the fact that you missed or miss-placed "set nocompatible" in your .vimrc. That will cause your Vim misunderstood the backslashes used to continue long lines in it's scripting files. It's better to put "set nocompatible" at the very beginning of your .vimrc file.
If you see "if !did_filetype() ..." in the error message and compared it with the code in /usr/share/vim/vim{yourversion}/filetype.vim you can tell that there should be a "\" in between "" and "if".
see this entry for more details
YOU ARE NOT SUPPOSED TO DO ANYTHING IN /usr/share/vim/.
That file you have edited is not yours and should not be changed at all. These are the files and places where you can do whatever you want:
~/.vim/ <-- put your plugins and colorschemes in this directory
~/.vimrc <-- add your options and mappings to this file
As for your question:
Those lines were not added by a plugin, they were added by you or someone else who has access to your machine.
They throw an error because the dumbass who pasted them somehow thought it was a good idea to remove the line-continuation backslashes that are in the "original" (or this "original", or that one, or the older link in the post that leads to a 404, or the simpler version that is in $VIMRUNTIME/vimrc_example.vim).
Never run a command you find on the internet unless you are sure it won't cause troubles.

Vimrc not saving cursor position though enabled

My vim does not save the cursor position (at least not reliable) though I am using:
autocmd BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal g'\"" |
\ endif
In my vimrc.
My complete vimrc is here.
I already reenabled nobackup and noswapfile but the problem remains.
What could the reason be for this?
You need to use the motion g`" to jump to the last known line and column. You are using g'", which jumps to the first character on that line.
The executed command in your code should be:
exe "normal g`\""

Disable warning in VIM?

Is there a way to disable warnings in VIM?
In particular, I want to disable Warning 12 when a file turns from read-only to writable. I have a script which opens the file for edit in perforce, but vim thinks the file has changed and issues a warning.
Thanks
I have the following in my .vimrc; you should only need the second one. It echoes the message to the status bar instead of popping up a dialog.
autocmd FileChangedRO * echohl WarningMsg | echo "File changed RO." | echohl None
autocmd FileChangedShell * echohl WarningMsg | echo "File changed shell." | echohl None
Try :help FileChangedShell for more information.
add the following line to your .vimrc file :
set autoread
This will disable "read-only to writeable" warnings
I've been using FileChangedRO for a while now to automatically checkout files when starting to edit them and found the W12 warning annoying as well. The problem is that p4 edit updates the file attributes to remove the read only flag. If as part of the initial edit you also change the file, Vim sees this as a conflict since it's no longer read only. Here's the solution I use which is a bit more conservative about using FileChangedShell in case the file was changed externally for some other reason.
let s:IgnoreChange=0
autocmd! FileChangedRO * nested
\ let s:IgnoreChange=1 |
\ call system("p4 edit " . expand("%")) |
\ set noreadonly
autocmd! FileChangedShell *
\ if 1 == s:IgnoreChange |
\ let v:fcs_choice="" |
\ let s:IgnoreChange=0 |
\ else |
\ let v:fcs_choice="ask" |
\ endif

Resources