Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [functions]

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

0
votes
1answer
11 views

Check nerd tree is open function

I'm trying to create a simple function in vim which lets me map <leader>b, <leader>n, and <leader>0 to execute a function that checks if a nerd tree is open then cycle tabs and do :...
-1
votes
2answers
25 views

Increase numbers by one in a visual block

I edit DocBook XML files. In my plugin, i want to implement a function that would increase or decrease the section numbers recursively, assuming that cursor is at the toplevel section. For example, ...
0
votes
0answers
30 views

How to stop a function when <esc> is pressed? OR how to catch a <esc> signal in vimscript?

I have a function which calls another function that reads user input from input(). function! fun1() call fun2() endfunction function! fun2() let temp = input("Please input:") endfunction What I ...
5
votes
3answers
398 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 ...
1
vote
1answer
50 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. ...
5
votes
3answers
149 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"),(...
3
votes
1answer
598 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 ...
0
votes
1answer
25 views

How to return control characters from a function?

I have a keybinding that works: inoremap <silent> <Tab> <C-r>= \ pumvisible() ? "\<lt>C-N>" : \ CursorAfterOnlyWhitespace() ? "\<lt>Tab>" : \ "\<lt>C-\&...
1
vote
1answer
69 views

function: sort output alphabetically

This function outputs data between braces as joined lines in a quickfix window. The output is sorted based on line numbers. I'm searching for a method to sort the data alphabetically. I tried to ...
0
votes
1answer
41 views

Why does my function sometimes respect whitespace?

I have a few functions to help with adding PHP docblock tags. Suppose a block like this: /** * Does a thing * * | */ Where | is my cursor position. I have a mapping to <localleader>@p, for ...
1
vote
1answer
62 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 ...
0
votes
1answer
37 views

map <expr> <F2> Cannot Check Focus Pane

How can a keypress dynamically change its behavior, depending upon which split pane has focus? (Specifically, whether the pane containing the cursor has another split pane to the right, or not.) map ...
1
vote
0answers
47 views

Add conceal in function

Trying to maintain add conceals to the generated location list, but I can not figure it out. The current workaround is to add it manually everytime through the vim commandline, like: : set ...
0
votes
0answers
87 views

Exclamation mark after autocommands

I'm trying to understand what does exclamation mark after autocommands mean. Sometimes it is possible to see something like this: function! LangSpecificOptions() if (&filetype == 'markdown' || ...
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) " ...
2
votes
1answer
49 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 ...
0
votes
1answer
57 views

function to search for a pattern

I just recently found a very nice command to search for a pattern in a document, and display all hits: :g/pattern/z#.1 Since the command is a bit cumbersome to type, I would like to make a shortcut, ...
0
votes
2answers
43 views

Vim: Foldtext contain all content, instead of only the first line

Would like to have all content of the folded block, instead of only the first line. My current foldsettings are: set foldmethod=marker set foldmarker={,} Example how my data is structured: { ...
0
votes
1answer
35 views

Function arguments + shell

I'm trying to write a command for adding a Python import line. The problem is that what's added it import a:name and not the module name. Here's the code func! PyImport(name) w silent !isort ...
0
votes
1answer
24 views

vimL, how to get alternate window's current buffer number

Say, with 2 windows open in vim, cursor is window 2, how can I get the current buffer of the alternate window (window 1 in this case)?
0
votes
1answer
39 views

Capture the next keypress under 200ms

1) I need to capture the next key press from the keyboard, if there are any in 200 millisecond. Which function should I use to achieve this? 2) Basically I need to re-send the captured key, with some ...
0
votes
1answer
21 views

Is it possible to non-recursively remap a command?

So Fugitive defines :Glog. This is, apparently, aliased to a call to the script-local s:Log() function (according to :command Glog.) My muscle-memory has been using glog at the command-line, for ...
0
votes
3answers
122 views

How can I echo the result of function with a range?

If I have a function that takes a range, how can I echo the return value of the function? For example, the following does not work: :echo 2,8call MyFunction()
0
votes
1answer
38 views

Why does `:call f(…)` do nothing while mapping it to a binding works?

I'm not really sure how to describe this behavior better than with the example I'm dealing with. I'm trying to write a custom completion using fzf-vim ... and as I'm looking through the documentation,...
0
votes
1answer
52 views

What does it mean by `…` parameters in a function?

I often see vim function with function definition written like this: function! MyFunction(...) " function body... endfunction What does the ... do within the function?
6
votes
2answers
495 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 ...
1
vote
1answer
52 views

Binding horizontal scroll in a function with feedkeys

I'm trying to create a function to make editing TSV files easier. The function changes spacing settings, turns off wrapping, and creates a 1 row tall split at the top so that the column headers are ...
0
votes
1answer
100 views

Sharing vim commands history across different users and computers

I have configured .dotfiles. Eg. see here https://wiki.archlinux.org/index.php/Dotfiles So now I can do dgit push or dgit pull on whichever computer I want, and I have all my config files (including ....
0
votes
1answer
67 views

Function Unable to Fail: Breaks Macros

Vim functions do not seem to be able to fail without spitting out 3 unnecessary lines. For example, take this set of functions and maps: " NEXT / PREVIOUS " ~~~~~~~~~~~~~~~ " n and N always search ...
0
votes
1answer
31 views

How to get the number used before a command as an argument?

How can I write a function that can be invoked in normal mode with regular keystrokes like {myNumber}<Leader>p, and prints {myNumber} into the current buffer. For example: here's a command to ...
1
vote
1answer
56 views

How to exactly search current word?

Want to exactly matching current word with below function. function! My_Exact_Search () let CurrentWord = expand("<cword>") exec "/\<".CurrentWord."\>" endfunction But get error: ...
1
vote
2answers
62 views

separating folds with constant amount of newlines

Is there a succinct way to separate folds, open or closed, by the same number of newlines throughout the buffer? I'm using fold by indent. The goal is two visible blank lines between each fold (...
1
vote
0answers
56 views

Monkey-patch plugin function

Is it possible to override an autoloaded plugin-function? I want to customize an airline-internal function just like this: diff --git a/autoload/airline/extensions/default.vim b/autoload/airline/...
0
votes
1answer
37 views

How to invoke mapped `i_CTRL-W` in function

First I will describe what I am trying to achieve: In general I use set iskeyword+=- as I see this more usable (one should always put spaces around operators when needed). This is quite helpful, but ...
0
votes
1answer
131 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>') ...
1
vote
1answer
190 views

How to source .vimrc inside a function in .vimrc?

This is part of my vimrc-- function! Abbreviations() let a:code=input("Which Abbreviation: ") "gen if(a:code=="gen") source ~/.vim/Abbreviations/GeneralAbbreviations.vim endif ...
1
vote
1answer
38 views

How do I get help for a specific package?

I am at a phase of cleaning up my choice of plugins and I want to read the documentation provided by a package through vim it self. With that in mind, I don't know a way to: get to the top of the ...
3
votes
0answers
342 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' : ...
4
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 ...
3
votes
3answers
643 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 ...
0
votes
1answer
121 views

Convert newline to something else in variable using substitute

Inside of a variable, a newline is encoded as ^@ which represents a null byte. In the vim Ex command line it is possible to issue something like substitute(var, '^@', '\\n', 'g'), this produces \n ...
1
vote
1answer
111 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
42 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 ...
1
vote
2answers
213 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
67 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
48 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
147 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
60 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
49 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: ...