A manager of your Leetcode question and solution files in Vim.
This plugin downloads, loads and manages your Leetcode question and solution files (check this example repository to view the file structure of the leetcode files and the README.md generated by this plugin). To make the experience of doing Leetcode questions on vim tremendously smooth, this plugin also offers pretty much convenient features, e.g. display the question file in a vertical split window next to the solution file (configurable), automatically move your cursor to your last edit position when you re-open an old solution file, automatically add code to import common libraries so that syntax checking plugins such as syntastic may work, etc. What is more, it is made to not pollute your undo history, search history, change list and jump list. Therefore, you don't need to worry about the dysfunction of the built-in mappings such as g;
, <C-o>
and u
.
- POSIX shell commands. Windows users may need to install tools such as cygwin.
- Have leetcode-cli installed and sign in.
- "skygragon/leetcode-cli" is no longer in maintenance. Please fetch "leetcode-tools/leetcode-cli".
- If encountering problems in using it, check the status of its plugins. Please be reminded that cookie plugin is required for logging in a leetcode account.
For your reference, in an Unix-like system:
mkdir -p ~/.vim/plugged
cd ~/.vim/plugged
git clone https://github.com/8ooo8/leetcode.git
In ~/.vimrc, add below statement.
set runtimepath^=~/.vim/plugged/leetcode
For example, with manager "vim-plug":
Plug '8ooo8/leetcode'
:LdoQ [Question-ID-or-Name] [Code-Filename]
- Load the specified leetcode question and solution files. Download them if they do not exist.
- If the specified files do not exist, download the question file and solution template file using leetcode-cli.
- The programming language of the solution template file is determined by
g:leetcode_lang
. - The location where the files are stored is determined by
g:leetcode_root_dir
. - This command loads the specified solution file and possibly also the question file in a vertical split window, depending on the value of
g:leetcode_view_Q
. - Usages:
- (1)
LdoQ
- Load the last downloaded solution file and possibly also its corresponding question file in a split window.
- (2)
LdoQ Question-ID-or-Name
- Load the solution file lastly opened by the command
LdoQ
among those solution file(s) for the specified question and possibly also its corresponding question file in a split window. - If no such file, download the question file as well as the solution template file and load. The name of the solution file is given by leetcode-cli.
- Load the solution file lastly opened by the command
- (3)
LdoQ Question-ID-or-Name Code-Filename
- Load the specified solution file and possibly also its corresponding question file in a split window.
- If no such file, download the question file as well as the solution template file and load. The solution file will be named as
Code-Filename
.
- The parameter
Question-ID-or-Name
has to be in the forms of "ID", "Name" or "[Question-ID] Question-Name". Case-insensitive. - Press
<Tab>
to auto complete the parameters with the existing question and solution files;<C-d>
to list the available parameters.
- (1)
- Check this example repostory to view the file structure.
- This command adds code to the newly downloaded solution files to import the common libraries as well as the classes provided by the questions so that syntax checking apps, such as "syntastic", still work when the mentioned libraries and classes are used.
- This command closes all other windows in the current tab.
- This command changes the present working directory of the window(s) into the directory of the leetcode file(s).
- This command moves the cursor to the position of the last change made to the loaded solution file. If it is a newly downloaded solution file, move the cursor to where users' solution should start. Afterwards, depending on the value of
g:leetcode_auto_insert
, possibly also automatically enter Insert mode. - This command does not pollute the change list, jump list, search history and undo history. Hence, built-in mappings such as
g;
,<C-O>
andu
are not affected.
:LrenameCodeFile {New-Name}
- Rename the solution file in the current window with
{New-Name}
.
:Ltest[!] [Test-Case]
:Ltest
to test with the last failed testcase in your previous test(s) / submission(s) for the current LeetCode question, and if there is no failed testcase before, test with the default testcase.:Ltest <Test-Case>
to test with the <Test-Case>.:Lest!
to test with the default testcase.- Before submitting the solution to test, the code inserted for syntax check by
LdoQ
is commented to avoid an influence to the test. Upon the finish of the test, the code is uncommented and the solution file is saved. Change list, jump list, search history and undo history are unpolluted. Hence, built-in mappings such asg;
,<C-O>
andu
are not affected. - Use '\n' to separate the parameters, e.g.
:Ltest ParameterA\nParameterB
, e.g.Ltest ""1010"\n"1011""
:Lsubmit
- Sumbit the solution file in the current window.
- Before submitting the code, the code inserted for syntax check by
LdoQ
is commented to avoid an influence to the submission. Upon the finish of the submission, the code is uncommented and the solution file is saved. Change list, jump list, search history and undo history are unpolluted. Hence, built-in mappings such asg;
,<C-O>
andu
are not affected.
:LprintLastRunResult
:LupdateREADME
- Update the table of content(see below) in README.md; if no table found, append a table to README.md.
- The entries in the "Question" column are linked to the Leetcode official web pages which show the same questions; and the entries in the "Solution" column are linked to the corresponding solution files.
- The content in the columns "Question", "Difficulty", "Acceptance" and "Solution" are automatically generated while the rest needs to be input manually.
- The tuples are sorted by their acceptance rates or last modified time. Check
g:leetcode_table_of_finished_questions_sorted_by_acceptance_rate
. - Check this example for a clearer view of this command.
let g:leetcode_root_dir = 'the_root_directory_of_leetcode_files'
- Set this to determine where to store the leetcode question and solution files, as well as where to search for the existing leetcode files.
- If this variable does not exist, the present working directory of the current window in Vim will be used.
let g:leetcode_lang = 'cpp'
- Set this to determine which programming language to be used to do the leetcode questions.
- Default value: 'cpp'
- Currently supported language(s): 'cpp'
let g:leetcode_view_Q = 1
- Set 1 to also load the question file in a vertical split window when
LdoQ
command is used to load a solution file; 0 to turn this off. - Default value: 1
let g:leetcode_auto_insert = 1
- Set 1 to automatically enter insert mode after loading a solution file using command
LdoQ
(like auto pressinga
to enter insert mode); 0 to turn this off. - Default value: 1
let g:leetcode_table_of_finished_questions_sorted_by_acceptance_rate = 1
- Set 1 to make the table finished leetcode questions (generated by command
LupdateREADME
) sorted by acceptance rate (from low to high). - Set 0 to make the table finished leetcode questions (generated by command
LupdateREADME
) in reverse chronological order. - Default value: 0