I'm programming in vim and I'm having one little problem. My vim is multiple times splitted in both vertical and horizontal directions, and I want to activate word-wrap like set wrap when i switch from one window to another with ctrl-w/W, also i want to unwrap like set nowrap on focused one. I have no idea how to do this and I was never good at writing scripts for vim. Any ideas ?
I'm not sure of what you want to archieve, but maybe you could add
autocmd WinEnter * set nowrap
autocmd WinLeave * set wrap
to your .vimrc. This will trigger a set nowrap on a window you enter and a set wrap on a window you leave. From the tests I ran, WinEnter and WinLeave are triggered when you switch between several split windows.
Alternatively, you can replace the * by a *.txt if you just want these command to be issued only on buffers containing a txt file for instance.
Related
I am having trouble on making the Vim Vertical Split Highlight disappear permanently.
When I use this command in Vim command line :hi VertSplit ctermbg=None the vertical split highlight disappears (as seen in Screen 'B'), but when I use the same command in the .vimrc.after (I am using janus for vim, as seen in screen 'A') the command does not work. I have tried many variations of the command with 'let','set', etc but no result, the Vertical Split stays.
Can someone please help and let me know what I doing wrong or what I am suppose to do? I want to remove the VerticalSplit highlight from my Vim.
Your .vimrc is executed sequentially, so probably this command is
being executed but then having its changes overwritten by another
command or plugin of yours. The solution would then be to move the
command to after these other changes happen. This may and may not work
depending on your setup (check :h startup for a list of what happens
at startup and in what order).
The best solution in my opinion is to use an auto command. These will
execute a command after an event, for example startup finished or
colorscheme changed. Add this to your .vimrc:
autocmd! ColorScheme * hi VertSplit ctermbg=none
Now, whenever the colorscheme is changed, this command will trigger for
any file (*). This will trigger as well by your initial colorscheme
set in your .vimrc.
Important! If your colorscheme set in your .vimrc is done before
your autocmd line, then of course nothing will be triggered (it won't
be capturing the event yet). In this case you have two options: first is
to obviously move the colorscheme set to after the autocmd. Second
option is add an additional event listener by changing the line to:
autocmd! VimEnter,ColorScheme * hi VertSplit ctermbg=none
Now this will be triggered both after Vim's startup process (after all
.vimrc stuff is done and loaded) and also whenever you change your
colorscheme. This way, you don't need to change your colorscheme files.
And you should never have to do that anyway. The point of .vimrc is to
keep all your configuration in one file. So this editor of course
provides options to make this happen.
I've been trying to figure this out for so long but still failed, some of my customized key-mapping (majority of them are for plugins) behaves totally different between gui-mode and cli-mode, even if I use gui-mode command with -v flag (like mvim -v) or using :gui command in terminal Vi.
For example, the Emmet plugin which is handy for expanding HTML/CSS expressions, the default trigger key <C-y> never works at first (in Cli-mode which I often used to), I always don't know why, until one day I use MacVim for a while and suddenly find out it works!
After that, I try to re-map the default trigger from <C-y> to <C-e> or <C-k>, both of them works fine in MacVim but still unlucky in terminal Vim.
Is there any particular reason to cause this issue? Maybe something wrong with my configuration?
Any suggestion will be appreciated, thanks!
Finally I solved it by myself, but thank for #ebenezer 's reminding.
We often use set timeoutlen and set ttimeoutlen to tweak the latency for a key code or a mapping sequence to complete. Most of us can't tolerate with the default value for ttimeoutlen (which is -1), because it forces us to waiting for so long when quit from Insert mode by press ESC or Ctrl-[.
For this particular reason, I changed this value to 10 (in ms), but I copy this setting from somewhere I don't remember now, and it place set ttimeoutlen in a autocmd for all filetypes, like below:
if ! has('gui_running')
set ttimeoutlen=10
autocmd InsertEnter * set timeoutlen=0
autocmd InsertLeave * set timeoutlen=1000
endif
I don't know why this will make some plugins not work properly, and I change it a little to make it works for me now:
if ! has('gui_running')
autocmd InsertEnter * set ttimeoutlen=100
autocmd InsertLeave * set ttimeoutlen=-1
endif
Hope this will help you if you encounter same problems.
How to make vim automatically save only current buffer when focus is lost, not all files as described here http://vim.wikia.com/wiki/Auto_save_files_when_focus_is_lost
Based on your comments I can say that you have wrong question: you want not to “save only current buffer when focus is lost” (focus tends to have a meaning of “currently focused window”), but to “write buffer when switching to another one: when it is no longer in your focus of attention”. To complete this you may use
augroup AutoWrite
autocmd! BufLeave * :update
augroup END
, maybe combined with
set autowrite
and
set autowriteall
(sets of situations where first and second two variants are triggered intersect, but neither is a superset of another one).
Instead of :wa (write all), use :w.
Also you can try this
set updatetime=1000
autocmd CursorHoldI * silent w
just put it in your .vimrc
In my .vimrc I've put set foldmethod=syntax to enable folding of methods etc. However, I don't like the default that everytime I open a file, the whole thing is folded. Is there a way to enable foldmethod, yet have files unfolded when I open them?
set foldlevel=99
should open all folds, regardless of method used for folding. With foldlevel=0 all folded, foldlevel=1 only somes, ... higher numbers will close fewer folds.
You can put this in your .vimrc:
au BufRead * normal zR
It declares an automatic command (au), triggered when a buffer is read (BufRead), matching all files (*) and executes the zR (opens all folds) command in normal mode.
set nofoldenable
Adding this to your .vimrc will temporarily disable folding when you open the file, but folds can still be restored with zc
In .vimrc add an autocmd for BufWinEnter to open all folds automatically like this:
autocmd BufWinEnter * silent! :%foldopen!
That tell vim to execute the silent :%foldopen! after opening BunWinEnter event (see :h BufWinEnter). The silent %foldopen! will execute foldopen on the whole buffer thanks to the % and will open all folds recursively because of the !. Any eventual error message will be suppressed by silent. (You could get error messages like E490: No fold found if the buffer actually didn't contain any fold yet)
Note: You could use BufRead instead of BufWinEnter but then if the file has a modeline that enables the folding that will override this autocmd. I mean BufRead autocmds run before the modeline is processed and BufWinEnter will run them after. I find the later to be more useful
You can add
set foldlevelstart=99
to your .vimrc file, and it will start editing any new file with all folds open.
If you want a way to have it display unfolded as soon as it is opened, you can use set foldlevelstart=99 as a lot of answers explained.
But, if you just want to see them unfolded, you can just press zi and it will unfold everything. Another, zi will close them back.
You could map it to keys to enable it.
For example,
nmap ,f :set foldmethod=syntax<CR>
Then while in normal mode hit the ",f" key combination
You can open unfolded file when you put set nofoldenable into your .vimrc file.
autocmd BufReadPost * silent! :%foldopen!
This worked best for me. After a buffer gets opened all folds are opened. This opens them to the correct level.
The set foldenable method was not good, because if I choose to close one fold level, it enabled folding again, and folded every thing to 0 level, instead of just going down one level on the one I activated.
I frequently make mode errors while using vim, i.e. I'll start typing text while in Normal mode, or start typing commands while in Insert mode. I understand this goes away with time as vim's quirks seep into your bones, but are there any ways to speed the process?
I use these autocmd's to highlight the entire line containing the cursor while in insert mode, and not while in normal mode:
if v:version >= 700
autocmd InsertEnter * set cursorline
autocmd InsertLeave * set nocursorline
endif
This provides a little bit more visual feedback about the mode.
If you haven't done so already you can display the current mode by using :set showmode. That'll display -- INSERT -- in the status bar when in insert mode.
Try to remember to always leave vim in normal mode.
Switch "Esc" and "Caps Lock" keys.
If you accidentally click on "Caps Lock" you will start inputing commands that has nothing to do with what you like to do. It is annoying if you are an experienced user; if you are a beginner it may be a hassle to understand what went wrong.
Every time you need to press the Esc key you have to move you entire hand and to get your pinky finger to touch the Esc key and then replace the entire hand again.
Some Vim users will tell you that after a while you get used to doing that and it is not a big deal. I think that argument falls short because you can pretty much get used to mapping any key any where. It is a matter of efficiency.
I believe "Esc" is used very frequently and "Caps Lock" is used seldomly if it is used at all.
So switching the two makes sense as it prevents errors and increases typing speed.
With gvim, the cursor changes from a block to a vertical bar when going between modes. This at least gives you a little visual feedback.
Insert mode should only be temporary. Normal mode is, as its name tells, the favourite mode for edition tasks.
Usually, you should spend more time in normal mode, and always hit ESC when you are done inserting something.
Maybe I speak only for myself, but now I have the habit of assuming that I am in normal mode at all times, and I am almost never wrong.
Here is my variant of Ned's Answer. It toggles on window switches (window focus is another modal behavior that provides little visual feedback).
if v:version >= 700
set cursorline cursorcolumn
au WinLeave * set nocursorline nocursorcolumn
au WinEnter * set cursorline cursorcolumn
au InsertEnter * set nocursorline nocursorcolumn
au InsertLeave * set cursorline cursorcolumn
endif
I use it with the zenburn color scheme, and I also turn off cursor blink:
if has("gui_running")
colorscheme zenburn
set guicursor+=a:blinkon0
endif