Stack Exchange Network

Stack Exchange network consists of 174 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.

1
vote
1answer
34 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
60 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
51 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 ...
1
vote
1answer
31 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: ...
0
votes
1answer
29 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
2answers
55 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
50 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
0answers
30 views

Is there anyway to look at the implementation of default commnads in vim

Talking about the default vim commands, I imagine that most basic commands are implemented in C++, but some are simply vimscript functions that use other commands. For example, ci" might be using T"...
0
votes
1answer
33 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
71 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 ...
0
votes
1answer
32 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 ...
1
vote
1answer
37 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
69 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
38 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
42 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 ...
0
votes
2answers
89 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
58 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
36 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
57 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
46 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
30 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
36 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
71 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
2answers
49 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
78 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
0answers
39 views

Is there any way to postpone updating/redrawing the window in a function that issues buffer-changing commands?

I'm looking to just completely freeze my view until a subsequent "restore the screen" command is issued. Even if the command is something like "delete this line." My understanding is winsaveview and ...
0
votes
1answer
37 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
99 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
174 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
26 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
13 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
137 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
36 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
47 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
131 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
977 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
180 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
76 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
65 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
90 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
104 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
30 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
136 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 ...
4
votes
3answers
101 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
0answers
69 views

How to append an action/function to a key mapping?

I want to call my function after a yank or delete is performed. For example, after yy, dw, D, etc. My function is very simple, all it does is make a system call with the contents of the " register. ...
0
votes
0answers
41 views

Function to go to the middle of the current line [duplicate]

I have been trying to adapt a snippet I found around the internet to go to the middle of the physical line, ignoring whitespaces both at the start as well as the end. The issue is it doesn't work ...
0
votes
1answer
299 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 ...
2
votes
1answer
184 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 ...
2
votes
1answer
77 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 ...
4
votes
1answer
578 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! ...