2022 Developer Survey is open! Take survey.

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
0 answers
28 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 ...
user avatar
  • 11
0 votes
0 answers
24 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 ...
user avatar
1 vote
1 answer
90 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 ...
user avatar
0 votes
0 answers
9 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 ...
user avatar
1 vote
2 answers
30 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, ...
user avatar
2 votes
1 answer
57 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>:...
user avatar
  • 23
1 vote
1 answer
24 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 ...
user avatar
  • 545
-1 votes
1 answer
39 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
user avatar
  • 900
0 votes
2 answers
78 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 ...
user avatar
0 votes
0 answers
34 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 ...
user avatar
  • 900
0 votes
1 answer
16 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 ...
user avatar
2 votes
1 answer
105 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 ...
user avatar
  • 1,572
2 votes
1 answer
26 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 ...
user avatar
2 votes
2 answers
88 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. ...
user avatar
  • 1,572
0 votes
1 answer
92 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 ...
user avatar
  • 1
1 vote
1 answer
30 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 ...
user avatar
  • 211
0 votes
1 answer
46 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 ...
user avatar
0 votes
1 answer
30 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 ...
user avatar
  • 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 ...
user avatar
1 vote
1 answer
32 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! ...
user avatar
2 votes
1 answer
163 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 ...
user avatar
2 votes
2 answers
157 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 ...
user avatar
1 vote
1 answer
38 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 ...
user avatar
  • 113
0 votes
1 answer
192 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 ...
user avatar
  • 3
0 votes
2 answers
79 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("..&...
user avatar
  • 105
0 votes
2 answers
89 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 & ...
user avatar
  • 103
1 vote
1 answer
52 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 ...
user avatar
  • 371
0 votes
0 answers
51 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 ...
user avatar
  • 485
0 votes
1 answer
143 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 ...
user avatar
0 votes
1 answer
59 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: ...
user avatar
  • 531
1 vote
0 answers
88 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> ...
user avatar
  • 141
1 vote
1 answer
118 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 ...
user avatar
  • 163
2 votes
2 answers
79 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 ...
user avatar
  • 33
0 votes
1 answer
101 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> ...
user avatar
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 ...
user avatar
2 votes
1 answer
36 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 ...
user avatar
  • 21
0 votes
1 answer
96 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&...
user avatar
2 votes
1 answer
143 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 ...
user avatar
  • 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 ...
user avatar
0 votes
1 answer
84 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:...
user avatar
  • 105
2 votes
1 answer
45 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, "\ //*********************************************************...
user avatar
1 vote
3 answers
199 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 ...
user avatar
0 votes
1 answer
77 views

How do I open a relative path under the cursor

I often have file log where there are several relative path to files. The paths are relative to the location of the filelog. My understanding is that I'm able to open the relative path if the CWD of ...
user avatar
1 vote
2 answers
30 views

Using ! in a function

I have the following command for arranging a few lines in a file using sed and column: :-3,+2!sed 's/- 2021/\&2021/' | sed 's/ 2021/\&2021/' | column -s '&' -t This works fine. For ...
user avatar
4 votes
1 answer
146 views

What is the most idiomatic way to achieve filetype-specific functions?

Is it possible to declare a function only for a specific filetype? Currently, if I have the following in ftplugin/tex.vim: function! Foo() echom "yes" endfunction Then when I load a *....
user avatar
0 votes
1 answer
40 views

Is it possible to call a shell command which includes default register contents inside a function?

I want to write a function which opens a vim plugin's Github page, eg calling the function while on this line: Plug 'tpope/vim-rhubarb' ... will do !open https://github.com/tpope/vim-rhubarb. Here's ...
user avatar
  • 101
3 votes
1 answer
151 views

Backward search in current line with search() and z option

I am trying to write a couple of simple functions to find the next and previous links in Vimwiki and move to them. Links in Vimwiki are indicated with the syntax [[link to something]]. Since I only ...
user avatar
  • 255
0 votes
1 answer
41 views

Change text without interrupting abbreviations

I'd like to add a semicolon automatically in my current line. So I have this in my init.vim: function Test() if matchstr(getline(line('.')), ';') != ';' && strlen(getline(line('.'))) > ...
user avatar
  • 388
0 votes
1 answer
86 views

Make function run only on specific filetype

So i have a specific function I'd like to edit in my .vimrc, so that it work only on specific filetype (eg: .vim). The code for this example is taken from this answer. function OrgFold(lnum) let ...
user avatar
0 votes
1 answer
56 views

How to return keybinding from a function?

I want the below function to return keybindings but all it returns is a string. nmap <expr> <space>t ManageTerminal() function ManageTerminal() if bufname('terminal') ==# 'terminal' ...
user avatar

1
2 3 4 5