How to make vim close pdf opened in evince? - vim

I've added the following line to my vimrc which makes vim run latexmk on tex file I open in latex:
au BufWinEnter *.tex :Latexmk
(:Latexmk comes from the Latex Box vim plugin)
How can I add something similar that will close the pdf file and kill latexmk after I close vim? Thanks!

:au BufDelete <buffer> :!killall evince
Would work if you only use evince for preview purposes. Also, it may kill other stuff named evince. This actually kills it when you close the file. Use
:au VimLeave :!killall evince
if you want to close the apps with vim.

Related

Open file at Vim startup

I like to have Vim open a cheatsheet at startup. Adding this to _vimrc works nicely with MacVim:
:e *path-to-cheatsheet*
but on Windows, vim displays a message box at startup:
"~\Dropbox\vimfiles\myvim\vimcheat.txt"
"~\Dropbox\vimfiles\myvim\vimcheat.txt" [unix] 52L, 1735C
When the message box is dismissed, Vim completes startup and opens the cheatsheet. How do I get this to work cleanly with Windows Vim?
The ~/.vimrc is processed at the very beginning of :help initialization. You can use the VimEnter event to open the file after all the startup stuff is done:
autocmd VimEnter * edit path/to/cheatsheet
If you don't want to see the messages, use silent edit. If you don't care about errors (if the cheatsheet isn't there), use silent!.

Golang: running `go build` on save using vim (macvim) and vim-go

I am recently moving from sublime 3 go to mvim (vim on the mac os) and am trying to get my Golang development environment to be as similar on vim to my sublime implementation as possible.
Within my sublime setup, it runs go build anytime I save the a Go file. This provides me instant feedback on if I have unused vars or other info go build provides.
I'm attempting to move to vim, and am wondering if I can have this functionality implemented there as well. I am using vim-go but have not found a setting to implement this.
In short I want to run :GoBuild upon the saving of a Go file while using vim / vim-go. Is this Possible and how do I do so?
yes, use vim autocommand to run :GoBuild:
You can specify commands to be executed automatically when reading or
writing a file, when entering or leaving a buffer or window, and when
exiting Vim. The usual place to put autocommands is in your .vimrc or
.exrc file.
Run the following command:
:autocmd BufWritePre *.go :GoBuild
Now each time you save your Go file with :w it will run :GoBuild too.
The event type is BufWritePre, which means the event will be checked just before you write *.go file.
BufWritePre starting to write the whole buffer to a file
BufWritePost after writing the whole buffer to a file
and:
When your .vimrc file is sourced twice, the autocommands will appear
twice. To avoid this, put this command in your .vimrc file, before
defining autocommands:
:autocmd! " Remove ALL autocommands for the current group.
If you don't want to remove all autocommands, you can instead use a
variable to ensure that Vim includes the autocommands only once:
:if !exists("autocommands_loaded")
: let autocommands_loaded = 1
: au ...
:endif
like this (put this at the end of your vim startup file):
:if !exists("autocommands_loaded")
: let autocommands_loaded = 1
: autocmd BufWritePost *.go :GoBuild
:endif
ref:
http://vimdoc.sourceforge.net/htmldoc/autocmd.html
http://learnvimscriptthehardway.stevelosh.com/chapters/12.html
Create ~/.vim/after/ftplugin/go.vim with:
autocmd BufWritePre *.go :GoBuild

Folding code in vim confuse me

I have a question about code folding in Vim. Let's say I open a file and fold some code, then I save it and quit. Later I open the same file, and my folds are gone. Any idea what's going on?
You can use :mkview to save folds and such when you close a file - but you have to use :loadview next time you use the file.
Further, you can automate this with .vimrc file. Add following to your vimrc.
autocmd BufWinLeave *.* mkview
autocmd BufWinEnter *.* silent loadview

Stop NERDTree opening when vim starts

I have NERDTree plugin installed but it is always present when I start vim like this:
vim .
I only want it to open on demand.
How can I stop it opening whenever I start vim?
Try adding
let g:NERDTreeHijackNetrw=0
to your vimrc and make sure there is no other line that sets this variable.
$ vim . always opens a file explorer. If you don't have NERDTree or NERDTree is configured as per ZyX's answer you'll get netrw by default anyway.
I wonder what would happen if the netrw plugin was somehow removed.
In your vimrc file, do below steps
comment autocmd VimEnter * NERDTree, which stops opening nerdTree
by default
paste map <C-n> :NERDTreeToggle<CR> in vimrc, this will open
NerdTree only when 'Ctrl+n' is pressed from keyboard

How to open pdf files under cursor (using 'gf') with external PDF readers in vim

The current gf command will open *.pdf files as ascii text. I want the pdf file opened with external tools (like okular, foxitreader, etc.). I tried to use autocmd to achieve it like this:
au BufReadCmd *.pdf silent !FoxitReader % & "open file under cursor with FoxitReader
au BufEnter *.pdf <Ctrl-O> "since we do not really open the file, go back to the previous buffer
However, the second autocmd failed to work as expected. I could not figure out a way to execute <Ctrl-o> command in a autocmd way.
Could anyone give me a hint on how to <Ctrl-O> in autocmd, or just directly suggest a better way to open pdf files with gf?
Thanks.
That's because what follows an autocmd is an ex command (the ones beginning
with a colon). To simulate the execution of a normal mode command, use the
:normal command. The problem is that you can't pass a <C-O> (and not
<Ctrl-O>) directly to :normal, it will be taken as literal characters (<,
then C, then r) which is not a very meaningful normal command. You have two
options:
1.Insert a literal ^O Character
Use controlvcontrolo to get one:
au BufEnter *.pdf normal! ^O
2.Use :execute to Build Your Command
This way you can get a more readable result with the escaped sequence:
au BufEnter *.pdf exe "normal! \<c-o>"
Anyway, this is not the most appropriate command. <C-O> just jumps to the
previous location in the jump list, so your buffer remains opened. I would do
something like:
au BufEnter *.pdf bdelete
Instead. Still I have another solution for you.
Create another command with a map, say gO. Then use your PDF reader
directly, or a utility like open if you're in MacOS X or Darwin (not sure if
other Unix systems have it, and how it's called). It's just like double clicking
the icon of the file passed as argument, so it will open your default PDF reader
or any other application configured to open any file by default, like images or
so.
:nnoremap gO :!open <cfile><CR>
This <cfile> will be expanded to the file under the cursor. So if you want to
open the file in Vim, use gf. If you want to open it with the default
application, use gO.
If you don't have this command or prefer a PDF-only solution, create a map to
your preferred command:
:nnoremap gO :!FoxitReader <cfile> &<CR>
If the default app is acceptable, then simply using :!open % in command mode works. You can always map this to a suitable leader combination in your vim config file etc.
If you want something that works with normal mode, then you could try something like the following (i use this too for opening HTML files), and modify to your own needs:
if has('win32') || has ('win64')
autocmd FileType html nmap <Leader>g :silent ! start firefox "%"<cr>
elseif has('mac')
autocmd FileType html nmap <Leader>g :!open "%"<cr><cr>
endif

Resources