Questions tagged [functions]

Question about the use of functions in Vimscript defined in the `.vimrc` or a script file.

0
votes
1answer
145 views

How do I get a numeric return value from an Ex command?

How can I get a numeric return value from an Ex command. For example, if I want to set a variable to the number of times a pattern occurs in a buffer. I tried this: :let answer=exe('%s/xyz//n') but ...
3
votes
0answers
366 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' : ...
0
votes
1answer
55 views

Replicate normal mode function in visual mode

I had the following mapping to go to the 1st non blank character of a line nnoremap M ^ I then decided that if I was already at the 1st non blank character I wanted M to take me to the absolute 1st ...
1
vote
0answers
18 views

Determining syntax group type from output of 'syn list' (doesn't appear to work for regions)

Why does the output of syn list not include region in the syntax group definition line, like it does for 'match' type groups? What is the meaning of the xxx in that line? I've got a vim 8.0 function ...
3
votes
1answer
176 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>; " ...
0
votes
1answer
38 views

How may I create an excmd that has a signature like g, s, v commands? (`:cmd/str1/str2/options`)

A g command can find the lines globally and then apply actions, like the following would find lines that contain foo and delete those lines: :g/foo/d An s command is more well-known for its ...
2
votes
1answer
102 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). .... ...
1
vote
2answers
268 views

vimscript: get result of cmdline-completion Ctrl-a

Need a function that, returns a string, which is result of cmdline-completion Ctrl-a, i.e., a list of words seperated by space. given an input string of partial ex-command. For example, given :h ...
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 ...
7
votes
1answer
234 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 ...
0
votes
1answer
121 views

Get list of javascript functions in file

The functionality that I am after, is to get a list of the javascript functions present in the file that I am in. Clicking a function in the list will then take me to that function. How do I go about ...
1
vote
1answer
99 views

Using range as argument returns a comparison

I am trying to capture the result returned from a function implementing range as an argument but apparently I get a comparison. With the following code: function! GetNumber() range return 4 ...
2
votes
0answers
129 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) ...
0
votes
1answer
159 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 ...
0
votes
0answers
35 views

Why is this function clearing screen output?

This is an example script which is meant to print the keyboard shortcut for a command. Applying it clears the screen output. The result of commands like intro or version get cleared off the screen ...
1
vote
0answers
175 views

Search not working in function called from autocmd

I am quite new to using vim so please forgive me if this has been asked else where. I have tried Googling but am not really sure what to search for. In my vimrc I have the follwing function that ...
3
votes
3answers
102 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 ...
0
votes
1answer
535 views

Exiting back to normal mode in terminal buffer from Vimscript

<C-\><C-n> is working as (I'd) exepected, but not from a Vimscript. I am trying to make a script that will: open Neovim's :terminal run a python script exit from the terminal input mode ...
1
vote
1answer
269 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 ...
1
vote
1answer
79 views

Can I map the number of search matches to a key?

I'd like to map 0 to whatever number of matches I currently have. For example: This here has 5 matches. If I wanted to perform a macro on every instance, i'd like to be able to simply type: 0Q (Q ...
3
votes
1answer
861 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! ...
1
vote
1answer
31 views

How do I use function to effect editing a file?

I wanted to replace Tab character with the following, depending on what is on either side of the tab. This is for quickly converting pasted in text (to vim 7.4) from a spreadsheet (xls/xlsx) that has ...
3
votes
3answers
669 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 ...
1
vote
1answer
38 views

How can I use a register with :find?

I'm trying to improve my vim-abilities. I'm writing a command and a function to find the current Word under the cursor. What I want to be able to do is type :FindFile and have the word under the ...
0
votes
2answers
119 views

How to make vim functions local to script but callable by mapping?

To make functions local, you prefix it with 's:'. But then it can't be called with mappings. Given this function: function! ScriptFunction() ... endfunction What convention or keywords do I add to ...
7
votes
0answers
222 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 ...
0
votes
1answer
473 views

Alternative to shellescape that doesn't add quotes in Linux?

According to the documentation for shellescape in vim: shellescape({string}, [, {special}]) shell escape() Escape {string} for use as a shell command argument. ... For other systems, it will ...
3
votes
1answer
609 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 ...
2
votes
2answers
89 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 ...
4
votes
1answer
429 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 ...
1
vote
1answer
314 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?
1
vote
2answers
1k 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) " ...
4
votes
1answer
164 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 ...
5
votes
1answer
523 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 ...
2
votes
1answer
326 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 ...
-2
votes
1answer
637 views

How to redraw vim after a function call to the commandline

While doing a fun vim exercise I ran into an odd redraw problem. nmap goo :execute ':call GoogleIt("' . input("What would you like to google?: ") . '")'<cr> function GoogleIt(content) ...
1
vote
0answers
199 views

Vimrun.exe keeps opening and closing when statusline is updated

I am working on a plugin and as part of that plugin I am trying to update the statusline with setlocal statusline=%!GetMPCStatusline(). But this causes vimrun.exe to open and close very fast. This ...
7
votes
1answer
269 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/...
12
votes
4answers
702 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. ...
3
votes
0answers
184 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
1answer
456 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('.')...
5
votes
3answers
227 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.
2
votes
1answer
341 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 ...
1
vote
2answers
372 views

Call function on several lines

I have a function that changes the 6th caracter of a line to a dollar symbol ($). I want it to be performed on several lines, so I naively did a "while" loop using a counter (as shown under). But ...
2
votes
1answer
50 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 ...
3
votes
1answer
672 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 ...
4
votes
2answers
142 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 ...
2
votes
1answer
340 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 ...
3
votes
1answer
80 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 ...
8
votes
3answers
1k 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.