Questions tagged [autocmd]

`autocmd`s are commands executed automatically on certain events. Questions on creating or modifying `autocmd` definitions are appropriate for this tag.

Filter by
Sorted by
Tagged with
0 votes
2 answers
28 views

Misunderstanding of interaction between local options, moving between windows, and WinLeave, WinEnter, BufLeave, BufEnter events

This what is logged in :messages when I'm printing the value of nu and rnu upon the 4 events WinLeave, WinEnter, BufLeave, BufEnter during the action, done twice, of opening :help and closing it. The ...
0 votes
0 answers
30 views

How to use variables inside autocmd?

I have defined the following augroup: vim9script augroup MY_AUGROUP autocmd! autocmd CursorMoved,CursorHold <buffer> \ if index(b:line_numbers, line('.')) != -...
29 votes
3 answers
9k views

What is the order of WinEnter, BufEnter, BufRead, Syntax, FileType events?

I've gone through the autocmd.txt help, but the information about this is scattered and not always present (eg. with BufRead). Could someone who understands this well list the order of these events -...
0 votes
1 answer
26 views

How to define a User autocmd event based on a command being executed?

I am struggling in using doautocmd to mimic a user-defined event. The event I am trying to mimic is "After :MyCommand has been executed" I am trying to achieve that goal with the following ...
0 votes
1 answer
19 views

Specifying filetype in ModeChanged event autocmd

I'm using stylua as the code formatter for lua files. I do not usually get used to its formatting style but sometimes I do need a little help with it, so I decided to make it the formatprg during the ...
0 votes
1 answer
133 views

'autocmd OptionSet' - How to make it work?

The following .vimrc snippet is by Martin Tournoij's answer posted here: How do I stop the colorcolumn from breaking into multiple pieces when there are wrapped lines? augroup wrappedcc autocmd! ...
0 votes
0 answers
21 views

How to override neovim paste implementation?

I wanted to add some handler that is triggered whenever a paste happened. From what I know, there is no such a thing like autocmd for paste action. I have look into some possible solution such as ...
0 votes
2 answers
44 views

filetype specific autocommand vs after/ftplugin in lua configuration?

Is there any reason advantage (esp using lua) to put filetype specific settings in an autocmd vs putting in after/ftplugin?
0 votes
0 answers
13 views

autocmd to set hls after having manually :nohls doesn't turn search highlighing on

tl;dr Why, after sourceing the following, moving the cursor doesn't result in the text matching the / being highlighted? set hls augroup AU_NAME autocmd! autocmd CursorMoved * set hls augroup END ...
1 vote
1 answer
90 views

Unable to reset settings after running command that triggers bash script

I have the following autocommand in my vimrc for vimwiki: autocmd BufWritePost *.md silent! VimwikiAll2HTML The VimwikiAll2HTML triggers a custom script with :! scriptname to process all the files in ...
0 votes
2 answers
33 views

Capturing a group in a pattern used in a Vim autocmd

I need Vim to ignore the .tmpl file extension to enable syntax highlighting. How can I rewrite this: autocmd BufNewFile,BufRead *.lua.tmpl set filetype=lua to be more general? I want to match the lua ...
1 vote
1 answer
46 views

How do I automatically open/close location or quickfix lists when they are populated by ALE?

I'm switching from the syntastic plugin to the ALE plugin. In syntastic there was an option g:syntastic_auto_loc_list that would automatically open the location list after it had been populated. If ...
0 votes
0 answers
20 views

BufEnter events triggered with a mouse click in gvim: line('.') appear to return a wrong value. How to fix it?

Consider the following auto-group augroup Test au! autocmd BufEnter * :echo line('.') augroup END and open two different buffers in split windows. When using commands such as wincmd p, then ...
6 votes
3 answers
1k views

How do I trim whitespace automatically?

I've been doing this: au BufRead,BufWrite * if ! &bin | silent! %s/\s\+$//ge | endif The problem is that it moves the cursor sometimes when I save the file.
1 vote
2 answers
649 views

Delete all marks except bookmarks in nvim

I use dashboard so I like my marks clean. So I generally make many marks while working on something and then delete them all except the file bookmark in the end. Is there any way to delete all marks ...
1 vote
0 answers
33 views

How to manually implement autochdir?

'autochdir' 'acd' boolean (default off) global {not in Vi} {only available when compiled with it, use exists("+autochdir") to check} ...
2 votes
1 answer
167 views

Dynamically toggle formatoptions based on input

Question: Is there a way to control the indentation + comment leader input, when adding a new line? Details: I am trying to set formatoptions-=cro when inserting a new line (in specific syntax region)...
1 vote
1 answer
89 views

Prevent vim from exiting in case of error during autocommand

I'd like to save my session before vim exits. Here's what I have so far: function SaveSession() let l:new_session = trim(inputdialog("Enter new session name: ", v:this_session, -1)) ...
0 votes
2 answers
106 views

How to close Terminal and NERDTree windows if they are the last windows?

If I close all buffers, I'm left with the terminal window, which automatically opens whenever I open Vim. I would like it close automatically when I exit Vim. I see that on the NERDTree repo, they ...
0 votes
1 answer
39 views

Vim: mapping tab to ctrl space only if the cursor is in front of a non whitespace character

I am using the jedi-vim extension and really don't like pressing ctrl-space to bring up auto-complete. I would like to use tab, but also obviously when coding I need tab for indentation as well. I ...
0 votes
1 answer
32 views

How to map `<LocalLeader>s` to send send the selected text to `SlimeRegionSend`?

For python development, I use slime with tmux and the following configuration let g:slime_target = "tmux" " configuration for vim in a split tmux window with a REPL in the other pane: ...
0 votes
1 answer
48 views

How to extract filename of :w command in vim's autocmd?

When I use Vim and type in the :w command, sometimes I will tap ] or \ by accident before I tap the Enter key because these keys are close to each other. As a result, I will get files such as ']' in ...
1 vote
1 answer
36 views

A command that transforms a code comment to a title in a square with many comment symbols, also called an ascii frame

How to create a command that transforms this text # Title Into this text ######### # Title # ######### Is there a name for this kind of text? I have a hard time searching for this in search engines. ...
5 votes
3 answers
3k views

How to remove Neovim trailing white space?

In Vim I used autocmd BufWritePre * :%s/\s\+$//e to delete trailing white space on save. Now, Neovim 0.7 has the new vim.api.nvim_create_autocmd. What is the correct syntax to adapt this autocmd in ...
1 vote
1 answer
41 views

How to suppress "No matching autocommands"?

I have a log library that has a autocmd log User hook, to show saved log or set it to a dedicated scratch buffer or etc. It works quite well, but there is slight nuisance. When inspection/debug done, ...
0 votes
1 answer
17 views

figure out plugin triggering autocmd that CD into home

When I open a new file, [n]vim changes directory into home, regardless of the directory i am in. I didn't wrote code for that behavior, so that must be a plugin. How can I figure out the origin of ...
2 votes
2 answers
557 views

Plugin conflicts over BufWinEnter Autocommands

I have vim-markdown and nerdtree installed. And everything was working fine until I added the following to my .vimrc (verbatim from the nerdtree readme) " Open the existing NERDTree on each new ...
1 vote
1 answer
43 views

Reloading a file with autoread throws an error related to undodir

I have the following contents in my ~/.vimrc: set undofile set undodir=$HOME/.vim/undodir " https://unix.stackexchange.com/a/383044 autocmd FocusGained,BufEnter,CursorHold,CursorHoldI * \ if ...
5 votes
5 answers
3k views

Stop highlighting when entering Insert mode

I would like Vim to stop highlighting matches when entering Insert mode. And not to highlight them again until a new search is performed. A first thought would be to add the following line to my ....
2 votes
1 answer
1k views

Is it possible to set a vim.api.nvim_create_autocmd for a FileType (not just a pattern)?

Currently I'm using pattern in vim.api.nvim.create_autocmd(): vim.api.nvim_create_autocmd({ "BufEnter", "BufFilePost" }, { pattern = { "*.tf" }, callback = ...
0 votes
1 answer
30 views

The pattern of `DirChangePre` is strange where it jumps between `global` and `tabpage`?

As title. I'm trying to fix my plugin with a problem related to the pattern of autocmd. I know that the event DirChangedPre brings a pattern with it when it happens. To reproduce the strange thing I ...
1 vote
1 answer
245 views

Always duplicate yanked text to system clipboard

I'm using neovim 0.8.2 Is there a way to automatically mirror last yanked text to system clipboard, regardless of which register I used to yank it to? So if I run something like "ayiw I will have ...
3 votes
1 answer
125 views

How do I change behavior on write based on file path?

I have the following function in my config and it works fine for formatting files on save function! Formatonsave() let l:lines="all" if has('python') pyf ~/bin/clang-format....
1 vote
1 answer
31 views

Is it correct that between a CmdlineLeave and a CmdwinEnter there must be a CmdlineEnter?

Given augroup Dummy autocmd! autocmd CmdLineEnter / echom "CmdLineEnter /" autocmd CmdwinLeave / echom "CmdwinLeave /" autocmd CmdLineLeave / echom "CmdLineLeave /&...
0 votes
1 answer
26 views

Is it possible to delete part of a wildcard autocmd?

If I put this augroup AU_NAME autocmd! autocmd CursorMoved *.cpp,*.hs echo localtime() augroup END autocmd! AU_NAME CursorMoved *.cpp in my vimrc, then open a file and enter :au AU_NAME, I see --...
6 votes
2 answers
413 views

Is there an autocmd-like event for when operator pending mode is entered?

I'd like to make a plugin to display currently pending keys while in operator-pending mode. As the first part of that, how would I register an autocmd to run when operator-pending mode is entered? I'm ...
4 votes
1 answer
177 views

Event SourcePost doesn't exist

While searching for autocommands event I noticed that there are no SourcePost event. This is strange, because nearly all of other event have a Post version of it (e.g. FileAppendPost, BufWritePost, ......
0 votes
0 answers
711 views

Neovim augroup and autocommand in lua (relative line numbering)

I'm converting my init.vim into a set of lua config files, called from init.lua I'm trying to convert this from init.vim augroup numbertoggle autocmd! autocmd BufEnter,FocusGained,InsertLeave * ...
2 votes
1 answer
336 views

Combine FileType with other events

I'd like to combine two events: FileType and TextChangedI. According to this awesome book I can do that, by adding a comma between both events like this: :autocmd BufWritePre,BufRead *.html :normal gg=...
0 votes
1 answer
72 views

`au BufWritePost` equivalent that matches on FileType?

There are a few cases where the file extension isn't necessarily indicative of the file contents, and filetype needs to be set with a modeline. For example, Salt *.sls files can contain many ...
0 votes
2 answers
80 views

What's the difference between `:doautocmd` and `doautoall`?

I'm using NeoVim and trying to understand why the need of creating augroup. Then I realized that I don't know the two related commands doautoall, doautocmd. Can anyone help me point out the important ...
1 vote
1 answer
85 views

vim: FileChangedShell-like notification for file created after editing started

I really dislike vim taking the focus when I enter a window with a file that has been edited elsewhere, asking me to pick (O)k, (L)oad file and other options. There is a solution for that: register a ...
0 votes
3 answers
163 views

Autocmd and key mapping with a conditional expression

I am currently running the following configuration for latex: NVim 0.9, Vmitex and Evince. I have been trying to map a key to open <current-file>.log, while the current-file is a name taken ...
18 votes
2 answers
11k views

Suppress output from a Vim autocomand

I have an autocommand defined in my vimrc: au BufWritePost * !./make.sh The script make.sh compiles a .tex file 3 times and I don't want the output to be displayed. Is there a way to suppress the ...
0 votes
0 answers
63 views

Terminal inside vim: I can't quit without first exiting the terminal, any autocmd for this?

When I enter vim and open :terminal I want to :wq or :q without first having to switch to the terminal window and typing exit Any autocmds for this? Can't seem to find any. Note: I have the terminal ...
0 votes
1 answer
126 views

Autocommand to run :Prettier on :w

I am working in a React (TypeScript) project. I am using LSP with Neovim and have TypeScript Server installed. Right now, after I make a change, I have to run :Prettier followed by :w. I'm getting ...
0 votes
1 answer
56 views

Vim: cursor manipulation in help file

So I want to be able to automatically open help files in a new tab. I saw a question on this SE that pretty much solved my problem. I wanted to make just one change: center the screen. "That will ...
2 votes
1 answer
178 views

How can I execute a command after searching with `/`?

After a search command / (i.e., in normal mode) is done, I would like another command to be executed automatically. At the moment the other command should be :normal! zz, in order to center the found ...
0 votes
1 answer
355 views

Reload vim autocmd Filetype [duplicate]

I tried searching for this on the internet but I get bombarded with answers saying to :so $MYVIMRCwhich does not work if I add lines of the type autocmd FileType foo bar. What's a more efficient way ...
1 vote
1 answer
42 views

autocmd calling function isn't triggered

The following ftdetect-file doesn't work as expected: augroup dummy au! au BufRead bla* setf dummy au BufRead bla* call s:detect_ft() augroup END echom 'one' function! s:detect_ft() abort ...

1
2 3 4 5
9