Only use relative linenumbers in current buffer for Vim in Terminal - vim

I want to use relativenumbers only for the current buffer and when Vim is that active pane in Tmux. This is so that I can use gdb in one pane and vim in another and be able to see the actual linenumbers in the Vim C sourcefile.
This is what im trying right now:
autocmd BufLeave * : set number
autocmd BufEnter * : set relativenumber
This does however not give me the wanted behavior since nothing happens when I switch out of Vim.

you need setlocal, try these two lines in your vimrc:
autocmd BufLeave * : setlocal norelativenumber
autocmd BufEnter * : setlocal relativenumber

Related

Autoload netrw when starting vim

I want netrw to autoload when I launch vim using the terminal. Completely new to linux/ubuntu. Is there any way of doing that?
Adding the following to your .vimrc (Vim's configuration file, located in the root of your home directory) will cause Vim to automatically load Netrw after starting up.
" Open Netrw after Vim starts up
augroup InitNetrw
autocmd!
autocmd VimEnter * :silent! Explore
augroup END
A problem with the preceding approach, as implemented, is that Netrw will also load when you use Vim with an argument to open a specific file. A workaround is to use the following modification, based on the suggested approach in Netrw's documentation (:help netrw-activate).
" Checks if there is a file open after Vim starts up,
" and if not, open the current working directory in Netrw.
augroup InitNetrw
autocmd!
autocmd VimEnter * if expand("%") == "" | edit . | endif
augroup END
The following pages have more details on autocommands and the .vimrc configuration file.
https://learnvimscriptthehardway.stevelosh.com/chapters/12.html
https://learnvimscriptthehardway.stevelosh.com/chapters/14.html
https://learnvimscriptthehardway.stevelosh.com/chapters/07.html
And the following code block in your vimrc:
set autochdir
let g:netrw_browse_split=4
augroup InitNetrw
autocmd!
autocmd VimEnter * if argc() == 0 | Lexplore! | endif
augroupend
Kind of does what #dannyadam suggested. But opens the netrw pane as a side bar on the right. If you want to be on the right use Lexplore without the bang(!).

How can I set nornu/rnu for CmdwinEnter/CmdwinLeave

I've want to make relativenumber disabled in the command-line mode. Because somethings I need know the absolute linenumbers in the command-line mode (goto someline by :<line_number>)
my setting as below, but it won't work...
autocmd CmdwinEnter * set norelativenumber
autocmd CmdwinLeave * set relativenumber
Somehow, this setting (Get fome here) work fine:
autocmd CmdwinEnter * let b:ei_save = &eventignore | set eventignore=CursorHold,InsertEnter
autocmd CmdwinLeave * let &eventignore = b:ei_save
I want to know why my CmdwinEnter/CmdwinLeave for **relavitenumber` didn't work, and how can I make it.
By the way,
This method works fine in Insert mode by:
autocmd InsertEnter * set norelativenumber
autocmd InsertLeave * set relativenumber
And:
My Vim version is: 7.4.250
Here is my whole vimrc.
This situation happens in my both Win7 and Linux system.
Yes, I read this thread, and the "best answer" doesn't works either.
you got confused by the event name: CmdwinEnter/Leave, they would be triggered by enter/leaving command line window, not command line mode. :h cmdwin
I guess with your current setting, in command mode, if you press ctrl-f (entering cmd-win), you should see the line number change, and it would be applied on the command line window, you have to find the window id of your main editing, to change the setting.
To catch the "event" to entering command line mode, you could map normal mode : to a function, there, you could do preprocessing.
For catching the "event" of leaving cmd mode, you have to check the current mode all the time, if it is changed into normal.

Vim plugin : Rainbow parentheses using tab

I'm using vim 7.3 and Rainbow Parentheses plugin. When opening multiple tabs with vim -p file1 file2 or with vim -S session.vim, or even with tabnew file or any other method, my parenthesis are colored in only one file.
I just put this into my .vimrc : au VimEnter * RainbowParenthesesToggle
as said here. I tried to use :RainbowParenthesesToggle on the other tabs once opened but it only toggles in the parenthesis-activated tab.
What should I do to make things work in all tabs ?
I made it work by adding the same instructions as here in my .vimrc, thanks to FDinoff. I replaced the last instruction to make it work using tab, as I intended first.
function! Config_Rainbow()
call rainbow_parentheses#load(0)
call rainbow_parentheses#load(1)
call rainbow_parentheses#load(2)
endfunction
function! Load_Rainbow()
call rainbow_parentheses#activate()
endfunction
augroup TastetheRainbow
autocmd!
autocmd Syntax * call Config_Rainbow()
autocmd VimEnter,BufRead,BufWinEnter,BufNewFile * call Load_Rainbow()
augroup END
The VimEnter flag on the autocommand tells vim to perform the command specified (in this case RainbowParenthesesToggle only when starting the editor, which is in your case when you open the first file.
If you want to extend the functionality to everytime you load a buffer you should do something like:
autocmd BufRead,BufNewFile * RainbowParenthesesToggle

How to select what NERDTree window Vim focuses on depending whether a file is passed

I currently have the following in my .vimrc:
au VimEnter * NERDTree
au BufWinEnter * NERDTreeMirror
This I believe launches NERDTree when I open Vim, as well as when I open a new tab using a plugin. Currently, when I launch Vim using
vim
at the command line, the cursor is sitting in NERDTree ready for me to navigate my files, which is great. However, when I use
vim my-file.txt
the cursor remains in the NERDTree window. I'm aware that I can add this to my .vimrc:
au BufNew * wincmd l
But that means the cursor will always be placed in the window to the right of NERDTree, even if I don't specify a file.
Does anyone have any ideas?
You can make the command conditional on whether arguments were passed to Vim:
:au VimEnter * if argc() > 0 | wincmd l | endif
(I'd use the VimEnter event for that; just make sure this autocmd comes after the one that opens NERDTree.)
PS: There are a couple of related questions here:
Auto-open NERDTree in "EVERY" tab
How do you add NERDTree to your vimrc?

How to hide cursor line when focus in on other window in vim

In my vimrc file i have this option set cursorline. I want to hide this line if that window is not in focus . Is there an option in vim to do that?
See this
Essentially, it's just the following autocmds:
augroup CursorLine
au!
au VimEnter * setlocal cursorline
au WinEnter * setlocal cursorline
au BufWinEnter * setlocal cursorline
au WinLeave * setlocal nocursorline
augroup END
But occasionally, you may want to define exceptions (i.e. permanently on or off) for certain windows. That's where my CursorLineCurrentWindow plugin may be helpful.
It sounds like you want the cursorline on when entering a vim buffer and off when leaving it. These commands in the vimrc file will achieve this:
autocmd BufEnter * set cursorline
autocmd BufLeave * set nocursorline

Resources