8

Let say I have

let l:letter = 'A'

is there a function that takes l:letter ascii code and transforms it to 'B'? I mean something like

let l:next_letter = l:letter + 1

I know there's <Ctrl-A> with set nf=alpha, but that requires to have the cursor over the character in the document and I was wondering if it's possible to do it with a variable without modifying the document.

Here's what I try so far, but as I said, it modifies the document and the cursor position:

fu! s:get_next_char(letter)
    set nf=octal,hex,alpha
    call setline(line('$') + 1, a:letter)
    exe ':normal! G^'."\<C-A>"
    set nf=octal,hex

    let l:next_letter = getline('$')
    normal! Gdd
    return l:next_letter
endfu
  • 1
    :help string-functions. – romainl Apr 20 '15 at 7:03
  • 2
    @romainl thanks for the tip but next time take into account that maybe I've been programming in vimscript for a couple of days and for what I know, string-functions is not a global standard man page for every language, seriously. – Jcao02 Apr 20 '15 at 7:25
  • 2
    I agree with @Jcao02, prior knowledge of the entirety of Vim help pages are not a requirement of this site. And I would classify :help string-functions as slightly obscure. Also, some scripting languages like python and perl use chr() and ord() functions to convert to ascii/character. So searching with these terms still wouldn't show up the nr2char and char2nr functions either. I think @romainl's comment should be posted as an answer instead. – akshay Apr 20 '15 at 7:38
  • 2
    I have to agree with romainl: it's very important to know how to navigate vim help pages. My first shell classes taught me man man. :help :help is equally important. But vim documentation is quite massive and hard to figure the first years. Thus tricks like :h function^D give us entries points. Here it would be :h functions (/:h string-functions). Then, it's easy to understand what's doing what. Even if we are used to other languages. We can always give the information (it's what we're doing most of the time), but teaching how to fish it is more important -- you know the proverb... – Luc Hermitte Apr 20 '15 at 10:24
  • 2
    @LucHermitte I would argue that just posting :help string-functions is not showing how to fish. It's more like giving someone the bait and not teaching them to fish at all. Posting how they can search for functions, is teaching them to fish. – akshay Apr 21 '15 at 2:33
12

It's easy to do with :help nr2char() and :help char2nr().

Example usage:

:let b_char = nr2char(char2nr('A') + 1)

Then, :echo b_char will return 'B'

1

The question is (emphasis mine):

is there a function that takes l:letter ascii code and transforms it to 'B'?

Since you want a function, the first thing to try is :help function<C-d> which lists every documentation tag containing function:

:function                    list-functions               folding-functions
function()                   time-functions               history-functions
functions                    mark-functions               mapping-functions
function-key                 text-functions               various-functions
function_key                 compl-function               numbered-function
function-list                local-function               autoload-functions
:function-verbose            spell-functions              quickfix-functions
function-argument            float-functions              complete-functions
nb-functions                 style-functions              anonymous-function
function-search-undo         ft-ada-functions             Dictionary-function
gui-functions                string-functions             completion-functions
arg-functions                server-functions             window-size-functions
var-functions                cursor-functions             interactive-functions
function-range-example       window-functions             command-line-functions
expr-function                buffer-functions             highlighting-functions
new-functions-5.2            system-functions             curly-braces-function-names
date-functions               syntax-functions             python-Function
dict-functions               xterm-function-keys          :endfunction
file-functions               vt100-function-keys          :delfunction
user-functions               bitwise-function

The most obvious candidate is function-list, let's try it:

There are many functions.  We will mention them here, grouped by what they are
used for.  You can find an alphabetical list here: |functions|.  Use CTRL-] on
the function name to jump to detailed help on it.

String manipulation:                    *string-functions*
    nr2char()       get a character by its ASCII value
    char2nr()       get ASCII value of a character
    [...]

You may have noticed a pattern, here: functions are grouped by theme. This means that, if you need a function to filter a list you can go directly to :help list-functions.

The Vim help pages are very useful; often times :help <keyword><C-d> will get you a long way ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.