I'm trying to achieve a consistent behavior when setting the tmux window name to the name of the current opened file in vim.
Here's what i have in my .vimrc
augroup tmux
autocmd!
if exists('$TMUX')
autocmd BufReadPost,FileReadPost,BufNewFile,FocusGained * call system("tmux rename-window " . expand("%:t"))
autocmd VimLeave,FocusLost * call system("tmux set-window-option automatic-rename")
endif
augroup END
I've been facing two situational problems:
Switching panes
This almost works. For jumping between panes, it renames every focus change but sometimes it fails and resets to default... I have no idea why.
Switching windows
Same behavior as switching panes, sometimes it resets the name of the window you just left to default
I need to improve this script to behave like so:
Switching panes renames current tmux window to filename (works poorly)
Switching away from a window persists it's name
Trunkate large filenames (is this even possible?)
I wonder if this has to do with my status bar refresh interval or some settings in my .tmux.conf so here it is
my_tmuxconf
Here are some gifs on the wierd behavior:
switching windows
switching panes
Update
This implementation gives better results:
if exists('$TMUX')
autocmd BufEnter,FocusGained * call system("tmux rename-window " . expand("%:t"))
autocmd VimLeave * call system("tmux rename-window zsh")
endif
But the drawback is that it can't rename when you switch from a pane running vim to a pane running something else eg, regular bash.
The missing piece is to make Tmux autorename on every pane switch, but only if the focused pane is not running vim. Can it be done?
Related
What I want to do:
Inside a file/buffer in vim, especially an unnamed one (for example, created with :enew), sometimes I feel the content I'm writing is worth saving (say, I suddenly want to send it via an email), but don't feel the particular need to save it to a temp file, nor do I trust myself enough to "remember" to save it upon exit.
So I thought, what if I run
autocmd BufLeave * ggvG"+y
in the vim command line once and be free from the fear of losing this content.
But it doesn't work. When I exit vim, the system clipboard reminds intact.
My questions:
does it do anything if we run autocmd on the fly, as opposed to in vimrc?
is there a way to tell vim to "hey, when you exit, run these"?
Thanks a ton!
Two problems:
1) You didn't leave the buffer to go to another buffer (BufLeave); you left Vim (VimLeave).
2) autocmd expects a command, not normal mode keystrokes.
With that in mind,
autocmd VimLeave * normal gg"+yG
I'm just starting to try MacVIM as a primary text editor after years of using vi only when I was sshing into a remote server. After installing the Janus set of extensions, I launch MacVIM from the Terminal with macvim . to get a listing of the current directory.
When launching MacVIM in this manner, by default I get two windows, a narrow NERDtree window and a buffer window taking up the rest of the space available and the focus being in the NERDtree window. I want to split the non-NERDtree window into either two equal parts or, failing that, create a new window at least 83 columns wide. From the default setup, I would enter <CTRL-W>l:vsplit, and that would do the job.
Of course, I don't want to do that every time, so how do I script it in my .gvimrc (or actually, with Janus, .gvimrc.local) file? I've tried a number of ways to do this all with no success. Attempts have included 80vsplit, and
<C-W>
l
vsplit
I use this in my .vimrc to move the cursor to the content panel when vim starts, you may want to put this in your list of commands to get to the right panel before splitting:
autocmd VimEnter * NERDTree "run nerdtree
autocmd VimEnter * wincmd p "cursor to right
Can I fix a buffer so that the only way to remove it from vim window is closing it?
For instance, I'm using the NERDtree plugin, which displays the filesystem in a vim window. Sometimes I forget to change focus to other window before using the quickfix commands and the erroneus file ends up replacing the file tree. (Not to mention that NERDtree's window default width is only 31)
Edit:
What I'm trying to achieve with this question is to simulate Eclipse's notion of views and editors inside vim. In this terms, NERDTree (and other plugins destined to exclusively display information) would be a view while the other windows would be editors.
This isn't exactly an answer, but if you screw up, Ctrl-^ undoes a change in a buffer's content.
I've written an autocommand that does this. Adding the following to vimrc will prevent NERDtree buffers from being overwritten:
autocmd BufEnter * if bufname("#") =~ "NERD_tree" && bufname("%") !~ "NERD_tree" | b# | endif
The autocommand runs every time a new buffer is opened -- if it detects that the previous buffer was NERDtree and the current buffer is not NERDtree, then it will go back to the NERDtree buffer, essentially 'locking' NERDtree in place.
Since mac os x's terminal.app does not support many of the vim visual aspects, including the cursor change from block to line when switching to insert mode, I use osascript to accopmlisch something similar.
In my .vimrc file I've written:
autocmd InsertEnter * silent !osascript -e 'tell application "Terminal" to set current settings of first window to settings set 11`j
autocmd InsertLeave * silent !osascript -e 'tell application "Terminal" to set current settings of first window to settings set 12`j
where settings set 11 is a set of terminal setting that has a line cursor and settings set 12 is one that has a block cursor.
This actually works quite well but there is one small problem.. On InsertLeave the cursor always gets moved one character to the left, which isn't such a big deal but it can be anoying.
I tried to compensate by putting autocmd InsertLeave h into my .vimrc, but to no avail (it gives me an error).
How should I tell vim to:
not shift to the left?
if the above isn't possible, to compensate by shifting to the right
Before answering the question, I'd recommend you to have a look in MacVim (if you haven't). If you would like (or need) to stick with terminal, maybe another terminal like iTerm will provide more functionality. Anyway, the cursor change between block - bar is not present in iTerm (at least I think so) and your way to solve it was phenomenal, it's bookmarked here now. Thanks!
An easy way to solve it would be adding another autocommand, like you said. But in yours, the pattern and the correct command to execute are missing.
The h is not a command. To execute a normal mode sequence, use the :normal command. This may work correctly:
au InsertLeave * normal! h
I'm used to my editors autosaving files when they lose focus. I recently switched to MacVim, and I can't recreate that behavior. I tried this:
autocmd BufLeave,FocusLost * wall
but if a buffer is unnamed when the window or MacVim loses focus, I get an error like the following:
Error detected while processing BufLeave Auto commands for "*":
E141: No file name for buffer 1
I'm confused, because :wall's documentation says:
Write all changed buffers. Buffers without a file
name or which are readonly are not written.
so I'd expect it to skip unnamed buffers. Is there a better way to do this?
Edit:
I'm pretty sure that the better way to do what I intended by using BufLeave -- saving buffers when I switch between them, so I don't get "No write since last change" -- is to set 'autowriteall'. The question still stands for when MacVim loses focus to other applications (FocusLost).
You don’t care about errors in those circumstances since there is nothing you can reasonably do about them anyway – especially when losing focus. So just swallow them:
autocmd BufLeave,FocusLost * silent! wall
Much simpler than an elaborate dance to figure out where there would be an error in order to avoid it.
I suspect when docs for wall say "without a file name" they may be referring to buffers with buftype=nofile . One way to get what you want would be to have the autocmd have bufdo call a simple function. E.g., some untested code to give the idea:
autocmd BufLeave,FocusLost * bufdo! call WriteFile()
function WriteFile()
if (&buftype=="") && (expand("%:r") > "") && (&readonly==0)
write
endif
endfunction
I think the standard way of getting something like this automatic saving of buffers would be to set the autosave option in Vim.
autocmd BufLeave,FocusLost * bufdo! call WriteFile()
function WriteFile()
if (&buftype=="") && (expand("%:r") > "") && (&readonly==0)
write
endif
endfunction
This method has a side-effect that, you can only open one unamed buffer. If you open the second one, it would automatically be synced to the content of the first unamed buffer.
just put this in .vimrc
set updatetime=1000
autocmd CursorHoldI * silent w