Skip to main content
Share Your Experience: Take the 2024 Developer Survey

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
1 answer
16 views

Autocommand to reformat working only partially

I'm adding autoformatting on write to my neovim config, but it's only working partially. vim.api.nvim_create_autocmd('BufWritePre', { pattern = {"*.c", "*.cpp"}, callback = ...
Teddy's user avatar
  • 1
1 vote
1 answer
30 views

How to guard doautocmd?

Say that I want to define my own autocmd as it follows: autocmd User Foo echom "Here I am!" And then I have a function that at some point trigger that autocmd, like the following: def Func()...
Barzi2001's user avatar
  • 606
1 vote
1 answer
117 views

How do I Conditionally Execute an autocmd Based on the File Extension?

I have an autocmd which removes all trailing white space on write: The autocmd: autocmd BufWritePre * :%s/\s\+$//e For markdown files I need there to be trailing spaces. I'm looking for something ...
Nickotine's user avatar
  • 202
2 votes
1 answer
29 views

How do I prevent/remove/delete temp files?

When I create .txt files using Vim the following files are also created. How do I prevent/remove/auto-delete the files ending in ~?
javilan's user avatar
  • 23
0 votes
0 answers
53 views

undofile without changing :undo

I think of writing as saving, and quitting as committing. Recently I discovered "undofile" which while wonderful breaks my save/quit mental model. I'd like for ":undo" with "...
user19087's user avatar
  • 101
0 votes
0 answers
20 views

WSL: Value of v:event.regname for non-alpha-numeric registers

I have a rather slow autocmd TextYankPost command that I'm trying to limit to the quoteplus (+) register. I've tried the following to get this to work: autocmd TextYankPost * if v:event.regname is '+' ...
Rob Mosher's user avatar
0 votes
0 answers
32 views

How to close unnamed, unmodified buffer when hidden?

I'm currently trying to set this up using the following autocommand, but it is not working for some reason. -- Delete unnamed, unmodified buffers when they are hidden. vim.api.nvim_create_autocmd(&...
jinscoe123's user avatar
2 votes
1 answer
27 views

Neovim: Making a function that checks the beginning of line and acts depending on it

Extra details, The whole code: local obsidianSettingsGroup = vim.api.nvim_create_augroup('Obsidian Group', { clear = true }) vim.api.nvim_create_autocmd('FileType', { pattern = {'markdown'}, ...
Crimson Crusader's user avatar
2 votes
3 answers
88 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 ...
Dave's user avatar
  • 123
1 vote
1 answer
39 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 ...
YJH16120's user avatar
  • 135
1 vote
1 answer
94 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 ...
HeavyWrench's user avatar
2 votes
1 answer
195 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 ...
Meinrad's user avatar
  • 25
1 vote
2 answers
132 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 ...
paradroid's user avatar
  • 377
1 vote
2 answers
62 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 ...
Antonio's user avatar
  • 287
0 votes
2 answers
207 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? ...
Nias26's user avatar
  • 11
1 vote
1 answer
110 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 ...
M_Sea's user avatar
  • 157
1 vote
1 answer
103 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 ...
idbrii's user avatar
  • 661
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 ...
M_Sea's user avatar
  • 157
3 votes
1 answer
681 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 ...
paradroid's user avatar
  • 377
1 vote
1 answer
157 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 ...
M_Sea's user avatar
  • 157
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....
Ronak M Soni's user avatar
1 vote
1 answer
65 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() :...
balki's user avatar
  • 355
0 votes
0 answers
21 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", { ...
user48990's user avatar
2 votes
1 answer
36 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 ...
idbrii's user avatar
  • 661
2 votes
2 answers
504 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....
Jethro Cao's user avatar
1 vote
2 answers
72 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 ...
Andy Stewart's user avatar
  • 1,359
1 vote
1 answer
281 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 ...
Futarimiti's user avatar
0 votes
1 answer
68 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]....
Melvin Q. Watchpocket's user avatar
0 votes
0 answers
16 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, ...
Iggy's user avatar
  • 175
0 votes
2 answers
321 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
Ari Sweedler's user avatar
1 vote
1 answer
176 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('~') .. '*/...
Stein's user avatar
  • 143
0 votes
0 answers
35 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 ...
Enlico's user avatar
  • 2,214
1 vote
1 answer
371 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" }, { ...
Nico Elbers's user avatar
0 votes
0 answers
46 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, ...
Enlico's user avatar
  • 2,214
0 votes
1 answer
412 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:...
Nico Elbers's user avatar
0 votes
1 answer
39 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 ...
Enlico's user avatar
  • 2,214
0 votes
2 answers
42 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 ...
Enlico's user avatar
  • 2,214
0 votes
0 answers
82 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('.')) != -...
Barzi2001's user avatar
  • 606
0 votes
1 answer
85 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 ...
Barzi2001's user avatar
  • 606
0 votes
1 answer
117 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 ...
Futarimiti's user avatar
1 vote
0 answers
140 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 ...
Muhammad Sulthan Mazaya's user avatar
0 votes
2 answers
526 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?
Boyd's user avatar
  • 209
0 votes
0 answers
24 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 ...
Enlico's user avatar
  • 2,214
0 votes
2 answers
96 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 ...
Nikita M. Grimm's user avatar
1 vote
1 answer
422 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 ...
FalcoGer's user avatar
  • 149
0 votes
0 answers
22 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 ...
Barzi2001's user avatar
  • 606
1 vote
0 answers
97 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} ...
minseong's user avatar
  • 2,333
0 votes
1 answer
130 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: ...
Paul Rougieux's user avatar
0 votes
1 answer
94 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 ...
rqdmap's user avatar
  • 3
1 vote
1 answer
41 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. ...
Paul Rougieux's user avatar

1
2 3 4 5
9