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
3 votes
1 answer
564 views

How do you undefine existing functions and key maps?

I am looking at a scenario where in order to keep track of changes to my setup, rather than delete functions and keymaps defined in vimrc or other plugin I add commands which disable those functions ...
vfclists's user avatar
  • 907
3 votes
2 answers
325 views

Reliably saving and restoring registers across a function call [duplicate]

When writing functions to use in mappings and user-defined commands (for plug-ins), it's often convenient to use a Normal-mode command to yank or delete buffer contents. In those cases, it's also nice ...
filbranden's user avatar
  • 28.8k
3 votes
1 answer
681 views

How to organize autoload functions?

I'd like to put all my custom functions into autoload folder, but I have functions with the same name. How should I organize them?
Cyker's user avatar
  • 187
3 votes
1 answer
138 views

Use motion in normal mapping calling a function

I have this function which displays line numbers. I want it to be called in different ways: from a command, specifying the lines as arguments with a visual selection, printing the first and last line ...
nobe4's user avatar
  • 16k
3 votes
1 answer
252 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
3 votes
1 answer
373 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
3 votes
1 answer
2k views

Function that opens file for editing

Is there a function that can be called to open a file for editing? I know how to open and edit files in Vim, and I know I can use :e filename, but is there a built in function I can call that does ...
still_dreaming_1's user avatar
3 votes
1 answer
187 views

Defining a command on more than one line

It seems like the command command expects a one line set of arguments. What is a good way to define a command from something longer? I suppose defining a function with no argument and then calling ...
Toothrot's user avatar
  • 3,129
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
3 votes
1 answer
416 views

Get correct length for unicode characters

Basically title, the length of unicode characters as calculated by the len() function is off by one, for example :echo len("à") 2 or :echo len("°") 2 is there a way to fix this, ...
noibe's user avatar
  • 179
3 votes
3 answers
128 views

remap ':' to maintain last command, and erase all char if any key other than <CR> was pressed

As the title stated, remap ':' to maintain last command, and erase all char if any key other than <CR> was pressed. possible use cases: I have typed a long command, and I may or may not ...
qeatzy's user avatar
  • 960
3 votes
1 answer
774 views

Improve copy to clipboard using xclip

I'm trying to improve the answer here for copying to clipboard in a vim without the +clipboard option. The problem with the above approach is that :w always writes the entire line, so it is not ...
Tim Mak's user avatar
  • 133
3 votes
1 answer
750 views

Insert mode mapping to leave insert mode then invoke a plugin function

In Insert mode, I want a mapping which will return to Normal mode, then call a plugin function. I've tried this: inoremap <a-1> <ESC><Plug>AirlineSelectTab1 It exits Insert mode, ...
LondonRob's user avatar
3 votes
1 answer
394 views

Vim function somehow adds Ctrl-j to macro?

I've written a short vim function that allows me to edit the contents of vim registers: fun! RegBuff() let vimstuff = "/tmp/vimstuff" if !isdirectory(vimstuff) call mkdir(vimstuff) ...
Brian Albert Monroe's user avatar
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
3 votes
1 answer
1k views

Get column of last character in visual line - Vimscript

In one of my vim scripts I require the column of the last non-whitespace character of the current line and the column of the last character of the current line (when the current line is wrapped into ...
user avatar
3 votes
2 answers
565 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 ...
mgarort's user avatar
  • 275
3 votes
1 answer
1k views

Toggle fzf.vim if already open

I am trying to write a vim function to toggle the fzf window if it is already open using fzf.vim. The use case is if I am in the :Buffers view and I want to change to the :Files view if I try to ...
Otis Wright's user avatar
3 votes
0 answers
208 views

system function output with Chinese chars

I want to run shell command with system function and the command output has some Chinese chars. echo system('svn status') But I got incorrect encoding output like this: svn: ?\232?\173?\166?\229?\...
Fenghua Shu's user avatar
2 votes
6 answers
1k views

How to delete a pair of parenthesis with backspace

I'm using vim to code C++ and I want to handle the parenthesis more easily. I knew that there was a plugin named Surround, which can help us but it is not what I expected. I set some shortcuts as ...
Yves's user avatar
  • 1,013
2 votes
1 answer
739 views

Function to refactor indents and tabs

I'm writing a vim function to set hard tabs and refactor indents in a whole file. This is the function: function RefInd() :set tabstop=4 softtabstop=0 noexpandtab shiftwidth=4 gg=G ...
Davide Pucci's user avatar
2 votes
3 answers
185 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
2 votes
2 answers
878 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
2 votes
2 answers
131 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. ...
Enlico's user avatar
  • 2,194
2 votes
2 answers
208 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 ...
d.k's user avatar
  • 692
2 votes
1 answer
327 views

Error With Simple Function Using setline()

I'm trying to start writing some Vim Script, and I've come across this issue. fu Test() call setline(1, getline(".")) endf autocmd InsertCharPre * call Test() This is in my .vimrc. When I type ...
Mason's user avatar
  • 521
2 votes
1 answer
763 views

How to set opfunc to a lambda function?

I've found it's common to set opfunc in this manner: nnoremap <Leader>r :set opfunc=<SID>my_function<CR>g@ With Vim8 supporting lambda functions, is it possible to now set opfunc ...
Roxy's user avatar
  • 177
2 votes
1 answer
75 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, "\ //*********************************************************...
Self-Motivated Wu's user avatar
2 votes
2 answers
804 views

Insert output of function with variable into buffer

I'm trying to make a vim function that takes a variable and uses that as part of a shell command and buts it into the current buffer. I tried using read but that just interprets the var as a string. ...
hemlock's user avatar
  • 23
2 votes
1 answer
3k views

Inside a mapping check if a variable is empty in vim

So i'm new to vim, and specially new to vimscript, and messing around i made the following code to get the name for a file and then create said file in a new buffer. I want a way to check if FileName_ ...
Akuseru's user avatar
  • 41
2 votes
2 answers
68 views

Two consecutive identical calls to echo have different output

Question reposted on StackOverflow, where it's receiving more attention. Given the following function in the .vimrc file, fu! MyFun(count) echo a:count echo a:count if a:count > 0 ...
Enlico's user avatar
  • 2,194
2 votes
1 answer
2k views

How to use a value stored in a variable in the command mode?

Consider the following function function! AbbreviationsOpen() "go to the first word of the last line normal G "store the word in the register named a let @a = expand('<cword>') ...
caffeinemachine's user avatar
2 votes
2 answers
122 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 ...
prizel's user avatar
  • 33
2 votes
1 answer
39 views

Where is the help function defined or located?

I'm trying to find where the help function/command is defined. So far I tried: :verbose function help but since the h isn't in uppercase, it doesn't work (not sure if it's even a function at this ...
Nordine Lotfi's user avatar
2 votes
2 answers
628 views

How to create an abbreviation with space

I'm trying to create an abbreviation with the command ab: if I type if ( under INSERT mode, I want some piece of code to be printed immediately. Here is what I've tried: ab if<Space>( if<...
Yves's user avatar
  • 1,013
2 votes
1 answer
256 views

Possible to set `foldexpr` using a function reference?

Got this: let Func = function(folding_function) "folding_function is name of function setlocal foldexpr=call(Func(v:lnum)) This is so user can set a custom function for folding in their config file. ...
StevieD's user avatar
  • 1,492
2 votes
1 answer
670 views

Carriage return as argument breaks the function

I have a remap that calls a function with a key combination as an argument. In this function I want to execute another remap to yet another function. But whatever I do, it complains about a missing ...
hgiesel's user avatar
  • 1,944
2 votes
1 answer
66 views

Can't seem to get syntax information when in insert mode

This is for an indent function that should maintain the indentation should the cursor happen to be in a string region. It maintains the indentation when I hit == in normal mode but changes when I open ...
Plakhoy's user avatar
  • 121
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
  • 469
2 votes
1 answer
2k 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 ...
Enlico's user avatar
  • 2,194
2 votes
1 answer
388 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 ...
mgarort's user avatar
  • 275
2 votes
1 answer
1k views

execute + colon character doesn't work

I want to :retab selection in my vimscript function. When I write execute "normal! :'<,'>retab" nothing happens. If I write: let cmd = "normal! :'<,'>retab" echom cmd The output is ...
ashrasmun's user avatar
  • 167
2 votes
1 answer
717 views

vim argument substitution inside a function

I would like to use the arguments passed to a function, as an argument to replace a search. Something like this: function! Myfunction(...) %s/hello/ goodBye a:1, a:2/g endfunction Does anyone ...
davide1421's user avatar
2 votes
1 answer
551 views

vimscript: strange behavior of search() with flags 'be'?

vim-help :h search() says it start search at column 0 of cursor position. Search for regexp pattern {pattern}. The search starts at the cursor position (you can use |cursor()| to set it). .... ...
qeatzy's user avatar
  • 960
2 votes
1 answer
527 views

join string and <cword> for vimgrep in command

I want to have a function that vimgrep for the word under the cursor appended to a predefined string. I have defined this command: command! -nargs=0 GJL vimgrep <cword> *.tex | copen which ...
Jan's user avatar
  • 212
2 votes
2 answers
150 views

Running python code in a function

I'm having problems with latex-suite (some research indicates that many people are). The issue at hand is that they have a nifty python script that gives you a nice outline of your document and ...
A Gold Man's user avatar
2 votes
1 answer
865 views

Function does not work when called as from a mapping

I have a function that depends on the character under the cursor : if it's a ), then the function does nothing ; if it's not, the function adds a ). function! Ket() let char = getline(".")[col('.')...
Feffe's user avatar
  • 1,761
2 votes
1 answer
200 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
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
2 votes
1 answer
63 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