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
1answer
118 views

Visual mode toggle block commenting with vimrc command

I am trying to get vim to do block commenting and uncommenting of highlighted lines while keeping the correct indentation level. I am so frustratingly close. let b:commentChar='//' autocmd ...
0
votes
1answer
46 views

Add some lines in vim

!sed -i "N;4i\xxxxxxxx" % !sed -i "N;10a\yyyyyyyy" % Add a line containing xxxxxxx before the 4-th line,and add a line containing yyyyyyyy after the 10-th line? How to write a function to add a ...
3
votes
2answers
276 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 ...
6
votes
1answer
76 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 ...
1
vote
1answer
55 views

How to transform this mapping into a function?

So I made this mapping "%p$?/<CR>d$V"uy:cd <C-r>u<CR> That is the crux of the mapping but it leaves artifacts in the current file, that is part of what I want to avoid. My attempt ...
0
votes
1answer
195 views

Learning vimscripting and functions

Can you help me how this function does what it does, like step by step? function! GitBranch() return system("git rev-parse --abbrev-ref HEAD 2>/dev/null | tr -d '\n'") endfunction I ...
1
vote
1answer
68 views

Convert control-character (^F) to ascii string (“^F”)

I'm writing a vimscript function that converts a single ASCII control-character in the range 1 - 26 (e.g. ^F) to its two-character printable representation (e.g. ^ F). Currently, I have: func! ...
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 ...
0
votes
1answer
55 views

passing local script variables to viml functions

If I run this code: let s:user_config=fnamemodify('user.vim', ':p') echom s:user_config if filereadable("s:user_config") echom 'this is true' else echom 'this is false' endif the result will be: ...
0
votes
2answers
44 views

Extract JavaScript variable (feature example): how to improve it?

I am a Vim newbie with one week experience and I'm already enjoying it. I've successfully written an (ugly) command+function+mapping with which I can extract some code to a new variable in JavaScript....
1
vote
1answer
128 views

How to use function returns?

I want to create a function to copy VHDL component. I can get the component start and end line number as returned value from other functions. But don't know how to use the returns to copy the ...
0
votes
1answer
97 views

My status line is getting erased, so I can't print function result

I have a function that counts the number of matches in a file using the substitute Ex command with the /n flag and returns the value. However, I cannot echo the answer because the status line is ...
0
votes
1answer
68 views

What does execute() return?

In the function-list, it gives execute() as running an ex command, but it does not seem to be returning a string as I expect. For example, if I run this on a file that has 'abc' in it somewhere: :...
0
votes
1answer
153 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 ...
7
votes
1answer
274 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/...
0
votes
1answer
76 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
20 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
182 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>; " ...
7
votes
1answer
241 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 ...
4
votes
1answer
100 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 ...
2
votes
1answer
112 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
308 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
4answers
1k 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 ...
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 ...
0
votes
1answer
128 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 ...
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 ...
2
votes
0answers
132 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
169 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
180 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 ...
0
votes
1answer
577 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
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 ...
1
vote
1answer
283 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 ...
4
votes
1answer
468 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
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
1answer
526 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
934 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
32 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 ...
0
votes
2answers
124 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
228 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 ...
2
votes
2answers
91 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 ...
1
vote
1answer
331 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?
4
votes
1answer
171 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
540 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
339 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 ...
1
vote
2answers
385 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
364 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 ...
12
votes
4answers
735 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. ...
-2
votes
1answer
669 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) ...