Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: how to use variables in vimrc?

Tags:

variables

vim

here what I am trying to do, a simple function to increment a global variable. It works fine.

let g:high_ind = 1

fun! IncHighlightInd()
  let g:high_ind = (g:high_ind + 1) %10
  return g:high_ind
endf

I want to use this variable in a map

map <C-h> :call IncHighlightInd() <CR> :Highlight g:high_ind <CR>

But g:high_ind is not recognized as a variable. How to use this variable?

Or more interestingly, is it possible to do something like the below?

map <C-h> :Highlight IncHighlightInd() <CR>
like image 986
Jérôme Avatar asked Apr 10 '09 08:04

Jérôme


People also ask

How do you set a variable in Gvim?

We can also set options as variables using the let command. Run the following commands: :let &textwidth = 100 :set textwidth? Vim will display textwidth=100 .

How do I access my vimrc file?

In the terminal, type vi . vimrc . This will create an empty vimrc system file which you want to use. In the file, type set number , and then hit Esc on the keyboard and type in :wq .

What is let G in Vim?

b: local to the current buffer. l: local to a function. g: global. :help internal-variables. Follow this answer to receive notifications.

What is silent in vimrc?

<silent> tells vim to show no message when this key sequence is used. <leader> means the key sequence starts with the character assigned to variable mapleader -- a backslash, if no let mapleader = statement has executed yet at the point nmap executes.


1 Answers

You have to use :exe or c_CTRL-R_=:

nnoremap <c-h> :exe ":Highlight ".IncHighLightInd()<cr>
nnoremap <c-h> :Highlight <c-r>=IncHighLightInd()<cr><cr>

BTW, I suspect you should have a look at this page: Highlight multiple words on vim.wikia.

like image 157
Luc Hermitte Avatar answered Sep 19 '22 19:09

Luc Hermitte