I'd like to add commands in my .vimrc
programmatically in a function. For example, instead of hardcoding commands like this
command! MyCommand echom "My command"
I'd prefer to add them in a simple function like this
function! AddCommands()
for commandName in ["One", "Two", "Three"]
" TODO: Add a command with the given name that prints something
endfor
endfunction
How can I add the commands One
, Two
, and Three
using the for loop above?
Once I got that working, I want to read a file containing file paths and automatically create commands that open those files. I call that file "bookmarks file".
A line in the bookmarks file looks like this:
BookmarkName: /path/to/bookmarked/file.txt
Here's what I got so far:
" Read file paths from our bookmark file
" and create a command for each bookmark that, when executed, opens the
" file in a new tab.
function! ReadBookmarks()
let bookmarks = readfile(fnameescape($BOOKMARKS_FILE))
let bookmarkSeparator = ": "
for bookmark in bookmarks
let nameAndPath = split(bookmark, bookmarkSeparator)
let name = nameAndPath[0]
let path = nameAndPath[1]
" TODO: Add a command that opens the file in a new tab
endfor
endfunction
execute
) is in no way specific to code that exists in the .vimrc. I also agree with @statox that a marginally less-contrived example would improve the question.