I've added a little quality-of-life improvement to the .vimrc which invokes netrw # startup, namely:
augroup ProjectDrawer
autocmd!
autocmd VimEnter * :Explore!
augroup END
Works like a charm, however this interferes with invoking vim to edit a particular file vim file_foo (I end up with netrw not with file_foo).
How can I modify my .vimrc to e.g. invoke ProjectDrawer when there were no arguments to the vim on invocation (vim), otherwise open provided files (vim file_foo)?
You can add a conditional that checks argc(), which gives the number of arguments passed to Vim (the arguments itself are returned by argv({nr})):
augroup ProjectDrawer
autocmd!
autocmd VimEnter * if argc() == 0 | Explore! | endif
augroup END
Related
I am trying to execute a yapf command to format my python file when I save it, so I created a function to call this command:
function Format_python_file()
silent :!yapf --style="{based_on_style: pep8, indent_width: 4}" -i %
silent :e %
endfunction
autocmd BufWritePost *.py call Format_python_file() <afile>
The problem is with your autocmd line, you have a trailing <afile> there.
In fact, the message I see is quite explicit about that:
Error detected while processing BufWritePost Autocommands for "*.py":
E488: Trailing characters: <afile>
You should just drop the <afile>, the function itself already works on the current buffer, doesn't need any argument or other reference to the current file.
Also note that it's a good practice to put your autocmds inside an augroup which gets cleared first. That way, if you reload your source file (vimrc or otherwise), it won't create duplicated autocmds.
The cleaner way to set up this autocmd would be:
augroup python_yapf
autocmd!
autocmd BufWritePost *.py call Format_python_file()
augroup END
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(!).
I have my vim set up to save files whenever I change buffers and on checktime. The problem is that I use Netrw and end up saving Netrw buffers. Can I run an autocommand on every type of file except netrw?
You can use an :if in your autocmd to guard against netrw files. e.g.
autocmd FileType * if &ft != 'netrw' | echo "do something" | endif
However this still isn't quite right. You have stopped from saving netrw buffers, but there are other buffers that shouldn't be saved. I would suggest checking 'buftype' and looking for files that start with a protocol e.g. foo://.
Here is an example of auto creating intermediary directories using such an approach:
" create parent directories
" https://stackoverflow.com/questions/4292733/vim-creating-parent-directories-on-save
function! s:MkNonExDir(file, buf)
if empty(getbufvar(a:buf, '&buftype')) && a:file!~#'\v^\w+\:\/'
let dir=fnamemodify(a:file, ':h')
if !isdirectory(dir)
call mkdir(dir, 'p')
endif
endif
endfunction
augroup BWCCreateDir
autocmd!
autocmd BufWritePre * :call s:MkNonExDir(expand('<afile>'), +expand('<abuf>'))
augroup END
I'd like a simple augroup that switches all buffers to absolute line numbers (for Ex commands) when I go into the command window
my current code is:
augroup cmdWin
autocmd!
autocmd CmdwinEnter * setglobal nornu
autocmd CmdwinLeave * setglobal rnu
augroup END
but this doesn't seem to work.
:setglobal won't work, because it just sets the future default, but doesn't update the values in existing windows. You need to apply this for all current windows, normally with :windo, but changing windows is a bad idea when the special command-line window is involved. Therefore, we toggle the option(s) "at a distance" via setwinvar() and a loop:
augroup cmdWin
autocmd!
autocmd CmdwinEnter * for i in range(1,winnr('$')) | call setwinvar(i, '&number', 1) | call setwinvar(i, '&relativenumber', 0) | endfor
autocmd CmdwinLeave * for i in range(1,winnr('$')) | call setwinvar(i, '&number', 0) | call setwinvar(i, '&relativenumber', 1) | endfor
augroup END
This toggles between nu + nornu and nonu + rnu; adapt the logic if you want to keep nu on and just toggle rnu.
I'd recommend to look at two vim plugins:
http://www.vim.org/scripts/script.php?script_id=4212
https://github.com/jeffkreeftmeijer/vim-numbertoggle
I put together the following pile of awesomeness:
if !exists("g:AsciidocAutoSave")
let g:AsciidocAutoSave = 0
endif
fun! AsciidocRefresh()
if g:AsciidocAutoSave == 1
execute 'write'
execute 'silent !asciidoc -b html5 -a icons "%"'
endif
endf
fun! AsciidocFocusLost()
augroup asciidocFocusLost
autocmd FocusLost <buffer> call AsciidocRefresh()
augroup END
endfun
augroup asciidocFileType
autocmd FileType asciidoc :call AsciidocFocusLost()
augroup END
The only problem is: It saves twice every time vim loses focus when in an asciidoc file.
When I put :autocmd asciidocFileType into the command line, it shows:
---Auto-Commands---
asciidocFileType FileType
asciidoc :call AsciidocFocusLost()
:call AsciidocFocusLost()
The same with :autocmd asciidocFocusLost and AsciidocRefresh().
Why this duplication?
I can't tell you for sure why you got those duplicates (multiple sourcing?), but the canonical way to prevent this is clearing the augroup first:
augroup asciidocFileType
autocmd!
autocmd FileType asciidoc :call AsciidocFocusLost()
augroup END
Since you only have one definition, you can also do this in one command:
augroup asciidocFileType
autocmd! FileType asciidoc :call AsciidocFocusLost()
augroup END
#ingo-karkat's answer is great, but I think I know why you get twice as many autocmds and how to prevent it naturally:
Some distributions of vim / Neovim make sure by default that filetype plugin indent is on. Test it for neovim with env XDG_CONFIG_HOME=/dev/null nvim -c 'filetype' or for vim with env HOME=/dev/null vim -c filetype
If the output is
filetype detection:ON plugin:ON indent:ON
Then You don't need to add filetype * on to your vimrc.