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.
Related
Let's say I have the following in my .vimrc:
au bufenter * RainbowParenthesesToggle
However I'm on a unfamiliar machine and I haven't installed all of my plugins yet. This means when I start up Vim I get this error message:
E492: Not an editor command: RainbowParenthesesToggle
How can I guard against this, or what if statement do I want to wrap these calls in to avoid getting this error message when I start Vim?
suppress
The easiest would be to just suppress the error message via :silent! (note the !):
:au bufenter * silent! RainbowParenthesesToggle
check each time
It's cleaner (especially for an autocmd that runs on each BufEnter) to avoid the call. The existence of a command can be checked with exists(':RainbowParenthesesToggle') == 2.
:au bufenter * if exists(':RainbowParenthesesToggle') == 2 | RainbowParenthesesToggle | endif
avoid definition
It would be best to check only once, and avoid defining the autocmd at all. The problem is that your ~/.vimrc is sourced before the plugins! There are two ways around this:
1) Explicitly source the plugin before the check:
runtime! plugin/rainbowparentheses.vim
if exists(':RainbowParenthesesToggle') == 2
au bufenter * RainbowParenthesesToggle
endif
2) Move the definition and conditional to a location that is sourced after the plugins. ~/.vim/after/plugin/rainbowparentheses.vim would be a good place for this.
You can check for the command using exists():
au bufenter * if exists(":RainbowParenthesesToggle") | RainbowParenthesesToggle | endif
(I have no such command defined myself, so I can verify that this works. :) )
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)
The answer to the question
Using vim Sessions Only With GUI? suggests using
au VimLeave * mksession! ~/.gvimsession
au VimEnter * source ~/.gvimsession
My problem is when I start Vim, say, by issuing $ gvim test.html, the test.html file is loaded into a buffer that is not shown.
How can I test if arguments where passed and not execute the au VimEnter in such a case?
Or, alternatively, how can I switch to the buffer holding the file with the given file name.
One can test whether there are any command-line arguments (using the
argc() function) and load the previously saved session only when
there are not any:
:autocmd VimEnter * if argc() == 0 | source ~/.gvimsession | endif
I usually open many files in tabs with vim -p. Is it possible to check if any of the files was changed outside of Vim since editing started?
Add these lines to your .vimrc:
au FocusGained,BufEnter * :silent! checktime
au FocusLost,WinLeave * :silent! w
Basically, check for and reload (or discard) external changes when Vim or the current buffer gains focus, and optionally, auto-save when leaving focus.
Source: Vim Wiki.
I came across an interesting find related to this question today...
Hidden in /usr/share/vim/vim71/vimrc_example.vim there is this command:
" Convenient command to see the difference between the current buffer and the
" file it was loaded from, thus the changes you made.
command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
\ | wincmd p | diffthis
It will open a vimdiff-like window with the current buffer and the underlying file highlighting all of the changes between the two.
vim usually warns me automatically if it detects an external change to a file; however, from perusing the documentation it looks like you can invoke that check manually with :checktime
Unfortunately I don't know how to disable that aforementioned automatic check to test and see if checktime does the right thing, so this answer might be completely off-base.
Use :edit
:help :edit for more info.
You can find out if the buffer in the active window is modified by running the command:
:set mod?
If it returns nomodified, then the contents of the buffer match those of the corresponding file. If it returns modified, then the buffer has unsaved changes.
By default, the status-line shows a [+] symbol if the current buffer has been modified. The status line is generally only visible if you have split windows. If you want to show the status line, even when you have just a single window, run:
:set laststatus=2
There's a good article about customizing your status line on Vim Recipes.
let s:pid=system("ps -p $$ -o ppid=")+0
if !exists('g:watches')
let g:watches={}
else
finish
endif
function! ModWatch(fname, match)
let fname=fnamemodify(a:fname, ':p')
if has_key(g:watches, fname)
return
endif
let shellscript=
\"while true ; do".
\" inotifywait ".shellescape(fname)." ; ".
\" kill -WINCH ".s:pid." ; ".
\"done"
echo shellscript
echo shellescape(shellscript)
let pid=system("sh -c ".shellescape(shellscript)." &>/dev/null & echo $!")+0
call extend(g:watches, { fname : pid })
augroup ModWatch
execute "autocmd! BufWipeOut ".a:match
execute "autocmd BufWipeOut ".a:match.' call DeleteWatch("'.
\escape(fname, '\"|').'")'
augroup END
endfunction
function! DeleteWatch(fname)
call system("kill ".g:watches[a:fname])
unlet g:watches[a:fname]
endfunction
augroup ModWatch
autocmd!
autocmd VimResized * checktime
autocmd BufNew * call ModWatch(expand("<afile>"), expand("<amatch>"))
augroup END
How do I add NERDTree to my .vimrc?
Okay, the previous version was a bit terse, but the answer you're looking for is to add the line below into your ~/.vimrc file. It tells Vim that you want to setup a command to run when Vim starts, but since it depends on various plugins to be loaded, you don't want to run it until all initialization is finished:
autocmd VimEnter * NERDTree
If, however, you're annoyed by the fact that the cursor always starts in the NERDTree window, you can add a second autocommand that will move the cursor into the main window:
autocmd VimEnter * NERDTree
autocmd VimEnter * wincmd p
I like to see NERDTree only when I start vim without file arguments, so I added this to my .vimrc:
autocmd VimEnter * if !argc() | NERDTree | endif
Are you on a Windows or unix-y system?
If you're on a unix-y system you put plugins in ~/.vim/plugin. Here's what my plugin directory looks like:
$ ls ~/.vim/plugin
NERD_tree.vim scratch.vim scratchfind.vim
After that it starts working right away. Try running vim like this:
$ vim .
It should open the current directory in the NERD tree view.
If you're on Windows you put plugins here: C:\Program Files\Vim\vim70\plugin
To get NERDTree to load automatically when you start up vim, run it like this from the command line:
$ vim -c "NERDTree" some_file.txt
You can set an alias for this in your .bashrc:
alias vimt='vim -c "NERDTree" $1'
Now whenever you run vimt (instead of vim) you'll also open up NERDTree on the left side of the window.
You could also add a shortcut key to start NERDTree in your .vimrc this way:
function OpenNERDTree()
execute ":NERDTree"
endfunction
command -nargs=0 OpenNERDTree :call OpenNERDTree()
nmap <ESC>t :OpenNERDTree<CR>
Now when you hit Esc then t it will pop open NERDTree.
Per the NERDTree instructions you can just use pathogen.vim. Install it with:
mkdir -p ~/.vim/autoload ~/.vim/bundle; \
curl -Sso ~/.vim/autoload/pathogen.vim \
https://raw.github.com/tpope/vim-pathogen/master/autoload/pathogen.vim
Add this to your .vimrc:
execute pathogen#infect()
then install NERDTree:
cd ~/.vim/bundle
git clone https://github.com/scrooloose/nerdtree.git
And if you want to open a NERDTree automatically when Vim starts up, add the following to your .vimrc:
autocmd vimenter * NERDTree
The answers here have a minor problem.
If you call vim --noplugin or use a script that uses --noplugin mode such as vimpager, it will cause this error:
Error detected while processing VimEnter Auto commands for "*":
E492: Not an editor command: NERDTree
To avoid this, put the command in ~/.vim/after/plugin/NERD_tree.vim instead:
autocmd VimEnter * NERDTree
And it might also be a good idea to test that NERDtree is available as well, i.e.:
if exists("loaded_nerd_tree")
autocmd VimEnter * NERDTree
endif
" NERD Tree
nmap <silent> <special> <F2> :NERDTreeToggle<RETURN>
I like to open NERDTree on startup, but with two requirements:
Open NERDTree only when there are no arguments (file arguments)
Display only the NERDTree window (I don't want to display the main window)
I use this command:
autocmd VimEnter * if !argc() | NERDTree | wincmd p | q | endif
Update (9 Jan 2022)
I've found a more performant way to meet the two requirements I've specified above.
Remove the autocmd, which I've mentioned above, from the .vimrc file. Instead, create an alias like this:
vim() {
if [ $# -eq 0 ]; then
/usr/bin/vim ./
else
/usr/bin/vim "$#"
fi
}