vim set number not working on InsertEnter - vim

As you will see from my vimrc file below I am trying to 'set number' on InsertEnter. In normal mode I use 'set relativenumber' to make it easier to more around with j,k. The config below work on my macbook but not on a RHEL box i'm running. Instead of doing 'set number' for the whole file when I enter Insert mode it sets number only for the line I am on. The rest of the file lines continue to display as relativenumbers.
How do I fix this? Any idea what is going on here?
execute pathogen#infect()
call pathogen#helptags()
syntax on
syntax enable
filetype plugin indent on
colorscheme molokai
set rnu
autocmd FocusLost * :set number
autocmd FocusGained * :set rnu
autocmd InsertEnter * :set number
autocmd InsertLeave * :set rnu
set rtp +=/home/evolution/.vim/bundle/powerline/powerline/bindings/vim
set splitright
set ttymouse=xterm2
set backspace=2
set laststatus=2
set t_Co=256
set laststatus=2
set showtabline=2
set noshowmode
set nocompatible
set background=dark
set shiftwidth=4 " operation >> indents 4 columns; << unindents 4 columns
set tabstop=4 " a hard TAB displays as 4 columns
set expandtab " insert spaces when hitting TABs
set softtabstop=4 " insert/delete 4 spaces when hitting a TAB/BACKSPACE
set shiftround " round indent to multiple of 'shiftwidth'
set autoindent " align the new line indent with the previous line
set cursorline
hi CursorLine cterm=NONE ctermbg=52
let g:CommandTAcceptSelectionMap = '<C-t>'
let g:CommandTAcceptSelectionTabMap = '<CR>'
let g:airline#extensions#tabline#enabled = 1
noremap <Up> <NOP>
noremap <Down> <NOP>
noremap <Left> <NOP>
noremap <Right> <NOP>
map <C-l> :tabn<CR>
map <C-h> :tabp<CR>

I found that this works:
set rnu
function ToggleNumbersOn()
set rnu!
set nu
endfunction
function ToggleRelativeOn()
set nu!
set rnu
endfunction
autocmd FocusLost * call ToggleNumbersOn()
autocmd FocusGained * call ToggleRelativeOn()
autocmd InsertEnter * call ToggleNumbersOn()
autocmd InsertLeave * call ToggleRelativeOn()

Replace the autocommand with set number by set nornu
set nu
autocmd InsertEnter * :set nornu
autocmd InsertLeave * :set rnu
Mark as fixed when fixed

You may be interested in Vim 7.4 hybrid relative-absolute number mode.
set relativenumber
set number
This will show relative numbers but what would be zero will be the current line number.
However if that won't work for you then there are a few plugins that do what you ask: numbers.vim and numbertoggle

Related

How to properly align closing brace after line wraps onto next line in Vim?

After a line of code wraps onto the next line, I find that the closing brace isn't aligned properly. Here's an image to demonstrate what's going on:
Ideally the closing brace should be in line with while.
Here are my vimrc settings:
filetype plugin indent on
syntax on
set backspace=indent,eol,start
set expandtab
set tabstop=2
set shiftwidth=2
set softtabstop=2
set showtabline=2
set number
set showcmd
set cursorline
set wildmenu
set lazyredraw
set showmatch
call plug#begin('~/.vim/plugged')
Plug 'scrooloose/nerdtree'
Plug 'drewtempelmeyer/palenight.vim'
Plug 'NLKNguyen/papercolor-theme'
Plug 'rakr/vim-one'
Plug 'morhetz/gruvbox'
Plug 'tpope/vim-commentary'
Plug 'ajh17/VimCompletesMe'
Plug 'sheerun/vim-polyglot'
call plug#end()
highlight ColorColumn ctermbg=gray
set colorcolumn=81
autocmd BufNewFile,BufRead * setlocal formatoptions=croqtn textwidth=80
set t_Co=256
set term=xterm-256color
let g:gruvbox_contrast_dark='dark'
colorscheme gruvbox
set background=dark
map <C-x> :NERDTreeToggle<CR>
noremap <TAB> <C-W>w
" autoclose matching quotes, braces and parentheses
inoremap {<CR> {<CR>}<ESC>O
inoremap {;<CR> {<CR>};<ESC>O
inoremap jj <Esc>
inoremap <Esc> <Nop>
inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
inoremap <C-y> <C-o>h
inoremap <C-u> <C-o>l

How to handle my vim local variable(omnifunc, ...), it is not set as i wrote in vimrc

I use omnicomplete in VIM and my vimrc is like below.
snipping...
" Autocompletion
set completeopt=longest,noselect,menuone
set omnifunc=syntaxcomplete#Complete
autocmd FileType py set omnifunc=python3complete#Complete
But, when I restart vim. omnifunc doesn't work as i set, even value is different from my vimrc settings
I thought my restoring option was problem. However, block below settings.
" Save and load former states
"autocmd BufWinLeave ?* mkview
"autocmd BufWinEnter ?* silent loadview
It still doesn't work...
How can I resolve it?
Appreciate all your help.
EDIT on 2017.11.08 > Here is my full .vimrc file
syntax on
set nocp " no compatibility with VI
set nu " line number
set cursorline " highlight current cursorline
set ruler " display cursor position information at status line
set ic " case insensitive search
set smartcase " don't use ic when there is Capital letter
set hlsearch " hilight search
set incsearch " show search matches as type
set mouse=a " enalbe cursor move with mouse
set ts=4 " size of \t character (tab stop)
set sw=4 " tab size, when use <<, >>
set sts=4 " how many spaces, when type tab (soft tab stop)
set ls=2 " last window's status line option
set expandtab smarttab
set autowrite " Automatically :write before running commands
set autoread " Auto read when a file is changed on disk
set autoindent
set smartindent
set cindent
set vb noeb " visual bell instead of beep
set tm=500 ttm=0 " to leave insert mode without delay
set encoding=utf8
" Autocompletion
set completeopt=longest,noselect,menuone
set omnifunc=syntaxcomplete#Complete
autocmd FileType py set omnifunc=python3complete#Complete
" Cursor shape
let &t_SI = "\e[5 q" " Start Insert mode
let &t_EI = "\e[0 q" " End Insert mode
" Key mapping
nnoremap <F2> :!ctags -R -I --languages=C,C++ --c++-kinds=+p --fields=+iaS --extra=+q .<CR>
nnoremap <F3> :NERDTreeToggle<CR>
nnoremap <F4> :TlistToggle<CR>
nnoremap <F5> <C-w>=
nnoremap Y y$
nnoremap n nzz
nnoremap N Nzz
nnoremap * *zz
nnoremap # #zz
inoremap <C-h> <Left>
inoremap <C-j> <Down>
inoremap <C-k> <Up>
inoremap <C-l> <Right>
inoremap <C-b> <C-Left>
inoremap <C-f> <C-Right>
inoremap <C-a> <Esc>I
inoremap <C-e> <End>
inoremap <C-#> <C-x><C-o>
autocmd FileType c,h,cpp,hpp inoremap {<ENTER> {}<Left><ENTER><ENTER><UP><TAB>
" Save and load former states
autocmd BufWinLeave ?* mkview
autocmd BufWinEnter ?* silent loadview
" C/C++ header
function! s:header()
let name = "__".toupper(substitute(expand("%:t"), "\\.", "_", "g"))."__"
exe "norm! i#ifndef ". name "\n#define ". name "\n\n\n\n#endif\t//". name "\ekk"
endfunction
autocmd BufNewFile *.{h,hpp} call <SID>header()
" Python header
function! s:py_init()
exe "norm! i\n\n\ndef main():\npass\n\n\eIif __name__ == \"__main__\":\n\tmain()\n\egg"
endfunction
autocmd BufNewFile *.py call <SID>py_init()
" Plugin settings using Vundle
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'
Plugin 'tpope/vim-fugitive'
Plugin 'scrooloose/nerdtree'
Plugin 'taglist-plus'
Plugin 'nanotech/jellybeans.vim'
call vundle#end()
filetype plugin indent on
" airline settings
set laststatus=2
let g:airline#extensions#tabline#enabled=1 " turn on buffer list
let g:airline_theme='murmur'
let g:airline_powerline_fonts=1
let g:airline#extensions#branch#enabled=1
" NERDTree settings
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
let g:NERDTreeDirArrowExpandable='+'
let g:NERDTreeDirArrowCollapsible='~'
" taglist settings
let Tlist_Use_Right_Window=1
" Color settings with bundle theme (type help highlight in order to see color list)
if has("gui_running")
colo industry " industry, torte, koehler
else
colo slate " slate, koehler, ron, elflord, pablo
endif
colo jellybeans
highlight linenr ctermfg=brown ctermbg=NONE
highlight cursorlinenr ctermfg=green ctermbg=NONE
highlight cursorline cterm=underline
You can find out where an option was last set via
:verbose set omnifunc?
It looks like your problem is caused by a wrong filetype in your :autocmd. Though Python files usually have a *.py extension, the filetype in Vim is named python. So, this should work:
autocmd FileType python set omnifunc=python3complete#Complete
Additional critique
I would recommend putting the settings and mappings into ~/.vim/after/ftplugin/python.vim instead of defining lots of :autocmd FileType python; it's cleaner and scales better; requires that you have :filetype plugin on, though.
By using :set, the value of 'omnifunc' will be inherited by other buffers that are opened from a Python one (e.g. via :new). Usually, this is not what you want. Use :setlocal omnifunc=... instead.

Gvim: Empty bottom bar appears on re-sourcing vimrc

I recently decided to migrate to Gvim, and noticed that some of my gui-specific settings are borked.
I have defined an autocommand to re-source $MYVIMRC upon writing to it, and upon re-sourcing it (this does not happen on the initial startup), an empty bar appears on the bottom. I believe it is the bar that is meant to contain the horizontal scrollbar.
When I attempt to remove the scrollbar using
:set guioptions-=b
nothing happens. However, if I add it first and then remove it,
:set guioptions+=b
:set guioptions-=b
it disappears.
I thought this might be related to how and when I am defining my guioptions. This is the relevant part of my .vimrc:
set guioptions-=m " GUI: Disable Menu
set guioptions-=r " GUI: Disable right scrollbar
set guioptions-=T " GUI: Disable Toolbar
I attempted to put this in an augroup like this
augroup GuiWidgets
au!
set guioptions-=m " GUI: Disable Menu
set guioptions-=r " GUI: Disable right scrollbar
set guioptions-=T " GUI: Disable Toolbar
augroup end
but it made no difference.
What can be the source of this problem?
EDIT 1 - Added .vimrc
let mapleader = ','
inoremap jj <Esc>
set nocompatible
"********************************
" PLUGINS
"********************************
if has('vim_starting')
set runtimepath+=~/.vim/bundle/neobundle.vim/
endif
call neobundle#begin(expand('~/.vim/bundle/'))
NeoBundleFetch 'Shougo/neobundle.vim'
NeoBundle 'benmills/vimux'
NeoBundle 'scrooloose/nerdcommenter'
NeoBundle 'scrooloose/syntastic'
NeoBundle 'kana/vim-textobj-entire'
NeoBundle 'kana/vim-textobj-user'
NeoBundle 'Raimondi/delimitMate'
NeoBundle 'Lokaltog/vim-easymotion'
NeoBundle 'godlygeek/tabular'
NeoBundle 'surround.vim'
NeoBundle 'christoomey/vim-tmux-navigator'
NeoBundle 'Shougo/vimproc.vim'
NeoBundle 'Shougo/unite.vim'
NeoBundle 'Shougo/vimshell.vim'
NeoBundle 'c.vim'
call neobundle#end()
filetype plugin indent on
NeoBundleCheck
"******************************
" PLUGIN SETTINGS
"******************************
nnoremap <C-p> :Unite file_rec/async:! -silent -start-insert -auto-resize<CR>
"******************************
" PLUGIN MAPPINGS
"******************************
map <Space> <Plug>(easymotion-prefix)
cnoreabbrev bi Unite neobundle/install
cnoreabbrev bu Unite neobundle/update
cnoreabbrev bc NeoBundleClean
nmap <Leader>a= :Tabularize /=<CR>
vmap <Leader>a= :Tabularize /=<CR>
nmap <Leader>a: :Tabularize /:\zs<CR>
vmap <Leader>a: :Tabularize /:\zs<CR>
nmap <Leader>a# :Tabularize /#<CR>
vmap <Leader>a# :Tabularize /#<CR>
"******************************
" GENERAL SETTINGS
"******************************
set t_Co=256
let g:molokai_original=0
colorscheme molokai
filetype plugin indent on
syntax on
set complete-=t
set gfn=Source\ Code\ Pro\ 10
set encoding=utf-8
set number
set lazyredraw
set completeopt-=preview
set ttyfast
set backspace=indent,eol,start
set hidden
set showmatch
set wildmode=longest:list,full
set wildmenu
set shortmess+=I
set showcmd
set showmode
set clipboard=unnamed
set history=50
set nrformats=
set autoread
set ruler
set laststatus=2
set scrolloff=8
set visualbell
set splitright
set smarttab
set autoindent
set expandtab
set tabstop=4
set shiftwidth=4
set softtabstop=4
set ignorecase smartcase
set gdefault
set incsearch
set hlsearch
set nobackup
set noswapfile
set ttimeoutlen=50
set relativenumber
set cursorline
set cc=80
set guioptions-=m
set guioptions-=r
set guioptions-=T
"set guioptions=
match ErrorMsg '\%>80v.\+'
"***************************
" GENERAL MAPPINGS
"***************************
map cn <Esc>:cn<CR>
map cp <Esc>:cp<CR>
nmap <CR> o<Esc>k
inoremap {<CR> {<CR>}<C-o>O
nnoremap <leader>v :e $MYVIMRC<CR>
nnoremap <leader>h :let #/ = ""<CR>
nnoremap n nzz
nnoremap <silent> <leader>; :call setline('.', getline('.').';')<CR>
nnoremap <leader>q <C-w>q<CR>
nnoremap <leader>w :w<CR>
nnoremap <silent> <leader>d :bp\|bd #<CR>
nnoremap <leader>c :cd %:p:h<CR>
cnoreabbrev ci( %ci(
cnoreabbrev ci) %ci)
cnoreabbrev ci[ %ci[
cnoreabbrev ci[ %ci]
cnoreabbrev ci{ %ci{
cnoreabbrev ci} %ci}
"***************************
" AUTOCOMMANDS
"***************************
" Auto-source .vimrc
augroup Vimrc
au!
au bufwritepost .vimrc source $MYVIMRC
augroup end
" Filetype-specific settings
augroup Filetypes
au!
"au FileType c nmap <leader>r :SCCompileRun<CR>
au FileType c nmap <leader>r :make\|!./%:r<CR>
au FileType c setlocal sw=2 sts=2
au FileType ruby nmap <leader>r :call RunByFiletype("ruby")<CR>
augroup end
EDIT 2 - Added pictures (sorry for the small size)
On startup
After sourcing .vimrc (the empty scrollbar container appears)
After running :set guioptions+=b (the horizontal scrolling bar is added to the container)
After running :set guioptions-=b (the horizontal container disappears)
After running :set gfn=Source\ Code\ Pro\ 10 (the bar reappears)
Here is Vim in a terminal
Another example where I've tiled GVim to the upper right corner
The normal edge size is on the left, while on the right, is a forced edge size due to tiling and font height.
Ended up creating a block to initialize the GUI-specific options I wanted to set at startup and to not be subject of resourcing of .vimrc. I will no longer see the "instant effect" of changing the guifont in $MYVIMRC, but this will keep the bottom-bar from appearing.
if has('gui_running')
if has('vim_starting')
set gfn=Source\ Code\ Pro\ 10
set guioptions-=m
set guioptions-=T
set guioptions-=r
set guioptions+=c
endif
endif

Where to locate vim undo settings and how to turn them off?

I'm having a lot of trouble with vim undo. I have 'set noundofile' in my ~/.vimrc and attached is a screen shot of my working dir's, it is super annoying having all the .un~ files all over the place. little help here thanks!
Below is my .vimrc
set nocompatible
exec pathogen#infect()
filetype plugin indent on
filetype plugin on
"syntax enable
syntax on
set background=light
set noundofile
let g:solarized_termtrans = 1
colorscheme solarized
set number
noremap <Up> <NOP>
noremap <Down> <NOP>
noremap <Left> <NOP>
noremap <Right> <NOP>
vnoremap < <gv
vnoremap > >gv
set runtimepath^=~/.vim/bundle/ctrlp.vim
autocmd FileType ruby set ft=ruby.rails
autocmd Filetype ruby setlocal ts=2 sts=2 sw=2
set nobackup " no backup files
set nowritebackup " only in case you don't want a backup file while editing
set noswapfile " no swap files
set clipboard=unnamed " use Mac clipboard for yank/paste/etc.
" expand %% to file dir
cnoremap %% <C-R>=expand('%:h').'/'<cr>
set autoindent " always set autoindenting on
set copyindent " copy the previous indentation on autoindenting
set shiftround " use multiple of shiftwidth when indenting with '<' and '>'
set smarttab " insert tabs on the start of a line according to
" shiftwidth, not tabstop
set ts=2 sts=2 sw=2 expandtab "set two spaces by default
autocmd Filetype javascript setlocal et ts=2 sts=2 sw=2
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
autocmd Filetype html setlocal et ts=2 sts=2 sw=2
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd Filetype css setlocal et ts=2 sts=2 sw=2
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
au BufRead,BufNewFile *.hamlc set ft=haml
" Vim-pasta Settings
let g:pasta_disabled_filetypes = ['python', 'coffee', 'yaml']
" Indent Guide Settings
autocmd FileType html,css,ruby,eruby,javascript,php,xml,haml call indent_guides#enable()
set mouse=a
imap <C-l> <Space>=><Space>
"Make hashrocket with control-l
nmap <silent> <Leader>q :NERDTreeToggle<CR>
I personally like the persistent undo feature. However you can change where the undofiles are located by setting undodir.
set undofile
set undodir=$HOME/.vim/vimundo
If you do this you must make sure $HOME/.vim/vimundo exists first by running
mkdir -p $HOME/.vim/vimundo
(You still have to delete the old ones but at least they aren't cluttering up the working directory anymore)
You can also do the same with backup files if you want. (:h backupdir)
Other notes about your vimrc.
exec pathogen#infect()
...
set runtimepath^=~/.vim/bundle/ctrlp.vim
The set runtimepath^=~/.vim/bundle/ctrlp.vim shouldn't be needed because pathogen should have already appended it to the runtimepath.
And as #romainl says filetype plugin on is redundant.
From :help 'undofile':
boolean (default off)
[…]
When 'undofile' is turned off the undo file is NOT deleted.
so…
you don't need to set noundofile because it is off by default,
you will need to remove all those file by yourself.
Note that the undofile feature was implemented in Vim version 7.3. If you are using an earlier version and include set undofile or set noundofile in your .vimrc, you will get an error like this:
E518: Unknown option: noundofile
Ideas for checking the version of Vim to prevent these errors can be found here.

Vim slow over UNC paths

I am experiencing some very painful lag when accessing directories/files over UNC paths using gVim 7.3 on Windows Vista.
It is slow reading/writing files, as well as tab completiong of directory/file names when opening new buffers. I don't notice this lag when using WordPad though.
Things I've tried:
Cream (apparently they had a fix for directory naming schemes)
Mapping the network drive to z: or something else
Various settings
set ffs=dos
set complete=.,w,b,u,t
set noshellslash
I've tried cygwin, but the same noticeable lag appears there as well. I already have all swap/backup files turned off. Any help greatly appreciated... I've dumped my vimrc for reference
if v:progname =~? "evim"
finish
endif
set nocompatible
:runtime! ftplugin/man.vim
set backspace=indent,eol,start
colorscheme torte " murphy
syn on
set ffs=unix,dos
" portable friendly
set nobackup
set nowritebackup
set noswapfile
set viminfo=
" gui options (http://steveno.wordpress.com/2007/10/08/vim-for-windows/)
set guioptions-=T " No toolbar
set gfn=Consolas:h9:cANSI
set history=50
set ruler
set showcmd
set incsearch
set number
set tabstop=4
set softtabstop=4
set shiftwidth=4
set wrap
set wildmode=longest,list,full " Complete longest string, list alternatives
" then completed next full match, cycling back
function! ToggleHLSearch()
if &hls
set nohls
else
set hls
endif
endfunction
function! InsertTabWrapper()
let col = col('.') - 1
if !col || getline('.')[col - 1] !~ '\k'
return "\<tab>"
else
return "\<c-p>"
endif
endfunction
inoremap <tab> <c-r>=InsertTabWrapper()<CR>
nmap <silent> <C-n> <Esc>:call ToggleHLSearch()<CR>
nmap ,s :source ~/.vimrc<Return>
" change current directory to that of open buffer
nmap ,c :cd %:p:h<Return>
nmap <c-h> <c-w>h<c-w><bar>
nmap <c-l> <c-w>l<c-w><bar>
map <C-J> <C-W>j<C-W>_
map <C-K> <C-W>k<C-W>_
map Q gq
" Commenting blocks of text
" ,< <!-- --> html style comments
map ,< :s/^\(.*\)$/<!-- \1 -->/<CR><Esc>:nohlsearch<CR>
" ,/ // comments
map ,/ :s/^/\/\//<CR>
" ,# # comments
map ,# :s/^/#/<CR>
" uncommenting all of the above
"map ,- :s/^\(\/\/|<!-- |#\)\(.*\)\(-->\)*/\1/<CR>
if &t_Co > 2 || has("gui_running")
syntax on
set hlsearch
endif
if has("autocmd")
"&& !exists("autocommands_loaded")
let autocommands_loaded = 1
filetype plugin indent on
augroup vimrcEx
au!
" Remove ALL autocommands for the current group.
autocmd!
autocmd FileType text setlocal textwidth=78
autocmd BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal g`\"" |
\ endif
au BufRead *.html set filetype=html4
augroup END
" allow editing Word Docs sanely
autocmd BufReadPre *.doc set ro
autocmd BufReadPre *.doc set hlsearch!
autocmd BufReadPost *.doc %!antiword "%"
" uncomment the following to remember the view of the file edited between
" sessions
" au BufWinLeave * mkview
" au BufWinEnter * silent loadview
" run file with PHP CLI (CTRL-M)
autocmd FileType php noremap <C-M> :w!<CR>:!/usr/bin/php %<CR>
" parser check (CTRL-L)
autocmd FileType php noremap <C-L> :!/usr/bin/php -l %<CR>
" highlight current line only for current buffer
"autocmd BufLeave * setlocal nocursorline
"autocmd BufEnter * setlocal cursorline
au BufRead,BufNewFile *.tea set filetype=tea
"au! Syntax newlang source $VIM/newlanguage.vim
else
set autoindent
endif
the same thing was driving me insane, but there is a fix I just found.
I really do not know why yet, but the problem goes away when you disable plugin/matchparen.vim plugin (changing vim extension would do the trick).
I will definitely look into it as I use parentheses matching a lot.
See
:he swap-file
You can disable swapfiles with :se noswapfile (caution!) or use the `directory` setting to keep the swapfile on a local disk.
Starting vim with the -n option also disables swapfiles; you might want to combine that with -R for readonly mode
Another solution I found is that mounting a UNC path as a local drive letter and let Vim use the drive-letter based paths only.
e.g., \\some-server\path\to\workdir => Z:\path\to\workdir
This eliminates the lag completely unless you have connection problems.

Resources