44

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) ...


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 "...


37

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 (...


35

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 ...


31

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 ...


29

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 ...


28

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 ...


27

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


25

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


24

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 ...


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 ...


24

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-...


23

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 ...


22

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


22

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 ...


21

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 ...


21

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 ...


19

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)...


19

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


18

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 :...


16

To answer youre 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

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

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 ...


15

Unlike :source, :runtime doesn't need an absolute path to work. It is much better than :source for building portable setups. See :help :runtime. In the following example, we tell Vim to look for a vimrc file in the directories given by the runtimepath variable. Since ~/.vim/ is the first directory in the list, runtime vimrc works just like :source ~/.vim/...


15

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 ...


14

A possible solution is to use jumps: Before yanking or visually selecting you can set a new jump which you'll be able to access after your cursor has moved. To do so use m`. Then after your cursor has moved because of the yanking use ctrlo to jump back in the jump list. Also you can see the list of the available jumps with :jumps and navigate this list ...


14

If LANG is an environment variable you can just do: let language = $LANG Or, even simpler: if $LANG == 'en' … endif


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