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
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 ...
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 ...
1
vote
2answers
36 views

VIM command to insert multiline text with argument

new VIM user. I'm trying to make creating python properties easier for my class definitions. What I would like for say I type :pyp x then VIM will autofill where my cursor is @property def x(self):...
1
vote
3answers
54 views

NERDTree maximize on enter

I'd like to maximize NERDTree when I enter the NERDTree buffer. The command I have so far is: autocmd BufEnter NERD_tree_* <A> ...but the <A> part is wrong. I get the error: E21: Cannot ...
1
vote
1answer
88 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 ...
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?
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 ...
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 ...
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
2answers
60 views

Insert text in the middle of a string vimscript

I'm trying to make a function in vimscript that converts markdown to html files using pandoc and putting that converted file in a specified directory. Currently i am putting the file in a directory ...
1
vote
2answers
27 views

Gvim - Change window position horizontally

I want to create a command that will place my gvim window to a different "x" location on my screen while maintaining the "y" position. I do this in my _gvimrc by calling winpos 319 16 I want to ...
1
vote
1answer
59 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. ...
1
vote
1answer
70 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 ...
1
vote
1answer
62 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 ...
1
vote
2answers
63 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
1answer
252 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
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 ...
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 ...
1
vote
1answer
67 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
1answer
69 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! ...
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 ...
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 ...
1
vote
1answer
472 views

How do I use input() to get the name of a new composer project from the user?

I often start new projects in the same way. I guess it exists a way to be fast a starting a project. For example: I am trying to start new composer project with the following function. I perfectly ...
1
vote
0answers
68 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 ...
1
vote
1answer
98 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 ...
1
vote
0answers
59 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/...
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 ...
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 ...
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 ...
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 ...
1
vote
0answers
205 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 ...
0
votes
3answers
161 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
69 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
1answer
31 views

Yank both sides of equation

While writing in latex I often want to copy a math equation's right hand side or left hand side. For example I have something like this \begin{gather*} 5 \cdot \left( A A 3 \right) + 50 \cdot \...
0
votes
1answer
31 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
56 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?
0
votes
1answer
30 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-\&...
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 ...
0
votes
1answer
24 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
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
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 ...
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
1answer
39 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 ...
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 ...
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 ...
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 ...
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
578 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 ...
0
votes
1answer
52 views

Run vimscript function only once although v:count1 > 1

I have a function that accepts a count of iterations, its command is defined with -range: command -nargs=1 -range DocbkPrintTag :call s:DocbkPrintTag(<f-args>) and to output <listitem> &...