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
16
votes
1answer
15k views

Why am I getting a “E488: Trailing characters” error on this custom command?

I have a ~/.vimrc that contains just this: function! NewFile() let filename = input("Filename:") endfunction command NewFile :call NewFile()<cr> (of course my real .vimrc is more ...
12
votes
4answers
787 views

Command to loop through spelling suggestions

I mapped zz to 1z=, which is great most of the time, but every now and then the first suggestion isn't the right one. So I'd like to keep repeating zz (or .) to cycle through the other suggestions. ...
8
votes
3answers
2k views

Tutorial for vim programming? [duplicate]

I am a veteran user of vi. When I started using it, there was basically nothing... Now, we have vim. But, how do I program it? Where does one start? With help? Not sure this is a good start.
7
votes
2answers
689 views

Go to the next word starting with specific letter on current line

I want to have the command which is stated in the title. I looked for it in intrinsic Vim command but did not find it. I ran some tutorial online about functions in vim, The idea would be to have ...
7
votes
1answer
253 views

Count number of visible lines in document with folding?

I'd like to write a plugin that automatically sets the foldlevel of a newly opened document so that as much of the document fits on the screen as possible. (In other words, I'd like to make sure short ...
7
votes
1answer
287 views

Make zG and zW persistent

Edit: I turned this into a little plugin. This is my first attempt at writing one and I have no idea what I'm doing, so any help is much appreciated. :) Here it is: https://github.com/danielbmarques/...
7
votes
0answers
241 views

vim ZZ lost output with autocommand

I want to check file before quitinq vim with custom functions, everything seems fine with QuitPre autocommand except using ZZ to exit. In this case the output is missing. Consider following function ...
6
votes
2answers
761 views

Are script local functions (s:funcName()) unit testable?

Consider a script local function in a plugin/myplugin.vim file. For example myplugin.vim: func! s:someFunction() return 4 endfunc I would like to unit test this function. In the simplest ...
6
votes
2answers
2k views

How to wait for user input in the middle of a mapping?

I want to make a mapping that does the following: nnoremap <F1> oHello, (user inputs their name. Ex. Jason). You have a very nice name.<ESC> This mapping should make a non-recursive map ...
6
votes
1answer
86 views

When should a function *not* be defined with “abort”

If you define a function with the abort argument, your function will abort as soon as an error is detected. Is there any case where this is not what you'd want? Even if your function needs to perform ...
5
votes
3answers
257 views

Way to get content of Visual Selection?

Now if I select something in visual mode, is there some way to get the selected area without having to yank it to some register? I looked through :h functions but didn't find anything.
5
votes
4answers
2k views

How to pass control characters, e.g. <c-e> to a function in a mapping?

I often find myself accidentally pressing <c-e>/^E while in normal mode inside a terminal buffer. This causes the terminal buffer to scroll down and a line with a ~ is displayed, like when your ...
5
votes
4answers
228 views

How to get all distinct words within a set of lines?

I would like to extract a list of distinct words from a set of lines. Is there a way of doing this ? Say for example I have lines that look like this: [ [(isPhysicallySettledFxFwd, NO,"Y"),(...
5
votes
1answer
601 views

Print an error message without “Error detected while processing function”

If I type: :echoerr "test error" I simply get the message printed (with red background): test error However, inside a function: function! Printerror() echoerr "test error" endfunction I get ...
5
votes
1answer
425 views

Vim Function to Send Highlighted Text to a Git Commit

Problem: Suppose I have the following text in my vim buffer: This is a commit msg. Suppose further I have a git repo located at ~/my_repo. Goal: Make a vim script so that I can highlight the text ...
5
votes
3answers
437 views

Making a custom function to comment out a range of lines

I want to make block commenting easy. To comment out a range of lines, what I can do is something like: :17,21s/^/# This will comment lines 17 to 21 in Python. But I don't want to write the whole ...
4
votes
1answer
1k views

How to test if a string is a vim command?

Hopefully, the solution is efficient if it exists. A solution that tests if it is a vim builtin command suffices. EDIT To answer @Xiong Chiamiov comment, here is what I do in my .vimrc: basically ...
4
votes
1answer
271 views

Does Vim compile user-defined functions?

If I define a function in my vimrc (or in a plugin), Vim will load it and I can use it. But what's happening under the hood after my vimscript is parsed? Is it transcoded down to C and compiled? Is ...
4
votes
1answer
179 views

Strange behavior with “map(range …)”

I'm trying to get into more advanced features of vimscript, so I'm trying to figure out how to use the map function, so I thought I'd write a function that returns every character with ASCII values ...
4
votes
1answer
514 views

if/else condition for matching a regex in a file [duplicate]

I want to run a function with a condition that checks for ANSI color escape codes when I open files but I don't know how to write the condition. I expect it to look something like this: if ? '^[[0;...
4
votes
1answer
101 views

Can my mapping/function detect when count/range 1 is given instead of none?

I have a key mapped to a command which runs a function, and I would like to detect whether it has been passed a range (prefixed with a count) or not. The difficultly is that I need to be able to ...
4
votes
2answers
167 views

How to debug an external function

I am trying to debug the vim-fugitive plugin, to dig into how it works. When I develop a piece of vim script, I am used to testing a function with the debug command. Here it does not work because the ...
4
votes
3answers
96 views

Save functions in a session

I have a function I wrote specifically for a certain session in vim. Manually redefining it every time I load the session is tedious. Is there any way custom functions can be saved with the session? ...
4
votes
1answer
695 views

Call function from string name

I am trying to achieve the following : Having a set of predefined functions in my script Having a map of relation between the functions and their argument Calling the functions with their argument ...
4
votes
1answer
75 views

Get visual column number with conceal feature

One can use col('$') to get the length of the current cursor line. Further, one can use virtcol('$') to get the same length, but this returns the actual screen column. However, it ignores the conceal ...
4
votes
1answer
504 views

How do I write a multi-line message to the quickfix or location list?

I've got a program that spits out multi-line messages separated with something other than a newline. I want these messages to show in the quickfix and location lists spanning multiple lines as ...
3
votes
2answers
350 views

Remove new line from result of system() [duplicate]

I would like to store the result of a system() call in a variable without the trailing ^@ character. At the moment I do something like var = system('command | xargs -i echo -n "{}"') but it feels ...
3
votes
1answer
189 views

:exe moves cursor to first line of buffer

function! DebugC() let l:debugStr = "printf(\"" . s:debugPrefixStr . s:debugCounter . "\");" if g:debugstringAlwaysIncludeHeader let l:incStr = "#include <stdio.h>; " ...
3
votes
3answers
1k views

How to put custom functions at bottom?

I want to organize my vimrc better, so I want to put all the custom functions at bottom: "different setting between different os if MyDetectOS() ==? "windows" set rtp+=~/vimfiles/bundle/Vundle....
3
votes
1answer
61 views

VimL/Vimscript: how to access local variable of outer function from its inner function?

I posted this question on stackoverflow already, but realized afterwards that it's probably better to ask here. So I have a function of the following form: function! s:my_function(dict_arg) let ...
3
votes
1answer
1k views

Change command depending on file extension/type

I usually work with php projects and phpunit. I've mapped <leader>T thus I can run tests. # my .vimrc file portion nnoremap <Leader>T :!vendor/bin/phpunit --filter function! ...
3
votes
1answer
82 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 ...
3
votes
3answers
104 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 ...
3
votes
3answers
746 views

Is it possible to create a function with the same name for different filetypes (and different implementations)?

Is it possible to create several functions with the same name whose implementation depends on the file type? For example, I could imagine a function named CurrentFunctionName that returns the name of ...
3
votes
1answer
786 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 ...
3
votes
0answers
436 views

Getting balloonevalterm to work (terminal version)

I'm trying to get balloonevalterm to work in the terminal version of vim, but for some reason it does not yet work. Here's my macros.vim file: function! MyBalloonExpr() let macros = { \ 'MACRO' : ...
3
votes
1answer
649 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 ...
3
votes
0answers
188 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?\...
2
votes
2answers
2k views

Use variable for mapping inside vimrc

I'm testing NeoVim and would like to put in my vimrc universal mapping for either vim and nvim. I've sketch sth like below so far, but it doesn't work and it's very rough. function! Meta(key) " ...
2
votes
1answer
380 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 ...
2
votes
1answer
67 views

What is the purpose of user `command` in Vi/Vim?

What is the purpose of user command in Vi/Vim? I noticed that a lot of commands are usually used to call functions like this: command! TrimWhitespace call TrimWhitespace() From my observation, ...
2
votes
1answer
134 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 ...
2
votes
1answer
358 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?
2
votes
2answers
37 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 ...
2
votes
1answer
62 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 ...
2
votes
1answer
399 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 ...
2
votes
1answer
52 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 ...
2
votes
1answer
126 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 ...
2
votes
1answer
135 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). .... ...
2
votes
2answers
94 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 ...