51

The best way is to use has(), with this function you can check for features of Vim; OS specific features from :help feature-list: macunix Macintosh version of Vim, using Unix files (OS-X). unix Unix version of Vim. win32 Win32 version of Vim (MS-Windows 95 and later, 32 or 64 bits) ...


39

Yes, you can take optional arguments in a function. It is not as convenient as python's way of doing it, but this is how you do it: function FooBar(...) " This is like *args in python echom a:0 " a:0 contains an integer which is the number of arguments passed to the function echom a:1 " a:1 contains the first argument passed, a:2 contains the ...


39

You can call the function silently by defining a silent map: noremap <silent> H :call FirstCharOrFirstCol()<cr> For more info, see :h :map-<silent>. Note in particular that this will only ensure that the command is not echoed to screen when the mapping is executed. The :silent command is used to silence output from the function itself (...


38

let assigns a value to a variable, and set assigns a value to one of Vim's internal options. For example, you would use :let mystring='Hello!' to declare a new variable, but you would use :set tabstop=4 or :set expandtab to set one of Vim's options. See also: :help let, :help set. g: simply signifies a global variable. There are several of these "...


34

As @lcd047 said in his comment, vimscript use C-like operators && and ||. You can find description of their usage on :h expr2. Some important points mentioned by the doc are the following You'll find that the operators can be concatenated and && takes precedence over ||, so &nu || &list && &shell == "csh" Is equivalent ...


32

From :he filename-modifiers: :t Tail of the file name (last component of the name). Must precede any :r or :e. :r Root of the file name (the last extension removed). When there is only an extension (file name that starts with '.', e.g., ".vimrc"), it is not removed. Can be repeated to remove ...


31

Late to the party a bit but I didn't see my favorite one: function! FunctionName(arg1,...) let arg2 = get(a:, 1, 0) let arg3 = get(a:, 2, 0) if arg2 "Do stuff with arguments" else "Do stuff without arguments" endif endfunction in which get(a:, n, default) will get the nth optional argument returning default if it's not ...


30

You can do this with the system function: let language = system('echo $LANG') Bonus point: if your output is a list, you can use the systemlist instead to get back a list. e.g. let files = systemlist('ls') " ['bin', 'dev', ... ] ref: :h system


29

has() sounds like a good idea until you try it on Mac OS X: in the default /usr/bin/vim, has('unix') is true but both has('macunix') and has('mac') are false while, in the regular MacVim download, all three are true whether you use the GUI or the TUI. So the best solution is a mix of has('winXX') for Windows and uname on unix-like systems. Note that the ...


29

Background: I was playing around with Vim 8.2's popups recently (I have a small plugin that allows navigation of sections in a markup document and I was looking at showing section hierarchies in popups). I figured the info I collected would make a decent introduction... Overview Vim 8.2's popup windows allow Vimscript authors and plugin developers to ...


25

This pattern will let you assign meaningful names to each argument, and provide a default value for any arguments that were not provided: function FunctionName(foo, ...) let bar = a:0 >= 1 ? a:1 : 0 let baz = a:0 >= 2 ? a:2 : 0 ... " Code that makes use of a:foo, bar and baz As pointed out by Boris Brodski: a:0 counts the number of ...


25

let @z = systemlist('date')[0] removes the newline for you.


25

A "script" does nothing more than run a sequence of ex commands. An "ex command" is what you type when you use : in Vim. For example :wq, :set wrap, :e file, etc. are all ex commands. The : is not part of the command; it is merely a keystroke to start the command-line mode; you don't always need to include the :, for example when you chain multiple commands ...


25

The only difference between single and double quoted string is related to backslash. To display special characters like newline, bells, tabs, etc, you need to use double-quotes -> "\n". Within a single-quoted string, '\' is itself => '\n' is a two-characters string (a backslash + n). Within double quotes, you have to double it -> "\\", which makes them un-...


24

You can use substitute(), or define a function: function! Chomp(string) return substitute(a:string, '\n\+$', '', '') endfunction This variant will call system for you and then chomp the result: function! ChompedSystem( ... ) return substitute(call('system', a:000), '\n\+$', '', '') endfunction (This function is also available in my ingo-library ...


23

Remove the trailing <cr> That is only needed for mappings, but not for commands.


23

The execute function takes a string as argument, it expands the string and execute it a a regular ex command. Thus you can do: function! MyFunction(someArg) execute "normal! ". a:someArg. "l" endfunction The . is a standard vimscript operator to concatenate strings. execute can be shortened to exe. See :h :execute EDIT I'll add a point about the ...


21

You should use getpos(): To save you position in a variable: let save_pos = getpos(".") getpos() takes as argument a mark, here "." represents the current position of your cursor. And to restore it: call setpos('.', save_pos) Here the first argument indicate that you will move the mark of the current position of your cursor (hence your current position)...


20

There's a lot of way to have fun in Vim: for example, one could install the vim-script nibble plugin (this one has a dependency which also must be installed). Once the plugin is installed the command :Nibble will start a snake game in a new buffer. The snake can be controlled with hjkl and the game can be paused with space. With that it is possible to spend ...


20

You need to put another | before else otherwise vim will think your command is set listchars+=space:· else instead of two separate commands. That's why you got the error message: else is an unknown option for the command set. The correct syntax is the following: if has("patch-7.4.710") | set listchars+=space:· | else | set listchars+=trail:· | endif


19

Well… it's just as simple in vimscript. Current window Python: current_win = vim.current.window Vimscript: let current_win = winnr() Current buffer Python: current_buff = vim.current.buffer Vimscript: let current_buff = bufnr("%") Current tabpage Python: current_tabpage = vim.current.tabpage Vimscript: let current_tabpage = tabpagenr() See :...


18

To answer your question: the prototype of call() in the manual is call({func}, {arglist} [, {dict}]); the {arglist} argument needs to be literally a List object, not a list of arguments. That is, you have to write it like this: let @x = call(a:functionToExecute, [GetSelectedText()]) This assumes a:functionToExecute is either a Funcref (see :help Funcref), ...


16

If you were looking for the last point where an option was set (instead of a variable), you could use the :verbose command: :4verbose set is? incsearch Last set from ~/.vim/vimrc But currently there is no equivalent command for variables. You could inspect the code, or use search in the source, possibly using :vimgrep, :vim netrw_list_hide **/* (...


16

Can somebody tell me how to avoid the very annoying vim's slowdown? Hopefully in a user friendly non-geeky way? I run Vim on a company-provided 2013 15" Retina MacBook with a 2.3 GHz i7 processor, 16 GB of RAM, and a measly GT 750M with 2 GB dedicated memory. That machine is very far from a "gaming PC" and I've never experienced any slow down. Ever. Even on ...


16

That's part of the autoload mechanism. From Autoloading at Learn Vimscript the Hard Way and :help autoload: Autoload lets you delay loading code until it's actually needed, so if the following function execute pathogen#infect() has already been loaded, Vim will simply call it normally. Otherwise Vim will look for a file called autoload/pathogen.vim ...


15

Give this a try: function! Paste_Func() let s:inPaste = &paste if !s:inPaste set paste endif echom s:inPaste augroup paste_callback autocmd! autocmd InsertLeave <buffer> call Paste_End() augroup END startinsert endfunction function! Paste_End() augroup paste_callback autocmd! ...


15

You can use the &{option-name} in an if-statement like so: if &guioptions ==# "Trl" echo "Toolbars and scrollbars are present!" elseif &guioptions ==# "" echo "No toolbars and scrollbars present!" endif The & specifies that the variable name is a Vim option. See :help :let-& for the full documentation.


15

Lets fail with Style! silent! colorscheme evening silent! colorscheme solarized The :silent! will ignore the errors at start up. Just put color schemes in reverse order of what you really want. For more help see: :h :silent


15

Here is why : 42 / 0 tends to +infinity And how does Vim represent the largest number available ? 2147483647 See :h limits Furthermore, the float2nr function documentation states : When the value of {expr} is out of range for a |Number| the result is truncated to 0x7fffffff or -0x7fffffff. NaN results in -0x80000000. So you have here your 2 numbers :...


15

The command :Reverse explained at Vim Wiki can be used for this (you may include it in your .vimrc to make it permanent): command! -bar -range=% Reverse <line1>,<line2>g/^/m<line1>-1|nohl Then you can record a macro to run the command on every four lines: qmV3j:Reverse<cr>4jq 1000@m Explanation: qm: 'q' in normal mode starts (and ...


Only top voted, non community-wiki answers of a minimum length are eligible