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
24 views

How to save reference to function before overriding it?

I want to modify the behaviour of an existing plugin (without forking it), such that whenever a particular function plugin_name#foo() is called, my added behaviour is run afterwards. In some ...
TamaMcGlinn's user avatar
1 vote
0 answers
36 views

`term_start` fails to have `-m` option for shell like bash when using the shell path to invoke

I uses vim 9.1 shipped with bash 5.2.26(1)-release (x86_64-pc-msys) both installed by git-bash. :h terminal-use says :term will start one "terminal emulator" and ran the job. Then when I ...
An5Drama's user avatar
  • 125
0 votes
1 answer
27 views

Using the return of a user defined function in an :execute statement

I am trying to get the output of the following function to an :execute statement such as I have this in my .vimrc def GetBufAbsPath(): string return substitute(expand("%:p"), '[^\/]\+$', ...
freddie_ventura's user avatar
2 votes
3 answers
186 views

Insert current date into buffer

I want to insert current date into buffer. I did in my .vimrc - it works: nnoremap <F5> "=strftime("%Y%m%d %H:%M:%S %Z")<CR>P But how can I create a Vim command (e.g.: :...
Clem Fandango's user avatar
1 vote
4 answers
111 views

Vim mapping to make function parameters become multi-line?

I'm looking for a minimal way to convert code that looks like this: my_function(param1, param2=value2, param4=a_function_call()) into something like: my_function( param1, param2=value2, ...
beyarkay's user avatar
  • 205
0 votes
1 answer
34 views

Is there an efficient way to delete the duplicated block with same start and end to keep unique?

Description I have many large size files may including the same function definition. The block description is start with .start .* and end with .end In this example there is one duplicate definition ....
M_Sea's user avatar
  • 157
1 vote
2 answers
95 views

How to noremap plugin functions that take arguments?

I am aware that it is good practice to map some plugin function, like for example the piece of code that follows (Vim9): import autoload "../lib/my_script.vim" noremap <unique> <...
Barzi2001's user avatar
  • 550
1 vote
1 answer
339 views

Why are non-existent vim.fn functions defined in Neovim's Lua API?

From within Neovim, I can execute the command :lua print(vim.fn.non_existent_function), and would get something like function: 0x01052bc2a8 as the printed output. But of course, if I were to actually ...
Jethro Cao's user avatar
1 vote
1 answer
47 views

How do I get size of "whole" Vim instead of a single window?

I want to create a floating window, with the width of the entire Vim viewport. I had used winwidth() function, but I've encountered problems when windows are split. I could get a list of windows in a ...
szym.mie's user avatar
0 votes
2 answers
99 views

Use "normal" in function

I'm writing a function to automate some task. I did this: function InitTTbuffer() " Split file and find the first imap :exe "normal \<C-w>\<C-s>/imap\<CR>" ...
Antonio's user avatar
  • 287
0 votes
0 answers
62 views

Is there a way to access the text currently being typed in insert mode?

From :help registers I know that the . register contains the last inserted text. However, thanks to the following autocmd, I have verified that it's content is actually updated after exiting insert ...
Enlico's user avatar
  • 2,194
1 vote
1 answer
171 views

Insert result of expression after cursor

This function inserts the current date and time on the line after the cursor: function! InsertDate() put = strftime('%FT%T%z') endfunction I'd like to insert the date and time on the current line ...
Big McLargeHuge's user avatar
1 vote
3 answers
2k views

Can I use Telescope to find all commands and functions?

I am switching over from Emacs, Doom Emacs specifically. It had a very nice feature where you could just press M-x and then start typing the command you're looking for and it will fuzzy find it for ...
Vipul Rajan's user avatar
0 votes
2 answers
320 views

How can I write a custom formatexpr to specify the point at which 'gq' wraps?

I don't want VIM to automatically wrap text, so setting textwidth is not a solution to this problem. This comment gives a useful outline, but I'm not really sure how to go about doing this. My first ...
AmagicalFishy's user avatar
0 votes
1 answer
71 views

Passing arguments to function in remapping

I have the following solution in my .vimrc that allows me to wrap text with certain delimiters that works function! Wrap(action, delim) let l:delims = {'(': ')', '[': ']', '{': '}', '"': '&...
hilbor's user avatar
  • 15
0 votes
0 answers
129 views

How do I pass multiple optional arguments from custom command to function?

Given a function with two optional arguments fun s:foo(one = v:false, two = v:false) echom '<' . a:one . ',' . a:two . '>' endf and a custom command calling it command! -nargs=* Foo call s:...
Enlico's user avatar
  • 2,194
0 votes
1 answer
18 views

User-defined commands that call the same functions but with different arguments

I am trying to create a command that calls the same function but with different arguments, like the following: def g:MyFunction(a: string, b: bool) if b echo "foo" else ...
Barzi2001's user avatar
  • 550
0 votes
1 answer
393 views

Use regex pattern as argument to function

I want to be able to pass a plain regular expression like so autocmd BufReadPost * call TextEnableCodeSnip('bash', /^hello/, /^world/, 'SpecialComment') To a function and use it within as a regular ...
drcomputer's user avatar
1 vote
1 answer
437 views

How can I see a list of snippets in my Vim?

I installed vim-snippets, ultisnips and coc.nvim in my Vim (gVim on Windows). How can I see a list of snippets that I can use in my current open file? For example, when I'm editing a .js file, I want ...
mohamadi_arch's user avatar
1 vote
1 answer
64 views

How to remap F16 to F19 on macOS?

So far I managed to remap F1 to F15, but I can't remap F16 to F19. When I am in insert mode, nothing happens. What is weird is that in the terminal, I can see something is intercepted, for example, if ...
COil's user avatar
  • 121
0 votes
1 answer
39 views

How to predefine functions in syntax file?

I have a syntax file for CSV files. To be able setting the separator, there is a function ChangeChar() and the syn match ... terms are variable. To initialize it ; is used. But to call the SetMatches()...
Andy A.'s user avatar
  • 233
2 votes
1 answer
201 views

Get current vimscript script name and line number?

In C programming language, it's typical to log a message with filename and line number of the source code file the message is coming from: #define log(fmt, ...) printf("%s:%d: " fmt "\...
KamilCuk's user avatar
  • 265
0 votes
1 answer
60 views

Pass local variables in a vimscript function to python

For example, I want to use default arguments: function! MakeOrgTable(...) let l:num_col = a:0 > 0? (a:0) : 0; python << EOF import vim vim.current.buffer[:]=make_table(vim.current.buffer, ...
Tokubara's user avatar
  • 167
3 votes
1 answer
133 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....
emitrax's user avatar
  • 133
2 votes
1 answer
42 views

Can you select and execute VimL / VimScript inside another file?

Update: It is possible with '<,'>source. I figured it out right after writing this question, no need for the complicated workaround. I am gonna still let this question here and not delete it, ...
Bog's user avatar
  • 479
1 vote
1 answer
99 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, ...
qeatzy's user avatar
  • 959
-1 votes
1 answer
31 views

How to write the command statement to call customized function briefly?

I have written the function im .vimrc: function! Rep(raw,dest) range python3 << EOF #omit all the codes EOF endfunction In ex mode, i can use it with 1,10call Rep("raw","dest&...
newview's user avatar
  • 291
1 vote
1 answer
49 views

User function unexpectedly returns the same output every call

UPDATE As @Matt pointed out g:jsonTemplate is a list that needs to be copied. I originally thought that multiline strings were strings and overlooked the varible assignment detail after debugging. The ...
a_baguette's user avatar
2 votes
2 answers
52 views

How could I make this mapping to a function repeatable? [duplicate]

I have a mapping that comments/uncomments lines like 5gc to comment/uncomment five lines, and I would like to make it repeatable with .. Here is my mapping and the function it uses: function! Comment()...
geb's user avatar
  • 31
3 votes
2 answers
261 views

Recursion in lambda expressions

Alright, so I have two recursion functions with the exact same purpose: let Fac = { n -> n == 0 ? 1 : n * Fac(n - 1) } function! Fact(n) return a:n == 0 ? 1 : a:n * Fact(a:n - 1) endfunction ...
Futarimiti's user avatar
1 vote
2 answers
977 views

Select entire function call or extend selection

I have the following situation a lot, where I always end up having to yank two things in a row: For example, I want to yank a function call: func(a, b) Okay, here I can go to f and press vf). But ...
lo tolmencre's user avatar
0 votes
1 answer
231 views

Nvim function to compile file in terminal

function! RunCppFile(filename, filedir) let compile = "g++ " . a:filename . " -o " . a:filedir . "/a.exe" :terminal <i> endfunction nnoremap <C-L&...
Sai Nallani's user avatar
1 vote
1 answer
284 views

What is the default 'tabline' function?

Or at least, where to find it in the Vim source code? Tried to show it with :set tabline, but only got an empty string back. The custom function example in :h tabline is helpful and one that ...
toraritte's user avatar
  • 400
1 vote
1 answer
68 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 ...
radlan's user avatar
  • 677
2 votes
1 answer
64 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/\...
Jonas Mechtheim's user avatar
5 votes
1 answer
419 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 ...
BaRud's user avatar
  • 481
0 votes
1 answer
89 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 ...
Jin's user avatar
  • 185
3 votes
1 answer
253 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" ...
Amarakon's user avatar
  • 261
0 votes
0 answers
196 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 ...
Amarakon's user avatar
  • 261
1 vote
1 answer
30 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 ...
Katie And's user avatar
1 vote
1 answer
165 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 ...
lobelk's user avatar
  • 21
4 votes
0 answers
69 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 ...
Jesse's user avatar
  • 41
0 votes
0 answers
597 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 ...
Marwan Fikrat's user avatar
1 vote
1 answer
164 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 ...
Evan Carroll's user avatar
  • 1,314
1 vote
2 answers
79 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, ...
shadowtalker's user avatar
3 votes
1 answer
377 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>:...
Sangria's user avatar
  • 33
1 vote
1 answer
170 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 ...
radlan's user avatar
  • 677
-1 votes
1 answer
1k 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
Fisher's user avatar
  • 1,086
2 votes
2 answers
891 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 ...
Nordine Lotfi's user avatar
0 votes
0 answers
37 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 ...
Fisher's user avatar
  • 1,086

1
2 3 4 5 6