Questions tagged [functions]

Question about the use of functions in Vimscript defined in the `.vimrc` or a script file.

Filter by
Sorted by
Tagged with
1 vote
1 answer
29 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 ...
  • 587
2 votes
1 answer
35 views

Different replace patterns depending on visual/visual line mode

I've written some customizations in my .vimrc that I would like to further improve and refine. In particular, for some key combinations, I'm doing text substitutions, like this: vmap <C-S-w> :s/\...
3 votes
1 answer
87 views

How can I convert vim8 function mapping to vim9?

I have a function in vim8 as: function! BSkipQuickFix(command) let start_buffer = bufnr('%') execute a:command while &buftype ==# 'quickfix' && bufnr('%') != start_buffer execute ...
  • 437
0 votes
1 answer
31 views

Vimscript: how to overwrite a variable that is inside an if block?

I have some code in a function like so: let branch = substitute(system('git rev-list -1 HEAD'), '\n', '', 'g') let num_branches = system('echo `git branch --contains '.branch.'` | wc -l') if ...
  • 165
3 votes
1 answer
157 views

How Can I Rename a Command but Still Use the Original Command

I have the following Vim code: function! TabClose(...) if a:1 == "" tabonly elseif a:1 == "left" Tabops Close Left elseif a:1 == "right" ...
0 votes
0 answers
39 views

How Can I Make the `feedkeys` Function Run Silenty

I saw this post on Stack Overflow asking the same question but the response simply was "Don’t use feedkeys, here is a different solution." Well I have a use case where I think I do need ...
1 vote
1 answer
20 views

How can I make a command that checks iteratively through parent roots until it finds something or reaches C:/?

I made a command line tool to generate static websites. I want a way to use this command line tool from the vim command line, something like the G command from the Fugitive plugin. The tool can only ...
1 vote
1 answer
47 views

Automatically highlight visual selection matches

Since there are no VisualEnter and VisualLeave events, I am trying to make a function that will activate on every cursor movement in visual mode, i.e. it will automatically highlight all the matches ...
  • 21
1 vote
0 answers
33 views

How to Make Macros Stop with Custom Motion?

I am writing a custom Vim motion. The problem is that when used in a macro (especially at the end of a macro), the macro does not automatically stop when the motion no longer applies. For example, if ...
  • 11
0 votes
0 answers
110 views

(Code review) Copying file path relative to cwd with the line number appended

I am replicating what PHPStorm and VSCode do by allowing me to copy the file path and line number (plus column) to clipboard. I have written a function in lua to do this, but, I wanted to ask about ...
1 vote
1 answer
97 views

Wrap :retab! or create a new command that calls it?

My vim does most everything what I want with :retab! one thing I would like it to do in addition is remove all trailing whitespace like this, :%s/\s\+$// Is there a way to redefine :retab! such that ...
0 votes
0 answers
12 views

How can I sync part of a DOC cross reference with another file?

Is there a way in Vim to sync part of the markdown DOC across multiple files? For example, I have a description of a project, and I have to insert it in other files. When the main description is ...
1 vote
2 answers
45 views

Create an autoload alias for an autoload function

I have a function like this in autoload/foo/bar.vim: function! foo#bar#do_stuff(...) abort " do stuff return endfunction Is it possible to create an "alias" for this function, ...
2 votes
1 answer
97 views

How can I call function in visual mode

I want a function to change underline word to little camel for one line. When I use <shift+v>:s/_\(\w\)/\u\1/g, it work fine. And then I want to make a function to simplify it, <shift+v>:...
  • 23
1 vote
1 answer
36 views

convert byte index to character index

Some function in vim return a byte index instead of a character index (e.g. match()). If the matched string contains multibyte characters this byte index differs from the character index. Does vim ...
  • 587
-1 votes
1 answer
100 views

How to select lines by line number?

Need a function to select lines visually between the given start and end line number. function! Select_Lines (start_line_number, end_line_number) ... endfunction
  • 932
2 votes
2 answers
135 views

Calling function without using call

This is just my own curiosity speaking here, but would it be possible (if it's even possible by default in normal vim, without any additional plugins etc) to call a function inside a vimscript ...
0 votes
0 answers
36 views

How to keep function argument format?

function! test(address, ...) echom a:address return If run :call test(0x000), I'll get 0. How to keep a:address = 0x000? Because I want to populate address width from the input address ...
  • 932
0 votes
1 answer
20 views

How to auto-confirm shortcut command

I have a function called Comment in my vimrc, and I have a shortcut that 'runs' this function: nnoremap <S-Tab> :call Comment() The way it currently works is I press [Shift]+[Tab] in normal ...
2 votes
1 answer
437 views

How can I define vim9 functions and vim9 lambdas in a vim9 script?

In :help vim9 I read The Vim9 script syntax and semantics are used in: a function defined with the :def command a script file where the first command is vim9script an autocommand defined in the ...
  • 1,756
2 votes
1 answer
30 views

Vimscript - Prevent Column From Moving When Using a Count

When you use a count to a key mapping it goes down count number of times and resets to column 1. Actually it already resets to col 1 when entering the function if there is a count. So I can save the ...
2 votes
2 answers
100 views

How can I test the script-local functions of my plugin?

I have the feeling that the answer is you can't, but I'd like to be sure about it. My plugin has some s:function-name functions, and one plugin-name#function-name function that uses the former ones. ...
  • 1,756
0 votes
1 answer
244 views

Vimscript - Loading function(s) from another .vim file

I'm forking an old vim plugin (effectively dead since 2018) that needed some love. Once I got to the .vim file, I noticed that the functions were all defined in there, which made the file unreasonably ...
  • 1
1 vote
1 answer
31 views

Apply next non-blankline function as one part of multi-part user-command

Why do I seem to get different results when I apply this function from the vim command line than when I apply it as part of a user-command? " advance to next non-blank line only if current line ...
  • 211
0 votes
1 answer
52 views

Generate relatively unique "cache filename" for given file path (like swap files do)

For each filename, Vim generates (relatively) unique filename for it's swap cache directory. Let's say I would like to store some information for each file (like swap files do), e.g. with : silent ...
0 votes
1 answer
39 views

How to use histadd() with a custom function

I've got the following in my vim config: map <leader>ot :call OT()<cr> function! OT() let fname = input("ot: ", "", "file") let res = system("ot ...
  • 135
0 votes
1 answer
30 views

Join contiguous lines of natural text and leave adjacent, non-connected lines

I would like to join all lines in a file which are continuous parts of some natural language but not back-to-back lines which are two separate entities. For example: Join: There was once a man called ...
1 vote
1 answer
39 views

Jump to pattern without overriding search

I'm trying to implement a mapping for making the vim cursor jump to a pre-defined search pattern without overriding the current search, but it is falling short in multiple ways: function! ...
2 votes
1 answer
184 views

How can I expand a:000 list in order to pass through all varargs parameters to another function?

I have a varargs function, which I want to refactor to include a separate 'argument handling function' first, so that that becomes separately callable. I've read :help function-argument but was unable ...
2 votes
2 answers
162 views

Vim 8.2 no user defined functions

Yesterday I updated Vim to version 8.2 thru my Linux distro repositories (Fedora). And now I noticed that no user defined functions exist. They worked just yesterday, before the update. I define ...
1 vote
1 answer
45 views

How do I call a function from a user command? (Getting a "trailing character" error, E488)

So I was trying to setup a custom command to change tab width on the fly. function! SetTab(width) set tabstop=a:width endfunction :command Tab -nargs=1 call SetTab(<args>) The idea is to ...
  • 113
0 votes
1 answer
278 views

How can I open a split terminal with :term and then run a command in that terminal?

I'm trying to write a function to open a split terminal in Vim and then run the current file in that terminal. A non-working example trying to explain what I'm trying to do is function! Foo() :bel ...
  • 3
0 votes
2 answers
98 views

Function-local inner functions

These exist with lambdas function A() let B = { -> 'thing' } endfunction // B does no exist But the lambda returns a value and not a sequence of commands. (I know I could put execute("..&...
  • 115
0 votes
2 answers
114 views

using EasyAlign in a function

I frequently use EasyAlign in my tex files to align tables around the $ delimiter such that this header1 & header2 & header3 \\ data1 & data2 & data3 \\ becomes this header1 & ...
  • 103
1 vote
1 answer
58 views

Character level buffer modification

It is possible to modify buffer at the line granularity. E.g. :py3 vim.current.buffer[0] = "abc". But it deletes the attached marks even if a single character is replaced. Is it possible to ...
  • 381
0 votes
0 answers
73 views

Does vim have support for processing its comma-separated options?

Vim has comma-separated dictionary options like 'comments' and 'listchars': :set comments? comments=s:--[[,m: ,e:]],:-- :set listchars? listchars=tab:⇥ ,trail:␣,extends:⇉,precedes:⇇,nbsp:⚭ Commas ...
  • 525
0 votes
1 answer
192 views

Can I define a function with '#'-namespace in a file with hyphen '-' in its name?

I often see #-namespaces in plugins source code, e.g coc#status(), fzf#run(), fugitive#extract_git_dir. I would like to use namespaces in my own config, mappings and commands source code also. But I'm ...
0 votes
1 answer
60 views

Turn a search & replace regex one liner, with counters, into a function with a for next or while loop

this post has been simplified and re-written here: Making a function work with a loop I have this one liner, which takes the regex pattern match and replaces it with a string and a couple of counters: ...
  • 561
1 vote
0 answers
148 views

neovim, vimscript execute keystroke from function

I am trying to execute a keystroke from function when its called. However, it does not work. function SmartNERDTreeFocus() if expand('%') =~ 'NERD_tree.*' normal <C-w><C-p> ...
  • 141
3 votes
1 answer
200 views

What's the difference between command and function?

As I know, both of them can do quite the same things, but one should call to invoke and other one should use execute to invoke. But whats the difference between command and function? when should I use ...
  • 183
2 votes
2 answers
82 views

Making functions react to keypress

Im very new to vim scripting. I have the following script in my .vimrc: If there is no user input aka I press <CR> => go to recent buffer If input is 'd' => delete all buffers except for ...
  • 33
0 votes
1 answer
135 views

Calling a function before autocmd event is triggered

In my vimrc I have a key mapping for switching buffers that also automatically resizes the splits by calling a function: nnoremap <silent> <M-h> <c-w>h:call Splitresize()<CR> ...
0 votes
0 answers
20 views

Remove or Unset a function while vim is running [duplicate]

I know how to create a function: function Something() ... endfunction And how to overwrite it: function! Something() ... endfunction But how to actually remove/unset the function, so it doesn't ...
2 votes
1 answer
49 views

vimscript to skip a block of text with lines containing similar words

I need a little help with some vimscript-ing. I have a huge log file with multiple blocks of text resembling the following: [log file start] ... ... Information: Setting NDR rule shield_rule on net a ...
  • 21
0 votes
1 answer
121 views

How can I append to a terminal buffer in vimscript?

I am trying to implement a vimscript function which appends to a terminal buffer. It works when I write this as a single mapping which uses the normal mode 'a' command to append: nnoremap <Leader&...
2 votes
1 answer
200 views

Passing arguments function with --argument or -a format

Is it possible to pass arguments to Vimscript functions with the --argument or -a format, similar to arguments in the command line? I am writing a command that submits a job in a remote machine. I ...
  • 255
0 votes
0 answers
31 views

Measurement/ tracking the time for editing a file (overall and statistics)

I use vim for coding and also for writing scientific articles and book chapters. It would be very interesting to know the time I spent on a file. For example how many hours or minutes the buffer was ...
0 votes
1 answer
107 views

Using a multiline closure to execute commands in vimscript

I'm writing a script in which I am in need of closure functionalities. Take this function, for instance: func! SetOpNorm(norm_comm, motion_key = 'y') let l:norm_comm = a:norm_comm let l:...
  • 105
2 votes
1 answer
47 views

How to use func to quickly add specific comments to certain lines of a file?

I wrote a function, the function is used to add comments to some lines, the content is as follows: func Add(x) call setline(a:x+1, "\ //*********************************************************...
1 vote
3 answers
209 views

Function That Substitutes Across Multiple Files

I'm trying to create a function that implements the functionality outlined here: Vim search replace all files in current (project) folder My current attempt: nnoremap <C-F> :call ...

1
2 3 4 5 6