practicalli / neovim

Neovim, Conjure and LSP for Clojure development using Fennel configuration
https://practical.li/neovim/
Creative Commons Attribution Share Alike 4.0 International
20 stars 4 forks source link

neovim insert filename or path into buffer #42

Open practicalli-johnny opened 11 months ago

practicalli-johnny commented 11 months ago

How to get the filename or full path of a file and then past it into a buffer

Use cases

Normal mode

" % is the neovim register that holds the filename associated with the current buffer (if it has a file associated with it)

" % p will past the filename into the buffer in normal mode

Vim command

:read inserts the output of a command into the current buffer

:read echo % inserts the filename

:read! echo %:p inserts the full path of the file

:help read and :help filename-modifiers for more details

:put % inserts the filename from the % register

:put=expand('%:p') inserts the full path to the file

A more terse approach is to use !! which replaces the current line in the buffer with the result of the next command

!!echo %

Use basename to ensure only the the file name even when the register contains the full path

`!!basename %``

Bind a key (review)

inserts the current filename without the extension at the cursor position, when you are in insert mode.

:inoremap \fn =expand("%:t:r") To keep the extension use:

:inoremap \fn =expand("%:t") To insert the absolute path of the directory the file is in use:

:inoremap \fn =expand("%:p:h") To insert the relative path of the directory the file is in use:

:inoremap \fn =expand("%:h")