Фон
Я заинтересован в написании функции, которая назначается сочетанию клавиш ; s при вызове:
Пример * * тысяча двадцать-одна
Для аргумента abc
функция вставит:
# abc ---------------------------------------------------------------------
Проблема
Приведенный ниже код не вставляет нужный текст, а только добавляет значение 0
.
код
" The functions inserts RStudio like section break. Starting with a word and
" continuing with a number of - characters.
function! InsertSectionBreak()
let title = input("Section title: ") " Collect title
let title_length = strlen(title) " Number of repetitions
let times = 80 - (title_length + 1)
let char = "-" " Create line break
let sep_line = repeat(char, times)
let final_string = '#' + title + ' ' + sep_line " Create final title string
call setline('.', , getline('.'), final_string) " Get current line and insert string
endfunction
" Map function to keyboard shortcut ';s'
nmap <silent> ;s :call InsertSectionBreak()<CR>
Обновление
Следуя советам, высказанным в комментариях, я переписал функцию на:
function! InsertSectionBreak()
let title = input("Section title: ") " Collect title
let title_length = strlen(title) " Number of repetitions
let times = 80 - (title_length + 1)
let char = "-" " Create line break
let sep_line = repeat(char, times)
let final_string = '#' + title + ' ' + sep_line " Create final title string
call append(line('.'), final_string) " Get current line and insert string
endfunction
Поведение
Функция теперь вставляет 0
под текущую строку. Я считаю, что final_string
не сконструирован должным образом.