Questions tagged [functions]
Question about the use of functions in Vimscript defined in the `.vimrc` or a script file.
277
questions
1
vote
1
answer
34
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 ...
0
votes
2
answers
51
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>"
...
0
votes
0
answers
28
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 ...
1
vote
1
answer
89
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 ...
0
votes
3
answers
758
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 ...
0
votes
2
answers
107
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 ...
0
votes
1
answer
48
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 = {'(': ')', '[': ']', '{': '}', '"': '&...
0
votes
0
answers
79
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:...
0
votes
1
answer
17
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
...
0
votes
1
answer
51
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 ...
1
vote
1
answer
203
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 ...
1
vote
1
answer
43
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 ...
0
votes
1
answer
38
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()...
2
votes
1
answer
141
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 "\...
0
votes
1
answer
41
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, ...
3
votes
1
answer
127
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....
2
votes
1
answer
32
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, ...
1
vote
1
answer
68
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, ...
0
votes
0
answers
24
views
How to deal with the argument which contains whitespace in command?
A function and it's command format:
function! Rep(raw, dest, ...) range
echo a:firstline
echo a:lastline
echo a:raw
echo a:dest
endfunction
command! -range -nargs=+ Rep <line1>,&...
-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&...
1
vote
1
answer
48
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 ...
2
votes
2
answers
51
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()...
3
votes
2
answers
237
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
...
1
vote
2
answers
414
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 ...
0
votes
1
answer
156
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&...
1
vote
1
answer
139
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
...
1
vote
1
answer
43
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
...
2
votes
1
answer
58
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/\...
4
votes
1
answer
351
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 ...
0
votes
1
answer
77
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 ...
3
votes
1
answer
203
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
165
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
24
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
117
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 ...
4
votes
0
answers
59
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 ...
0
votes
0
answers
450
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
126
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 ...
1
vote
2
answers
67
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
266
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>:...
1
vote
1
answer
120
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 ...
-1
votes
1
answer
821
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
2
votes
2
answers
497
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 ...
0
votes
1
answer
63
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
1k
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 ...
2
votes
1
answer
33
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
122
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
vote
1
answer
650
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
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 ...
0
votes
1
answer
64
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 ...