Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Vimscript path


May 24, 2021 Vim


Table of contents


Vim is a text editor, which (often) processes text files. T ext files are stored in the file system, and we use paths to describe files. Vimscript has some built-in methods that can help you when you need to work with paths.

Absolute path

Sometimes external scripts also need to get the absolute path name of a particular file. Execute the following command:

:echom expand('%')
:echom expand('%:p')
:echom fnamemodify('foo.txt', ':p')

The first command shows the relative path of the file we are editing. % indicates "current file." Vim also supports some other strings as expand()

The second command displays the full absolute path name of the current file. T he :p :p string tells Vim that you need an absolute path. There are many other modifiers that can be used here.

The third command shows the absolute path of the file foo.txt current folder, regardless of whether the file exists. T ry to see if the file doesn't exist? fnamemodify() a much more flexible Vim function than expand() you can specify any file name fnamemodify() and not just the special string required for expand()

List the files

You may want to get a list of files under a specific folder. Execute the following command:

:echo globpath('.', '*')

Vim outputs all files and folders in the current directory. globpath() function returns a string, each of which is separated by a line break. I n order to get a list, you need to go split() To execute this command:

:echo split(globpath('.', '*'), '\n')

This time Vim displays a Vimscript list that includes individual file paths. If your file name includes line breaks, you'll have to figure it out for yourself.

globpath() work just as you think. Execute the following command:

:echo split(globpath('.', '*.txt'), '\n')

Vim displays a list of all the .txt .txt the current folder.

You can ** recursively with . To execute this command:

:echo split(globpath('.', '**'), '\n')

Vim lists all files and folders under the current folder.

globpath() very powerful. You'll learn more when you've completed this chapter of practice.

Practice

Read :help expand() .

Read: :help fnamemodify() .

Read: :help filename-modifiers .

Read :help simplify() .

Read :help resolve() .

Read: :help globpath() .

Read :help wildcards .