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
2 votes
3 answers
70 views

Remove blank lines at end of file

How can I augment/modify my trailing whitespace logic to also remove any empty or all whitespace lines at the end of a file? My current whitespace stripping logic in my .vimrc is: "remove ...
1 vote
1 answer
83 views

Disable colorcolumn on BufLeave

I would like to disable the colorcolumn in a buffer when switching to a different buffer. I try to perform this with a autocommand using the BufLeave Event. In Neovim I use the following local ...
3 votes
2 answers
711 views

Detecting the Enter and Leave of Visual mode event

Are there any events similar to InsertEnter and InsertLeave that I could use for visual mode? Looking through the helpfiles, I did not fine any. If not, how would I be able to detect if Vim enters or ...
2 votes
1 answer
415 views

Can I make a bufwrite bound command save the buffer without triggering infinite filewrite operations?

I have a problem similar that for which nnoremap was conceived, but for the BufWrite aucmd. I have a code formatter which isn't very well-behaved. Gnatpp demands to read a file directly rather than be ...
1 vote
1 answer
28 views

Remembering folds across sessions but in Lua

the question is as the title says. I have code that works but not as I expect. For example, I have a fold here. And another one as well. These two folds are from the same file. When I save and quit ...
2 votes
1 answer
125 views

How to escape spaces on Windows path in autocmd?

I use the following autocmd to generate a template for markdown files in Vim. au BufNewFile C:/my_dir/*.md :silent 0r !C:/my_dir/template.py '%' Our IT department now forced me to move my files to a ...
0 votes
1 answer
252 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! ...
1 vote
1 answer
98 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 ...
1 vote
1 answer
2k 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 * ...
0 votes
1 answer
227 views

How to run a callback when an nvim plugin is loaded. Can I tie this back to running a callback when a plugin is required/imported?

I can see in vim how we can: Call function only when the plugin is loaded But I want to: Call function only when the plugin is required
1 vote
1 answer
146 views

Vim command-line mode encoding error when using global search in autocmd

Update Found the issue due to souce the menu this cause encoding=utf-8 and fileencoding=cp936 in _vimrc but in txt files it shows encoding=utf-8 fileencoding=utf-8 The way to fix is reset fenc=utf-8 ...
1 vote
2 answers
114 views

How can I disable the terminal statusline in gVim?

I want to disable the terminal statusline in gVim as it seems quite useless and I usually have the windows at a relatively small size when using gVim, so it takes up too much space. I've tried quite a ...
1 vote
2 answers
60 views

Unmap error in filetype autocommand

I use a plugin for todo.txt file (https://gitlab.com/dbeniamine/todo.txt-vim). Because I mapped some d[other_letters] in my .vimrc and there's a \d mapped in the plugin, to avoid delays when I type \d ...
1 vote
2 answers
1k 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
1 answer
87 views

How do I create a dummy autocommand for User? [duplicate]

:help User warns about using doautocmd without any registered autocmds: Note that when :doautocmd User MyEvent is used while there are no matching autocommands, you will get an error. If you don't ...
0 votes
2 answers
116 views

How to exclude a specific buffer from the BufEnter autocmd group?

I want to load a plugin when I enter a buffer, so I set this event to BufEnter. But when I start neovim, the plugin loads at the dashboard buffer. I want to exclude that buffer. How can I do it? ...
1 vote
1 answer
131 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)) ...
1 vote
1 answer
93 views

Put the mutiple search or replace results into different split window

Dsecription I am using :g/regex to do global search and it always list --More-- So I need to click Enter or Space to see the full results, it takes too much time. I found one way to achieve my goal ...
1 vote
1 answer
56 views

How to save the files when global replace completed in one command line

I have so many huge size of files and always need to do search and replacement so I map the command as nnoremap aa :let i=1 \| g/regexp/ s/^/\=printf("%1d. ",i)/g \|let i =i+1\|:w! 1.txt ...
3 votes
1 answer
674 views

How to make Vim start at the end of a template?

I have skeleton template files for various filetypes, and when I create a new file based on these templates, the cursor always starts at the top of the file. It would be more convenient if it started ...
2 votes
2 answers
286 views

How to exclude filetype in autocmd using Neovim's Lua API?

Translating this excellent Vimscript answer for creating an autocmd that applies certain options only to the focused window into Lua, I have the following: local focused_window_group = vim.api....
1 vote
0 answers
29 views

Autocommand for i_CTRL-x mode?

Is there a way to run a function when entering i_CTRL-X (ins-completion) mode? It doesn't seem to be an event I can run an autocmd for? I can just imap C-S-x (say) to run my function and then call C-x....
2 votes
2 answers
737 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
59 views

Autocommand is executed multiple times on filetype

I am trying to auto format python files on save. first.py #!/usr/bin/python3 print( "first") second.py #!/usr/bin/python3 print( "second") py.vim def BlackFormat() :...
2 votes
3 answers
867 views

How to set autocmd for files without a filetype?

I want some autocmds to run when I open a file without a filetype like nvim (no file name) or nvim myfile (no extension). I also want them to run when opening certain file types like text and markdown ...
0 votes
0 answers
19 views

switch to terminal opened from autocomand

I am trying to create autocommand to run current python file in a terminal, when it is saved. It is working with the following autocommand: vim.api.nvim_create_autocmd("BufWritePost", { ...
2 votes
1 answer
34 views

Create autocmd for downloaded files (filename contains spaces and incrementing numbers)?

I want an autocmd for downloaded log files. These files are called log.txt or log (1).txt (where 1 could be any number). (On Firefox/Windows, when you download a file with the same name as another ...
12 votes
3 answers
8k 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
2 answers
59 views

How to detect netrw directory listing from autocommand?

I want to detect from a BufEnter autocommand whether the buffer is a netrw directory listing. It seems that when the autocommand is fired, the filetype is empty and there are no netrw-related buffer ...
1 vote
1 answer
206 views

Is there an autocmd event that triggers when a command becomes available?

I'm using nvim-lsp and pyright. That latter provides a command PyrightSetPythonPath to reset the path to the python interpreter. Since I can see no other way to perform the same task, I'd like to call ...
2 votes
2 answers
1k views

Navigate to first diff in diff mode on startup?

Vim diff opens 2 files for diffing, but it doesn't navigate to the first diff section automatically on startup. Can I add something to startup script (like _vimrc) to do this?
0 votes
1 answer
64 views

Error message when opening .ini file

Vim shows the following long error message when I open a file with a dot-ini (.ini) file extension: Error detected while processing /usr/share/vim/vimrc[25]../usr/share/vim/vim90/syntax/syntax.vim[44]....
60 votes
7 answers
27k views

How do I save a file in a directory that does not yet exist?

Suppose I start Vim to edit a new file in a directory that is not yet created: vim nonExisitingDirectory/newFile.txt Vim will happily show me an empty buffer and I can start writing my new file. But ...
0 votes
0 answers
13 views

Vim Autocommand pattern to match rspec.rb files?

I am trying to trigger rspec each time I save a file, but none of the patterns I gave trigger the command. What did I do wrong? I am using the vim-rspec and dispatch.vim. The rspec commands run fine, ...
1 vote
1 answer
140 views

Negate pattern in autocmd

I have some lua auto command that triggers whenever I open a file that has */some_pattern/* in the path. vim.api.nvim_create_autocmd({ 'BufEnter' }, { pattern = vim.fn.expand('~') .. '*/...
8 votes
2 answers
2k views

autocmd with quickfix window

How can set BufEnter or WinEnter autocmds for entering quickfix window? I tried autocmd WinEnter quickfix echo 'winenter' autocmd BufEnter quickfix echo 'bufenter' None of them works. I doubt that ...
2 votes
1 answer
176 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
251 views

How can I run a java file from a neovim terminal using a remap

For a bit of background, I'm trying to set up a function that runs a java file in Neovim, I've tried using the following autocmd: vim.api.nvim_create_autocmd( { "FileType" }, { ...
0 votes
0 answers
33 views

Popup to show number of matches of a given string while in insert mode, and while completion popup is being shown

This is a follow up to a previous question. Thanks to that, I could come up with this augroup: augroup Foo autocmd! autocmd TextChangedI,TextChangedP * call <SID>foo() fun s:foo() let ...
0 votes
0 answers
43 views

How can I get the number of matches of pattern without altering the search buffer nor moving the cursor?

My end goal is to be able to highlight all the matches of the word being typed and echo their number, but this question is about one specific aspect of this, which I've been having trouble with, ...
0 votes
1 answer
320 views

Autocmd is breaking my lsp and not working (neovim)

I’m new to neovim so there’s a decent chance this is just a rookie mistake. I wanted to make an autocmd to remap F5 to running the (python) file. I found a stack overflow post on how to do this (https:...
0 votes
2 answers
382 views

nvim: How do I insert a luasnip for BufNewFile "_skel"

I am using nvim-cmp and luasnips and I have a _skel snippet, I would like to create an autocmd to trigger this snippet for New Files: I already have some code: local status_ok, packer = pcall(require, ...
3 votes
1 answer
493 views

How to use "FocusGained" to stop my cursor from moving

I tend to use GVim a lot. Very frequently, I'll click on some other window and then when I click on vim to regain focus, my cursor will end up in some random location depending on where I clicked. I ...
0 votes
1 answer
35 views

How does <cword> work in insert mode?

Basically I want to access the last \w\+ in my current insertion. (Hence I asked this question.) Another solution I thought of was something like the following (I'm echoing, but clearly I'd pass it to ...
0 votes
2 answers
41 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
73 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('.')) != -...
30 votes
3 answers
10k 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
70 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
109 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 ...
1 vote
0 answers
96 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 ...

1
2 3 4 5
9