Is it possible to execute a shell command when I suspend and resume vim? I guess that I can do something once <C-z> is pressed, so this can be considered us a suspend hook. But what about resume (with fg)? And also is it possible to do some work on vim startup and shutdown?
Maybe it's possible to establish handlers for vim's SIGSTOP and SIGCONT signals?
As the help on autocommand-events (suggested by #romainl) says, you can use VimEnter and VimLeave to execute a command when Vim launches or shuts down:
autocmd VimEnter * call EnterFunc()
autocmd VimLeave * call LeaveFunc()
The above code will execute a function EnterFunc when launching Vim, and LeaveFunc when exiting Vim.
To execute a function when vim is suspended or resumed, you can use VimSuspend or VimResume:
autocmd VimSuspend * call SuspendFunc()
autocmd VimResume * call ResumeFunc()
However, you should know that VimSuspend and VimResume are available only from Vim 8.2. If you're using Ubuntu you can add this PPA:
sudo add-apt-repository ppa:jonathonf/vim
and then:
sudo apt update
sudo apt install vim
to update vim to a version that supports VimSuspend and VimResume.
This is outlined here
Related
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!.
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
I configured the following statement in vimrc file:
autocmd VimLeave * mksession! ./vimsession
Is there some means to make vim stated as vim -S vimsession? So when I entered vi, and it executed as vi -S vimsession in background?
To automatically restore a persisted session, put this into your ~/.vimrc:
if filereadable('./vimsession')
source ./vimsession
endif
It's probably best to delay this until all plugins have been loaded:
if filereadable('./vimsession')
autocmd VimEnter * source ./vimsession
endif
I'm not sure I understand your question. It sounds like you want to distinguish whether Vim was started with or without a session.
If you load/save a session, v:this_session will hold the path to the session file.
:help v:this_session
So if I understood your question correctly, you want something like:
autocmd VimLeave *
\ if exists('v:this_session') && filewritable(v:this_session) |
\ execute 'mksession!' fnameescape(v:this_session) |
\ endif
On closing Vim it checks if there is a running session and updates it before exiting.
I have a .vimrc script that automatically creates a buffer for a bash terminal with Conque (and goes to insert mode) and then returns to the previous buffer (the file I have opened).
autocmd VimEnter * ConqueTermSplit bash
autocmd VimEnter * wincmd p
The problem is that when I start vim I am left in insert mode and I have to press <Esc> every time to go to normal mode.
Writing <C-v><Esc> at the end of .vimrc doesn't work, as the command is executed in command mode.
I don't have that plugin
autocmd VimEnter * exec "ConqueTermSplit bash" | silent norm!
could work
Update Just found out that Conque's documentation rocks
You can use the conque_term#open({command}, [buf_opts], [remain]) function to achieve what you want:
If you don't want the new terminal buffer to become the new active buffer, set
[remain] to 1. Only works if you create a split screen using [options].
So what you'd want is roughly
autocmd VimEnter * call conque_term#open('/bin/bash', ['split', 'resize 20'], 1)
When I open Vim from a terminal, copy some text to the system clipboard, and exit Vim, the system clipboard gets cleared.
How to keep the copied text in the clipboard?
Synthesizing answers from superuser, just add the following to your .vimrc
autocmd VimLeave * call system("xsel -ib", getreg('+'))
Install Parcellite, or glipper for Gnome and klipper for KDE.
Restart your computer or run it manually.
see: https://wiki.ubuntu.com/ClipboardPersistence
Based on Matt's answer, the following uses xclip instead of xsel:
autocmd VimLeave * call system('echo ' . shellescape(getreg('+')) .
\ ' | xclip -selection clipboard')
I ran into this issue and a related problem where suspending vim with ctrl-z would also clear the clipboard. I've extended Matt's solution to fix both:
set clipboard=unnamedplus
if executable("xsel")
function! PreserveClipboard()
call system("xsel -ib", getreg('+'))
endfunction
function! PreserveClipboadAndSuspend()
call PreserveClipboard()
suspend
endfunction
autocmd VimLeave * call PreserveClipboard()
nnoremap <silent> <c-z> :call PreserveClipboadAndSuspend()<cr>
vnoremap <silent> <c-z> :<c-u>call PreserveClipboadAndSuspend()<cr>
endif
The if executable("xsel") guard is there to avoid errors if xsel is not installed. The nnoremap mapping preserves the clipboard when suspending from normal mode and the vnoremap mapping handles suspending from visual or select modes.
I've confirmed this works on vim 7.4 and 8.0.
Use NeoVim. It by default doesn't clear the clipboard on exit. You will still need to set clipboard=unnamedplus (typically in ~/.config/nvim/init.vim) and have xsel or xclip tools installed.
Keep in mind that some other defaults are different as well.
Please correct me if I'm wrong but from my understandings of Vim...
1) Vim uses registers instead of the clipboard to store copied/cut data.
2) These registers are preserved when exiting vim in a status file but are not accessible outside of the running process unless you manually open the file and inspect its contents
3) Saving stuff to the + registre while Vim runs allows you to paste to other applications.
4) By suspending vim (CTRL-Z) instead of closing it, these registers are still accessible.
Does that provide assistance?
Based on Matt's answer
When using his method copying multiple lines added slashes to the end of lines when pasting.
This should remedy that.
autocmd VimLeave * exe ":!echo " . shellescape(getreg('+')) . " | xclip -selection clipboard"
When i used "shellescape" with "system" newlines kept getting escaped. But that didn't happen when i used exe.
Don't know the real reason. but this worked.